Skip to content

Commit

Permalink
k6runner: add check metadata and type to remote runner requests
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre committed Oct 8, 2024
1 parent 8df75c2 commit 4a879be
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
37 changes: 35 additions & 2 deletions internal/k6runner/k6runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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")

Expand Down
25 changes: 25 additions & 0 deletions internal/k6runner/k6runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4a879be

Please sign in to comment.