From d18fc9e6950492e6002e14b0e9c3a798566d6f8c Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Mon, 26 Jun 2023 11:43:24 +0200 Subject: [PATCH 1/6] output/cloudv2: Rename referenceID to testRunID It represents a testRunID so why not directly call it in that way?! --- output/cloud/expv2/flush.go | 6 +++--- output/cloud/expv2/output.go | 10 +++++----- output/cloud/expv2/output_test.go | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/output/cloud/expv2/flush.go b/output/cloud/expv2/flush.go index 69aa46b270e..4722c2b55b6 100644 --- a/output/cloud/expv2/flush.go +++ b/output/cloud/expv2/flush.go @@ -13,7 +13,7 @@ type pusher interface { } type metricsFlusher struct { - referenceID string + testRunID string bq *bucketQ client pusher aggregationPeriodInSeconds uint32 @@ -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 { @@ -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 { diff --git a/output/cloud/expv2/output.go b/output/cloud/expv2/output.go index 58ff778d110..62b2f179e76 100644 --- a/output/cloud/expv2/output.go +++ b/output/cloud/expv2/output.go @@ -40,7 +40,7 @@ type Output struct { logger logrus.FieldLogger config cloudapi.Config cloudClient *cloudapi.Client - referenceID string + testRunID string collector *collector flushing flusher @@ -74,7 +74,7 @@ func New(logger logrus.FieldLogger, conf cloudapi.Config, cloudClient *cloudapi. // SetReferenceID sets the Cloud's test run ID. func (o *Output) SetReferenceID(refID string) { - o.referenceID = refID + o.testRunID = refID } // SetTestRunStopCallback receives the function that @@ -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()), @@ -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 } diff --git a/output/cloud/expv2/output_test.go b/output/cloud/expv2/output_test.go index f440da9dc8c..99eaf9d3eed 100644 --- a/output/cloud/expv2/output_test.go +++ b/output/cloud/expv2/output_test.go @@ -42,7 +42,7 @@ func TestOutputSetReferenceID(t *testing.T) { t.Parallel() o := Output{} o.SetReferenceID("my-test-run-id") - assert.Equal(t, "my-test-run-id", o.referenceID) + assert.Equal(t, "my-test-run-id", o.testRunID) } func TestOutputSetTestRunStopCallback(t *testing.T) { From 1556fab9e675c0bb23c3e29b1005d2601196fd09 Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Mon, 26 Jun 2023 12:20:58 +0200 Subject: [PATCH 2/6] cloud/output: Renamed SetReferenceID to SetTestRunID --- output/cloud/expv2/integration/integration_test.go | 2 +- output/cloud/expv2/output.go | 6 +++--- output/cloud/expv2/output_test.go | 6 +++--- output/cloud/output.go | 4 ++-- output/cloud/output_test.go | 4 ++-- output/cloud/v1/output.go | 4 ++-- output/cloud/v1/output_test.go | 10 +++++----- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/output/cloud/expv2/integration/integration_test.go b/output/cloud/expv2/integration/integration_test.go index 496ff2924b7..15adfd66dbc 100644 --- a/output/cloud/expv2/integration/integration_test.go +++ b/output/cloud/expv2/integration/integration_test.go @@ -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 diff --git a/output/cloud/expv2/output.go b/output/cloud/expv2/output.go index 62b2f179e76..f911da1ceb4 100644 --- a/output/cloud/expv2/output.go +++ b/output/cloud/expv2/output.go @@ -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.testRunID = refID +// SetTestRunID sets the Cloud's test run id. +func (o *Output) SetTestRunID(id string) { + o.testRunID = id } // SetTestRunStopCallback receives the function that diff --git a/output/cloud/expv2/output_test.go b/output/cloud/expv2/output_test.go index 99eaf9d3eed..ba65445f2d6 100644 --- a/output/cloud/expv2/output_test.go +++ b/output/cloud/expv2/output_test.go @@ -41,7 +41,7 @@ func TestNew(t *testing.T) { func TestOutputSetReferenceID(t *testing.T) { t.Parallel() o := Output{} - o.SetReferenceID("my-test-run-id") + o.SetTestRunID("my-test-run-id") assert.Equal(t, "my-test-run-id", o.testRunID) } @@ -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()) @@ -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"))) } diff --git a/output/cloud/output.go b/output/cloud/output.go index f66424e5ca4..f644f5891b7 100644 --- a/output/cloud/output.go +++ b/output/cloud/output.go @@ -35,7 +35,7 @@ type versionedOutput interface { StopWithTestError(testRunErr error) error SetTestRunStopCallback(func(error)) - SetReferenceID(id string) + SetTestRunID(id string) AddMetricSamples(samples []metrics.SampleContainer) } @@ -350,7 +350,7 @@ func (out *Output) startVersionedOutput() error { return err } - out.versionedOutput.SetReferenceID(out.referenceID) + out.versionedOutput.SetTestRunID(out.referenceID) out.versionedOutput.SetTestRunStopCallback(out.testStopFunc) return out.versionedOutput.Start() } diff --git a/output/cloud/output_test.go b/output/cloud/output_test.go index 80c8f5c437a..d154580ad49 100644 --- a/output/cloud/output_test.go +++ b/output/cloud/output_test.go @@ -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) { diff --git a/output/cloud/v1/output.go b/output/cloud/v1/output.go index 6f295a656b6..fb80b5d20db 100644 --- a/output/cloud/v1/output.go +++ b/output/cloud/v1/output.go @@ -65,8 +65,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 } diff --git a/output/cloud/v1/output_test.go b/output/cloud/v1/output_test.go index 7d312c1ca5a..0d4e26a9259 100644 --- a/output/cloud/v1/output_test.go +++ b/output/cloud/v1/output_test.go @@ -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() @@ -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)) @@ -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{ @@ -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() @@ -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() From 8a5c696538f57490f520ea791394b8c5a6cff022 Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Mon, 26 Jun 2023 12:25:34 +0200 Subject: [PATCH 3/6] cloud/output: Renamed field referenceID to testRunID --- output/cloud/output.go | 32 ++++++++++++++++---------------- output/cloud/output_test.go | 14 +++++++------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/output/cloud/output.go b/output/cloud/output.go index f644f5891b7..3dc3a4e75c6 100644 --- a/output/cloud/output.go +++ b/output/cloud/output.go @@ -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 @@ -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() } @@ -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{ @@ -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. @@ -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 } @@ -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. @@ -333,7 +333,7 @@ func (out *Output) getRunStatus(testErr error) cloudapi.RunStatus { } func (out *Output) startVersionedOutput() error { - if out.referenceID == "" { + if out.testRunID == "" { return errors.New("ReferenceID is required") } var err error @@ -350,7 +350,7 @@ func (out *Output) startVersionedOutput() error { return err } - out.versionedOutput.SetTestRunID(out.referenceID) + out.versionedOutput.SetTestRunID(out.testRunID) out.versionedOutput.SetTestRunStopCallback(out.testStopFunc) return out.versionedOutput.Start() } diff --git a/output/cloud/output_test.go b/output/cloud/output_test.go index d154580ad49..ec465dd3859 100644 --- a/output/cloud/output_test.go +++ b/output/cloud/output_test.go @@ -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") } @@ -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"), @@ -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 @@ -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()) }) @@ -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" { From 1a6ea3c8a0d78930908170b5a29663f096b991cf Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Mon, 26 Jun 2023 16:59:24 +0200 Subject: [PATCH 4/6] Document why not renaming in v1 --- output/cloud/v1/output.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/output/cloud/v1/output.go b/output/cloud/v1/output.go index fb80b5d20db..a8554c0f3eb 100644 --- a/output/cloud/v1/output.go +++ b/output/cloud/v1/output.go @@ -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 From a53009676bd21834981af3db4167eda9ed57a70d Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Mon, 26 Jun 2023 17:10:16 +0200 Subject: [PATCH 5/6] cloudapi additional documentation --- cloudapi/config.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cloudapi/config.go b/cloudapi/config.go index 336924e7632..8ce727516a3 100644 --- a/cloudapi/config.go +++ b/cloudapi/config.go @@ -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. @@ -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"` @@ -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"` From a6f5a77ccf3c7bb950cce7e5ee726c0a0e1ed23b Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Wed, 5 Jul 2023 14:31:05 +0200 Subject: [PATCH 6/6] Renamed some missed text --- output/cloud/expv2/output_test.go | 2 +- output/cloud/output.go | 4 ++-- output/cloud/output_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/output/cloud/expv2/output_test.go b/output/cloud/expv2/output_test.go index ba65445f2d6..11e50eb55e9 100644 --- a/output/cloud/expv2/output_test.go +++ b/output/cloud/expv2/output_test.go @@ -38,7 +38,7 @@ 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.SetTestRunID("my-test-run-id") diff --git a/output/cloud/output.go b/output/cloud/output.go index 3dc3a4e75c6..8ed43d03fcb 100644 --- a/output/cloud/output.go +++ b/output/cloud/output.go @@ -163,7 +163,7 @@ func validateRequiredSystemTags(scriptTags *metrics.SystemTagSet) error { func (out *Output) Start() error { if out.config.PushRefID.Valid { out.testRunID = out.config.PushRefID.String - out.logger.WithField("testRunID", out.testRunID).Debug("directly pushing metrics without init") + out.logger.WithField("testRunId", out.testRunID).Debug("Directly pushing metrics without init") return out.startVersionedOutput() } @@ -334,7 +334,7 @@ func (out *Output) getRunStatus(testErr error) cloudapi.RunStatus { func (out *Output) startVersionedOutput() error { if out.testRunID == "" { - return errors.New("ReferenceID is required") + return errors.New("TestRunID is required") } var err error switch out.config.APIVersion.Int64 { diff --git a/output/cloud/output_test.go b/output/cloud/output_test.go index ec465dd3859..a649ddb0b0f 100644 --- a/output/cloud/output_test.go +++ b/output/cloud/output_test.go @@ -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) {