From b9307ff8f98f7d7459c3e0bc0c95feb8780d46ab Mon Sep 17 00:00:00 2001 From: Nadia Santalla Date: Mon, 7 Oct 2024 11:48:17 +0200 Subject: [PATCH] k6runner: add check metadata and type to remote runner requests --- internal/k6runner/k6runner.go | 37 ++++++++++++++++++++++++++++-- internal/k6runner/k6runner_test.go | 25 ++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/internal/k6runner/k6runner.go b/internal/k6runner/k6runner.go index 0ec4c222..b3d98398 100644 --- a/internal/k6runner/k6runner.go +++ b/internal/k6runner/k6runner.go @@ -10,6 +10,7 @@ import ( "github.com/go-logfmt/logfmt" "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" + sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/expfmt" @@ -20,11 +21,18 @@ import ( // Script is a k6 script that a runner is able to run, with some added instructions for that runner to act on. type Script struct { - Script []byte `json:"script"` + // Script is the blob of bytes that is to be run. + Script []byte `json:"script"` + // Settings is a common representation of the fields common to all implementation-specific check settings that the + // runners are interested about. Settings Settings `json:"settings"` - // TODO: Add Metadata and Features. + // Check holds information about the SM check that triggered this script. + Check CheckInfo `json:"check"` + // TODO: Add features. } +// Settings is a common representation of the fields common to all implementation-specific check settings that the +// runners are interested about. type Settings struct { // Timeout for k6 run, in milliseconds. This value is a configuration value for remote runners, which will instruct // them to return an error if the operation takes longer than this time to complete. Clients should expect that @@ -33,6 +41,31 @@ type Settings struct { Timeout int64 `json:"timeout"` } +// CheckInfo holds information about the SM check that triggered this script. +type CheckInfo struct { + // Type is the string representation of the check type this script belongs to (browser, scripted, multihttp, etc.) + Type string `json:"type"` + // Metadata is a collection of key/value pairs containing information about this check, such as check and tenant ID. + // It is loosely typed on purpose: Metadata should only be used for informational properties that will make its way + // into telemetry, and not for making decision on it. + Metadata map[string]any `json:"metadata"` +} + +// FromSM populates the given Check from the information of the SM check. Existing fields are overwritten. +func (c *CheckInfo) FromSM(smc sm.Check) *CheckInfo { + c.Type = smc.Type().String() + + if c.Metadata == nil { + c.Metadata = map[string]any{} + } + c.Metadata["id"] = smc.Id + c.Metadata["tenantID"] = smc.TenantId + c.Metadata["created"] = smc.Created + c.Metadata["modified"] = smc.Modified + + return c +} + // ErrNoTimeout is returned by [Runner] implementations if the supplied script has a timeout of zero. var ErrNoTimeout = errors.New("check has no timeout") diff --git a/internal/k6runner/k6runner_test.go b/internal/k6runner/k6runner_test.go index e5b1c613..4a6ed44d 100644 --- a/internal/k6runner/k6runner_test.go +++ b/internal/k6runner/k6runner_test.go @@ -12,6 +12,7 @@ import ( "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" "github.com/grafana/synthetic-monitoring-agent/internal/testhelper" + sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/expfmt" @@ -93,6 +94,30 @@ func TestScriptRun(t *testing.T) { require.True(t, success) } +func TestCheckInfoFromSM(t *testing.T) { + t.Parallel() + + check := sm.Check{ + Id: 69, + TenantId: 1234, + Created: 1234.5, + Modified: 12345.6, + Settings: sm.CheckSettings{ + Browser: &sm.BrowserSettings{}, // Make it non-nil so type is Browser. + }, + } + + ci := (&CheckInfo{}).FromSM(check) + + require.Equal(t, sm.CheckTypeBrowser.String(), ci.Type) + require.Equal(t, map[string]any{ + "id": check.Id, + "tenantID": check.TenantId, + "created": check.Created, + "modified": check.Modified, + }, ci.Metadata) +} + type testRunner struct { metrics []byte logs []byte