Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloudv2: s/ReferenceID/TestRunID/ #3137

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cloudapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"encoding/json"
"time"

"gopkg.in/guregu/null.v3"

"github.com/mstoykov/envconfig"

"go.k6.io/k6/lib/types"
"gopkg.in/guregu/null.v3"
)

// Config holds all the necessary data and options for sending metrics to the k6 Cloud.
Expand All @@ -24,7 +22,6 @@ type Config struct {
Timeout types.NullDuration `json:"timeout" envconfig:"K6_CLOUD_TIMEOUT"`

LogsTailURL null.String `json:"-" envconfig:"K6_CLOUD_LOGS_TAIL_URL"`
PushRefID null.String `json:"pushRefID" envconfig:"K6_CLOUD_PUSH_REF_ID"`
WebAppURL null.String `json:"webAppURL" envconfig:"K6_CLOUD_WEB_APP_URL"`
TestRunDetails null.String `json:"testRunDetails" envconfig:"K6_CLOUD_TEST_RUN_DETAILS"`
NoCompress null.Bool `json:"noCompress" envconfig:"K6_CLOUD_NO_COMPRESS"`
Expand All @@ -34,6 +31,12 @@ type Config struct {
// Defines the max allowed number of time series in a single batch.
MaxTimeSeriesInBatch null.Int `json:"maxTimeSeriesInBatch" envconfig:"K6_CLOUD_MAX_TIME_SERIES_IN_BATCH"`

// PushRefID represents the test run id.
// Note: It is a legacy name used by the backend, the code in k6 open-source
// references it as test run id.
// Currently, a renaming is not planned.
PushRefID null.String `json:"pushRefID" envconfig:"K6_CLOUD_PUSH_REF_ID"`

// The time interval between periodic API calls for sending samples to the cloud ingest service.
MetricPushInterval types.NullDuration `json:"metricPushInterval" envconfig:"K6_CLOUD_METRIC_PUSH_INTERVAL"`

Expand Down
6 changes: 3 additions & 3 deletions output/cloud/expv2/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type pusher interface {
}

type metricsFlusher struct {
referenceID string
testRunID string
bq *bucketQ
client pusher
aggregationPeriodInSeconds uint32
Expand All @@ -39,7 +39,7 @@ func (f *metricsFlusher) flush(_ context.Context) error {
// and group them by metric. To avoid doing too many loops and allocations,
// the metricSetBuilder is used for doing it during the traverse of the buckets.

msb := newMetricSetBuilder(f.referenceID, f.aggregationPeriodInSeconds)
msb := newMetricSetBuilder(f.testRunID, f.aggregationPeriodInSeconds)
for i := 0; i < len(buckets); i++ {
msb.addTimeBucket(buckets[i])
if len(msb.seriesIndex) < f.maxSeriesInBatch {
Expand All @@ -51,7 +51,7 @@ func (f *metricsFlusher) flush(_ context.Context) error {
if err != nil {
return err
}
msb = newMetricSetBuilder(f.referenceID, f.aggregationPeriodInSeconds)
msb = newMetricSetBuilder(f.testRunID, f.aggregationPeriodInSeconds)
}

if len(msb.seriesIndex) < 1 {
Expand Down
2 changes: 1 addition & 1 deletion output/cloud/expv2/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestOutputFlush(t *testing.T) {
// init and start the output
o, err := expv2.New(logger, conf, cc)
require.NoError(t, err)
o.SetReferenceID("my-test-run-id-123")
o.SetTestRunID("my-test-run-id-123")
require.NoError(t, o.Start())

// collect and flush samples
Expand Down
14 changes: 7 additions & 7 deletions output/cloud/expv2/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Output struct {
logger logrus.FieldLogger
config cloudapi.Config
cloudClient *cloudapi.Client
referenceID string
testRunID string

collector *collector
flushing flusher
Expand Down Expand Up @@ -72,9 +72,9 @@ func New(logger logrus.FieldLogger, conf cloudapi.Config, cloudClient *cloudapi.
}, nil
}

// SetReferenceID sets the Cloud's test run ID.
func (o *Output) SetReferenceID(refID string) {
o.referenceID = refID
// SetTestRunID sets the Cloud's test run id.
func (o *Output) SetTestRunID(id string) {
o.testRunID = id
}

// SetTestRunStopCallback receives the function that
Expand All @@ -98,12 +98,12 @@ func (o *Output) Start() error {
return fmt.Errorf("failed to initialize the samples collector: %w", err)
}

mc, err := newMetricsClient(o.cloudClient, o.referenceID)
mc, err := newMetricsClient(o.cloudClient, o.testRunID)
if err != nil {
return fmt.Errorf("failed to initialize the http metrics flush client: %w", err)
}
o.flushing = &metricsFlusher{
referenceID: o.referenceID,
testRunID: o.testRunID,
bq: &o.collector.bq,
client: mc,
aggregationPeriodInSeconds: uint32(o.config.AggregationPeriod.TimeDuration().Seconds()),
Expand All @@ -114,7 +114,7 @@ func (o *Output) Start() error {
o.periodicInvoke(o.config.AggregationPeriod.TimeDuration(), o.collectSamples)

if o.tracingEnabled() {
testRunID, err := strconv.ParseInt(o.referenceID, 10, 64)
testRunID, err := strconv.ParseInt(o.testRunID, 10, 64)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions output/cloud/expv2/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func TestNew(t *testing.T) {
assert.Equal(t, int64(99), o.config.APIVersion.Int64)
}

func TestOutputSetReferenceID(t *testing.T) {
func TestOutputSetTestRunID(t *testing.T) {
t.Parallel()
o := Output{}
o.SetReferenceID("my-test-run-id")
assert.Equal(t, "my-test-run-id", o.referenceID)
o.SetTestRunID("my-test-run-id")
assert.Equal(t, "my-test-run-id", o.testRunID)
}

func TestOutputSetTestRunStopCallback(t *testing.T) {
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestOutputCollectSamples(t *testing.T) {
o, err := New(logger, conf, cc)
require.NoError(t, err)

o.SetReferenceID("ref-id-123")
o.SetTestRunID("ref-id-123")
require.NoError(t, o.Start())
require.Empty(t, o.collector.bq.PopAll())

Expand Down Expand Up @@ -288,7 +288,7 @@ func TestOutputStopWithTestError(t *testing.T) {
o, err := New(logger, config, cc)
require.NoError(t, err)

o.SetReferenceID("ref-id-123")
o.SetTestRunID("ref-id-123")
require.NoError(t, o.Start())
require.NoError(t, o.StopWithTestError(errors.New("an error")))
}
Expand Down
36 changes: 18 additions & 18 deletions output/cloud/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type versionedOutput interface {
StopWithTestError(testRunErr error) error

SetTestRunStopCallback(func(error))
SetReferenceID(id string)
SetTestRunID(id string)

AddMetricSamples(samples []metrics.SampleContainer)
}
Expand All @@ -52,9 +52,9 @@ const (
type Output struct {
versionedOutput

logger logrus.FieldLogger
config cloudapi.Config
referenceID string
logger logrus.FieldLogger
config cloudapi.Config
testRunID string

executionPlan []lib.ExecutionStep
duration int64 // in seconds
Expand Down Expand Up @@ -162,8 +162,8 @@ func validateRequiredSystemTags(scriptTags *metrics.SystemTagSet) error {
// goroutine that would listen for metric samples and send them to the cloud.
func (out *Output) Start() error {
if out.config.PushRefID.Valid {
out.referenceID = out.config.PushRefID.String
out.logger.WithField("referenceId", out.referenceID).Debug("directly pushing metrics without init")
out.testRunID = out.config.PushRefID.String
out.logger.WithField("testRunId", out.testRunID).Debug("Directly pushing metrics without init")
return out.startVersionedOutput()
}

Expand All @@ -186,7 +186,7 @@ func (out *Output) Start() error {
if err != nil {
return err
}
out.referenceID = response.ReferenceID
out.testRunID = response.ReferenceID

if response.ConfigOverride != nil {
out.logger.WithFields(logrus.Fields{
Expand All @@ -201,17 +201,17 @@ func (out *Output) Start() error {
}

out.logger.WithFields(logrus.Fields{
"name": out.config.Name,
"projectId": out.config.ProjectID,
"duration": out.duration,
"referenceId": out.referenceID,
"name": out.config.Name,
"projectId": out.config.ProjectID,
"duration": out.duration,
"testRunId": out.testRunID,
}).Debug("Started!")
return nil
}

// Description returns the URL with the test run results.
func (out *Output) Description() string {
return fmt.Sprintf("cloud (%s)", cloudapi.URLForResults(out.referenceID, out.config))
return fmt.Sprintf("cloud (%s)", cloudapi.URLForResults(out.testRunID, out.config))
}

// SetThresholds receives the thresholds before the output is Start()-ed.
Expand Down Expand Up @@ -257,7 +257,7 @@ func (out *Output) StopWithTestError(testErr error) error {
}

func (out *Output) testFinished(testErr error) error {
if out.referenceID == "" || out.config.PushRefID.Valid {
if out.testRunID == "" || out.config.PushRefID.Valid {
return nil
}

Expand All @@ -275,12 +275,12 @@ func (out *Output) testFinished(testErr error) error {

runStatus := out.getRunStatus(testErr)
out.logger.WithFields(logrus.Fields{
"ref": out.referenceID,
"ref": out.testRunID,
"tainted": testTainted,
"run_status": runStatus,
}).Debug("Sending test finished")

return out.client.TestFinished(out.referenceID, thresholdResults, testTainted, runStatus)
return out.client.TestFinished(out.testRunID, thresholdResults, testTainted, runStatus)
}

// getRunStatus determines the run status of the test based on the error.
Expand Down Expand Up @@ -333,8 +333,8 @@ func (out *Output) getRunStatus(testErr error) cloudapi.RunStatus {
}

func (out *Output) startVersionedOutput() error {
if out.referenceID == "" {
return errors.New("ReferenceID is required")
if out.testRunID == "" {
return errors.New("TestRunID is required")
}
var err error
switch out.config.APIVersion.Int64 {
Expand All @@ -350,7 +350,7 @@ func (out *Output) startVersionedOutput() error {
return err
}

out.versionedOutput.SetReferenceID(out.referenceID)
out.versionedOutput.SetTestRunID(out.testRunID)
out.versionedOutput.SetTestRunStopCallback(out.testStopFunc)
return out.versionedOutput.Start()
}
20 changes: 10 additions & 10 deletions output/cloud/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestOutputStartVersionError(t *testing.T) {
})
require.NoError(t, err)

o.referenceID = "123"
o.testRunID = "123"
err = o.startVersionedOutput()
require.ErrorContains(t, err, "v99 is an unexpected version")
}
Expand All @@ -159,8 +159,8 @@ func TestOutputStartVersionedOutputV2(t *testing.T) {
t.Parallel()

o := Output{
logger: testutils.NewLogger(t),
referenceID: "123",
logger: testutils.NewLogger(t),
testRunID: "123",
config: cloudapi.Config{
APIVersion: null.IntFrom(2),
Host: null.StringFrom("fake-cloud-url"),
Expand All @@ -186,7 +186,7 @@ func TestOutputStartVersionedOutputV1(t *testing.T) {
t.Parallel()

o := Output{
referenceID: "123",
testRunID: "123",
config: cloudapi.Config{
APIVersion: null.IntFrom(1),
// Here, we are enabling but silencing the related async op
Expand All @@ -201,7 +201,7 @@ func TestOutputStartVersionedOutputV1(t *testing.T) {
assert.True(t, ok)
}

func TestOutputStartWithReferenceID(t *testing.T) {
func TestOutputStartWithTestRunID(t *testing.T) {
t.Parallel()

handler := func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -233,13 +233,13 @@ func TestCloudOutputDescription(t *testing.T) {

t.Run("WithTestRunDetails", func(t *testing.T) {
t.Parallel()
o := Output{referenceID: "74"}
o := Output{testRunID: "74"}
o.config.TestRunDetails = null.StringFrom("my-custom-string")
assert.Equal(t, "cloud (my-custom-string)", o.Description())
})
t.Run("WithWebAppURL", func(t *testing.T) {
t.Parallel()
o := Output{referenceID: "74"}
o := Output{testRunID: "74"}
o.config.WebAppURL = null.StringFrom("mywebappurl.com")
assert.Equal(t, "cloud (mywebappurl.com/runs/74)", o.Description())
})
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestOutputStopWithTestError(t *testing.T) {
require.NoError(t, err)

calledStopFn := false
out.referenceID = "test-ref-id-1234"
out.testRunID = "test-ref-id-1234"
out.versionedOutput = versionedOutputMock{
callback: func(fn string) {
if fn == "StopWithTestError" {
Expand Down Expand Up @@ -362,8 +362,8 @@ func (o versionedOutputMock) SetTestRunStopCallback(_ func(error)) {
o.callback("SetTestRunStopCallback")
}

func (o versionedOutputMock) SetReferenceID(id string) {
o.callback("SetReferenceID")
func (o versionedOutputMock) SetTestRunID(id string) {
o.callback("SetTestRunID")
}

func (o versionedOutputMock) AddMetricSamples(samples []metrics.SampleContainer) {
Expand Down
7 changes: 5 additions & 2 deletions output/cloud/v1/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type Output struct {
logger logrus.FieldLogger
config cloudapi.Config

// referenceID is the legacy name used by the Backend for the test run id.
// Note: This output's version is almost deprecated so we don't apply
// the renaming to its internals.
referenceID string
client *MetricsClient

Expand Down Expand Up @@ -65,8 +68,8 @@ func New(logger logrus.FieldLogger, conf cloudapi.Config, testAPIClient *cloudap
}, nil
}

// SetReferenceID sets the passed Reference ID.
func (out *Output) SetReferenceID(id string) {
// SetTestRunID sets the passed test run id.
func (out *Output) SetTestRunID(id string) {
out.referenceID = id
imiric marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
10 changes: 5 additions & 5 deletions output/cloud/v1/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func runCloudOutputTestCase(t *testing.T, minSamples int) {
})
require.NoError(t, err)

out.SetReferenceID("123")
out.SetTestRunID("123")
require.NoError(t, out.Start())

now := time.Now()
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestCloudOutputMaxPerPacket(t *testing.T) {
ScriptPath: &url.URL{Path: "/script.js"},
})
require.NoError(t, err)
out.SetReferenceID("12")
out.SetTestRunID("12")

maxMetricSamplesPerPackage := 20
out.config.MaxMetricSamplesPerPackage = null.IntFrom(int64(maxMetricSamplesPerPackage))
Expand Down Expand Up @@ -421,7 +421,7 @@ func testCloudOutputStopSendingMetric(t *testing.T, stopOnError bool) {
assert.NoError(t, json.Unmarshal(body, &receivedSamples))
})

out.SetReferenceID("12")
out.SetTestRunID("12")
require.NoError(t, out.Start())

out.AddMetricSamples([]metrics.SampleContainer{metrics.Sample{
Expand Down Expand Up @@ -512,7 +512,7 @@ func TestCloudOutputPushRefID(t *testing.T) {
})
require.NoError(t, err)

out.SetReferenceID("333")
out.SetTestRunID("333")
require.NoError(t, out.Start())

now := time.Now()
Expand Down Expand Up @@ -594,7 +594,7 @@ func TestCloudOutputRecvIterLIAllIterations(t *testing.T) {
m.Unlock()
})

out.SetReferenceID("123")
out.SetTestRunID("123")
require.NoError(t, out.Start())

now := time.Now()
Expand Down