diff --git a/internal/k6runner/k6runner.go b/internal/k6runner/k6runner.go index 0ec4c222..2346ec84 100644 --- a/internal/k6runner/k6runner.go +++ b/internal/k6runner/k6runner.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/go-logfmt/logfmt" + smmmodel "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" @@ -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. + // CheckInfo holds information about the SM check that triggered this script. + CheckInfo 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,32 @@ 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"` +} + +// CheckInfoFromSM returns a CheckInfo from the information of the given SM check. +func CheckInfoFromSM(smc smmmodel.Check) CheckInfo { + ci := CheckInfo{ + Metadata: map[string]any{}, + } + + ci.Type = smc.Type().String() + ci.Metadata["id"] = smc.Id + ci.Metadata["tenantID"] = smc.TenantId + ci.Metadata["regionID"] = smc.RegionId + ci.Metadata["created"] = smc.Created + ci.Metadata["modified"] = smc.Modified + + return ci +} + // 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..8c70edff 100644 --- a/internal/k6runner/k6runner_test.go +++ b/internal/k6runner/k6runner_test.go @@ -10,8 +10,10 @@ import ( "strings" "testing" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "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 +95,34 @@ func TestScriptRun(t *testing.T) { require.True(t, success) } +func TestCheckInfoFromSM(t *testing.T) { + t.Parallel() + + check := model.Check{ + RegionId: 4, + 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 := CheckInfoFromSM(check) + + require.Equal(t, sm.CheckTypeBrowser.String(), ci.Type) + require.Equal(t, map[string]any{ + "id": check.Id, + "tenantID": check.TenantId, + "regionID": check.RegionId, + "created": check.Created, + "modified": check.Modified, + }, ci.Metadata) +} + type testRunner struct { metrics []byte logs []byte diff --git a/internal/prober/browser/browser.go b/internal/prober/browser/browser.go index bff751ea..c9f188f5 100644 --- a/internal/prober/browser/browser.go +++ b/internal/prober/browser/browser.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/grafana/synthetic-monitoring-agent/internal/k6runner" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "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" @@ -26,7 +27,7 @@ type Prober struct { processor *k6runner.Processor } -func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, runner k6runner.Runner) (Prober, error) { +func NewProber(ctx context.Context, check model.Check, logger zerolog.Logger, runner k6runner.Runner) (Prober, error) { var p Prober if check.Settings.Browser == nil { @@ -40,7 +41,7 @@ func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, runne Settings: k6runner.Settings{ Timeout: check.Timeout, }, - // TODO: Add metadata & features here. + CheckInfo: k6runner.CheckInfoFromSM(check), }, } diff --git a/internal/prober/browser/browser_test.go b/internal/prober/browser/browser_test.go index 101bbee5..217b6cda 100644 --- a/internal/prober/browser/browser_test.go +++ b/internal/prober/browser/browser_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/grafana/synthetic-monitoring-agent/internal/k6runner" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/testhelper" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/rs/zerolog" @@ -19,33 +20,37 @@ func TestNewProber(t *testing.T) { logger := zerolog.New(zerolog.NewTestWriter(t)) testcases := map[string]struct { - check sm.Check + check model.Check expectFailure bool }{ "valid": { expectFailure: false, - check: sm.Check{ - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: sm.CheckSettings{ - Browser: &sm.BrowserSettings{ - Script: []byte("// test"), + check: model.Check{ + Check: sm.Check{ + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{ + Browser: &sm.BrowserSettings{ + Script: []byte("// test"), + }, }, }, }, }, "invalid": { expectFailure: true, - check: sm.Check{ - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: sm.CheckSettings{}, + check: model.Check{ + Check: sm.Check{ + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{}, + }, }, }, } diff --git a/internal/prober/dns/dns.go b/internal/prober/dns/dns.go index 55388614..4200972d 100644 --- a/internal/prober/dns/dns.go +++ b/internal/prober/dns/dns.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/dns/internal/bbe/config" bbeprober "github.com/grafana/synthetic-monitoring-agent/internal/prober/dns/internal/bbe/prober" "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" @@ -21,7 +22,7 @@ type Prober struct { experimental bool } -func NewProber(check sm.Check) (Prober, error) { +func NewProber(check model.Check) (Prober, error) { if check.Settings.Dns == nil { return Prober{}, errUnsupportedCheck } @@ -35,7 +36,7 @@ func NewProber(check sm.Check) (Prober, error) { }, nil } -func NewExperimentalProber(check sm.Check) (Prober, error) { +func NewExperimentalProber(check model.Check) (Prober, error) { p, err := NewProber(check) if err != nil { return p, err diff --git a/internal/prober/dns/dns_test.go b/internal/prober/dns/dns_test.go index 0fb4dcc5..cb936a2a 100644 --- a/internal/prober/dns/dns_test.go +++ b/internal/prober/dns/dns_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/go-kit/log" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/dns/internal/bbe/config" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/miekg/dns" @@ -27,17 +28,17 @@ func TestName(t *testing.T) { func TestNewProber(t *testing.T) { testcases := map[string]struct { - input sm.Check + input model.Check expected Prober ExpectError bool }{ "default": { - input: sm.Check{ + input: model.Check{Check: sm.Check{ Target: "www.grafana.com", Settings: sm.CheckSettings{ Dns: &sm.DnsSettings{}, }, - }, + }}, expected: Prober{ config: config.Module{ Prober: "dns", @@ -55,10 +56,12 @@ func TestNewProber(t *testing.T) { ExpectError: false, }, "no-settings": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Dns: nil, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Dns: nil, + }, }, }, expected: Prober{}, @@ -221,15 +224,17 @@ func TestProberRetries(t *testing.T) { } }() - p, err := NewExperimentalProber(sm.Check{ - Target: "www.grafana.com", - Timeout: 20000, - Settings: sm.CheckSettings{ - Dns: &sm.DnsSettings{ - Server: l.LocalAddr().String(), - RecordType: sm.DnsRecordType_A, - Protocol: sm.DnsProtocol_UDP, - IpVersion: sm.IpVersion_V4, + p, err := NewExperimentalProber(model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Timeout: 20000, + Settings: sm.CheckSettings{ + Dns: &sm.DnsSettings{ + Server: l.LocalAddr().String(), + RecordType: sm.DnsRecordType_A, + Protocol: sm.DnsProtocol_UDP, + IpVersion: sm.IpVersion_V4, + }, }, }, }) diff --git a/internal/prober/grpc/grpc.go b/internal/prober/grpc/grpc.go index a38831dc..9fb437d5 100644 --- a/internal/prober/grpc/grpc.go +++ b/internal/prober/grpc/grpc.go @@ -5,6 +5,7 @@ import ( "errors" "time" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" "github.com/grafana/synthetic-monitoring-agent/internal/tls" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" @@ -20,7 +21,7 @@ type Prober struct { config config.Module } -func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger) (Prober, error) { +func NewProber(ctx context.Context, check model.Check, logger zerolog.Logger) (Prober, error) { if check.Settings.Grpc == nil { return Prober{}, errUnsupportedCheck } diff --git a/internal/prober/grpc/grpc_test.go b/internal/prober/grpc/grpc_test.go index 4afad253..fef5e0ee 100644 --- a/internal/prober/grpc/grpc_test.go +++ b/internal/prober/grpc/grpc_test.go @@ -5,6 +5,7 @@ import ( "io" "testing" + "github.com/grafana/synthetic-monitoring-agent/internal/model" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/prometheus/blackbox_exporter/config" promcfg "github.com/prometheus/common/config" @@ -19,15 +20,17 @@ func TestName(t *testing.T) { func TestNewProber(t *testing.T) { testcases := map[string]struct { - input sm.Check + input model.Check expected Prober ExpectError bool }{ "default": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Grpc: &sm.GrpcSettings{}, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Grpc: &sm.GrpcSettings{}, + }, }, }, expected: Prober{ @@ -42,10 +45,12 @@ func TestNewProber(t *testing.T) { }, }, "no-settings": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Grpc: nil, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Grpc: nil, + }, }, }, ExpectError: true, diff --git a/internal/prober/http/http.go b/internal/prober/http/http.go index 320a90c2..09671708 100644 --- a/internal/prober/http/http.go +++ b/internal/prober/http/http.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" "github.com/grafana/synthetic-monitoring-agent/internal/tls" "github.com/grafana/synthetic-monitoring-agent/internal/version" @@ -29,13 +30,13 @@ type Prober struct { cacheBustingQueryParamName string } -func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, reservedHeaders http.Header) (Prober, error) { +func NewProber(ctx context.Context, check model.Check, logger zerolog.Logger, reservedHeaders http.Header) (Prober, error) { if check.Settings.Http == nil { return Prober{}, errUnsupportedCheck } if len(reservedHeaders) > 0 { - augmentHttpHeaders(&check, reservedHeaders) + augmentHttpHeaders(&check.Check, reservedHeaders) } cfg, err := settingsToModule(ctx, check.Settings.Http, logger) diff --git a/internal/prober/http/http_test.go b/internal/prober/http/http_test.go index b168ed01..10e774e8 100644 --- a/internal/prober/http/http_test.go +++ b/internal/prober/http/http_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/go-kit/log" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/http/testserver" "github.com/grafana/synthetic-monitoring-agent/internal/version" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" @@ -28,12 +29,12 @@ func TestName(t *testing.T) { func TestNewProber(t *testing.T) { testcases := map[string]struct { - input sm.Check + input model.Check expected Prober ExpectError bool }{ "default": { - input: sm.Check{ + input: model.Check{Check: sm.Check{ Id: 3, Target: "www.grafana.com", Settings: sm.CheckSettings{ @@ -43,7 +44,7 @@ func TestNewProber(t *testing.T) { }, }, }, - }, + }}, expected: Prober{ config: getDefaultModule(). addHttpHeader("X-Sm-Id", "3-3"). // is checkId twice since probeId is unavailable here @@ -52,26 +53,28 @@ func TestNewProber(t *testing.T) { ExpectError: false, }, "no-settings": { - input: sm.Check{ + input: model.Check{Check: sm.Check{ Id: 1, Target: "www.grafana.com", Settings: sm.CheckSettings{ Http: nil, }, - }, + }}, expected: Prober{}, ExpectError: true, }, "headers": { - input: sm.Check{ - Id: 5, - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Http: &sm.HttpSettings{ - Headers: []string{ - "uSeR-aGeNt: test-user-agent", - "some-header: some-value", - "x-SM-iD: 3232-32", + input: model.Check{ + Check: sm.Check{ + Id: 5, + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Http: &sm.HttpSettings{ + Headers: []string{ + "uSeR-aGeNt: test-user-agent", + "some-header: some-value", + "x-SM-iD: 3232-32", + }, }, }, }, @@ -207,19 +210,21 @@ func TestProbe(t *testing.T) { for name, tc := range testcases { t.Run(name, func(t *testing.T) { target := tc.srvSettings.URL(srv.Listener.Addr().String()) - check := sm.Check{ - Id: 1, - TenantId: 1, - Frequency: 10000, - Timeout: 1000, - Enabled: true, - Settings: sm.CheckSettings{ - Http: &tc.settings, + check := model.Check{ + Check: sm.Check{ + Id: 1, + TenantId: 1, + Frequency: 10000, + Timeout: 1000, + Enabled: true, + Settings: sm.CheckSettings{ + Http: &tc.settings, + }, + Probes: []int64{1}, + Target: target, + Job: "test", + BasicMetricsOnly: true, }, - Probes: []int64{1}, - Target: target, - Job: "test", - BasicMetricsOnly: true, } t.Log(check.Target) diff --git a/internal/prober/icmp/icmp.go b/internal/prober/icmp/icmp.go index 62142711..df0ffecb 100644 --- a/internal/prober/icmp/icmp.go +++ b/internal/prober/icmp/icmp.go @@ -7,6 +7,7 @@ import ( "time" "github.com/go-kit/log" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/prometheus/blackbox_exporter/config" @@ -29,7 +30,7 @@ type Prober struct { config Module } -func NewProber(check sm.Check) (Prober, error) { +func NewProber(check model.Check) (Prober, error) { var p Prober if check.Settings.Ping == nil { diff --git a/internal/prober/icmp/icmp_test.go b/internal/prober/icmp/icmp_test.go index ff0e7253..43d5a5a7 100644 --- a/internal/prober/icmp/icmp_test.go +++ b/internal/prober/icmp/icmp_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/go-kit/log" + "github.com/grafana/synthetic-monitoring-agent/internal/model" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/prometheus/blackbox_exporter/config" bbeprober "github.com/prometheus/blackbox_exporter/prober" @@ -21,15 +22,17 @@ func TestName(t *testing.T) { func TestNewProber(t *testing.T) { testcases := map[string]struct { - input sm.Check + input model.Check expected Prober ExpectError bool }{ "default": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Ping: &sm.PingSettings{}, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Ping: &sm.PingSettings{}, + }, }, }, expected: Prober{ @@ -49,11 +52,13 @@ func TestNewProber(t *testing.T) { ExpectError: false, }, "1 packet": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Ping: &sm.PingSettings{ - PacketCount: 1, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Ping: &sm.PingSettings{ + PacketCount: 1, + }, }, }, }, @@ -74,11 +79,13 @@ func TestNewProber(t *testing.T) { ExpectError: false, }, "2 packets": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Ping: &sm.PingSettings{ - PacketCount: 2, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Ping: &sm.PingSettings{ + PacketCount: 2, + }, }, }, }, @@ -99,10 +106,12 @@ func TestNewProber(t *testing.T) { ExpectError: false, }, "no-settings": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Ping: nil, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Ping: nil, + }, }, }, expected: Prober{}, @@ -213,13 +222,15 @@ func TestProber(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), deadline) defer cancel() - prober, err := NewProber(sm.Check{ - Target: "127.0.0.1", - Timeout: 1000, - Settings: sm.CheckSettings{ - Ping: &sm.PingSettings{ - IpVersion: sm.IpVersion_V4, - PacketCount: 1, + prober, err := NewProber(model.Check{ + Check: sm.Check{ + Target: "127.0.0.1", + Timeout: 1000, + Settings: sm.CheckSettings{ + Ping: &sm.PingSettings{ + IpVersion: sm.IpVersion_V4, + PacketCount: 1, + }, }, }, }) diff --git a/internal/prober/multihttp/multihttp.go b/internal/prober/multihttp/multihttp.go index 93970f7e..f762a278 100644 --- a/internal/prober/multihttp/multihttp.go +++ b/internal/prober/multihttp/multihttp.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/grafana/synthetic-monitoring-agent/internal/k6runner" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "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" @@ -28,7 +29,7 @@ type Prober struct { processor *k6runner.Processor } -func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, runner k6runner.Runner, reservedHeaders http.Header) (Prober, error) { +func NewProber(ctx context.Context, check model.Check, logger zerolog.Logger, runner k6runner.Runner, reservedHeaders http.Header) (Prober, error) { var p Prober if check.Settings.Multihttp == nil { @@ -40,7 +41,7 @@ func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, runne } if len(reservedHeaders) > 0 { - augmentHttpHeaders(&check, reservedHeaders) + augmentHttpHeaders(&check.Check, reservedHeaders) } script, err := settingsToScript(check.Settings.Multihttp) @@ -55,7 +56,7 @@ func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, runne Settings: k6runner.Settings{ Timeout: check.Timeout, }, - // TODO: Add metadata & features here. + CheckInfo: k6runner.CheckInfoFromSM(check), }, } diff --git a/internal/prober/multihttp/multihttp_test.go b/internal/prober/multihttp/multihttp_test.go index a3cdaf2f..7af5d0cc 100644 --- a/internal/prober/multihttp/multihttp_test.go +++ b/internal/prober/multihttp/multihttp_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/grafana/synthetic-monitoring-agent/internal/k6runner" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/testhelper" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/rs/zerolog" @@ -21,28 +22,30 @@ func TestNewProber(t *testing.T) { logger := zerolog.New(zerolog.NewTestWriter(t)) testcases := map[string]struct { - check sm.Check + check model.Check expectFailure bool }{ "valid": { expectFailure: false, - check: sm.Check{ - Id: 1, - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: sm.CheckSettings{ - Multihttp: &sm.MultiHttpSettings{ - Entries: []*sm.MultiHttpEntry{ - { - Request: &sm.MultiHttpEntryRequest{ - Url: "http://www.example.org", - QueryFields: []*sm.QueryField{ - { - Name: "q", - Value: "${v}", + check: model.Check{ + Check: sm.Check{ + Id: 1, + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{ + Multihttp: &sm.MultiHttpSettings{ + Entries: []*sm.MultiHttpEntry{ + { + Request: &sm.MultiHttpEntryRequest{ + Url: "http://www.example.org", + QueryFields: []*sm.QueryField{ + { + Name: "q", + Value: "${v}", + }, }, }, }, @@ -54,18 +57,20 @@ func TestNewProber(t *testing.T) { }, "settings must be valid": { expectFailure: true, - check: sm.Check{ - Id: 2, - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: sm.CheckSettings{ - Multihttp: &sm.MultiHttpSettings{ - Entries: []*sm.MultiHttpEntry{ - // This is invalid because the requsest does not have a URL - {}, + check: model.Check{ + Check: sm.Check{ + Id: 2, + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{ + Multihttp: &sm.MultiHttpSettings{ + Entries: []*sm.MultiHttpEntry{ + // This is invalid because the requsest does not have a URL + {}, + }, }, }, }, @@ -73,35 +78,39 @@ func TestNewProber(t *testing.T) { }, "must contain multihttp settings": { expectFailure: true, - check: sm.Check{ - Id: 3, - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: sm.CheckSettings{ - // The settings are valid for ping, but not for multihttp - Ping: &sm.PingSettings{}, + check: model.Check{ + Check: sm.Check{ + Id: 3, + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{ + // The settings are valid for ping, but not for multihttp + Ping: &sm.PingSettings{}, + }, }, }, }, "header overwrite protection is case-insensitive": { expectFailure: false, - check: sm.Check{ - Id: 4, - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: sm.CheckSettings{ - Multihttp: &sm.MultiHttpSettings{ - Entries: []*sm.MultiHttpEntry{ - { - Request: &sm.MultiHttpEntryRequest{ - Url: "http://www.example.org", - Headers: []*sm.HttpHeader{{Name: "X-sM-Id", Value: "9880-9880"}}, + check: model.Check{ + Check: sm.Check{ + Id: 4, + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{ + Multihttp: &sm.MultiHttpSettings{ + Entries: []*sm.MultiHttpEntry{ + { + Request: &sm.MultiHttpEntryRequest{ + Url: "http://www.example.org", + Headers: []*sm.HttpHeader{{Name: "X-sM-Id", Value: "9880-9880"}}, + }, }, }, }, diff --git a/internal/prober/multihttp/script_test.go b/internal/prober/multihttp/script_test.go index 9dbe5618..d8fa7a9c 100644 --- a/internal/prober/multihttp/script_test.go +++ b/internal/prober/multihttp/script_test.go @@ -12,6 +12,7 @@ import ( kitlog "github.com/go-kit/kit/log" //nolint:staticcheck // TODO(mem): replace in BBE "github.com/go-kit/log/level" "github.com/grafana/synthetic-monitoring-agent/internal/k6runner" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/testhelper" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/mccutchen/go-httpbin/v2/httpbin" @@ -836,12 +837,14 @@ func TestSettingsToScript(t *testing.T) { require.NoError(t, err) require.NotEmpty(t, actual) - check := sm.Check{ - Target: settings.Entries[0].Request.Url, - Job: "test", - Timeout: 10000, - Settings: sm.CheckSettings{ - Multihttp: settings, + check := model.Check{ + Check: sm.Check{ + Target: settings.Entries[0].Request.Url, + Job: "test", + Timeout: 10000, + Settings: sm.CheckSettings{ + Multihttp: settings, + }, }, } diff --git a/internal/prober/prober.go b/internal/prober/prober.go index eaed05b9..d9470965 100644 --- a/internal/prober/prober.go +++ b/internal/prober/prober.go @@ -62,33 +62,33 @@ func (f proberFactory) New(ctx context.Context, logger zerolog.Logger, check mod switch checkType := check.Type(); checkType { case sm.CheckTypePing: - p, err = icmp.NewProber(check.Check) + p, err = icmp.NewProber(check) target = check.Target case sm.CheckTypeHttp: reservedHeaders := f.getReservedHeaders(&check) - p, err = httpProber.NewProber(ctx, check.Check, logger, reservedHeaders) + p, err = httpProber.NewProber(ctx, check, logger, reservedHeaders) target = check.Target case sm.CheckTypeDns: if f.features.IsSet(feature.ExperimentalDnsProber) { - p, err = dns.NewExperimentalProber(check.Check) + p, err = dns.NewExperimentalProber(check) } else { - p, err = dns.NewProber(check.Check) + p, err = dns.NewProber(check) } target = check.Settings.Dns.Server case sm.CheckTypeTcp: - p, err = tcp.NewProber(ctx, check.Check, logger) + p, err = tcp.NewProber(ctx, check, logger) target = check.Target case sm.CheckTypeTraceroute: - p, err = traceroute.NewProber(check.Check, logger) + p, err = traceroute.NewProber(check, logger) target = check.Target case sm.CheckTypeScripted: if f.runner != nil { - p, err = scripted.NewProber(ctx, check.Check, logger, f.runner) + p, err = scripted.NewProber(ctx, check, logger, f.runner) target = check.Target } else { err = fmt.Errorf("k6 checks are not enabled") @@ -99,7 +99,7 @@ func (f proberFactory) New(ctx context.Context, logger zerolog.Logger, check mod // we know that the runner is actually able to handle browser // checks. if f.runner != nil { - p, err = browser.NewProber(ctx, check.Check, logger, f.runner) + p, err = browser.NewProber(ctx, check, logger, f.runner) target = check.Target } else { err = fmt.Errorf("k6 checks are not enabled") @@ -108,14 +108,14 @@ func (f proberFactory) New(ctx context.Context, logger zerolog.Logger, check mod case sm.CheckTypeMultiHttp: if f.runner != nil { reservedHeaders := f.getReservedHeaders(&check) - p, err = multihttp.NewProber(ctx, check.Check, logger, f.runner, reservedHeaders) + p, err = multihttp.NewProber(ctx, check, logger, f.runner, reservedHeaders) target = check.Target } else { err = fmt.Errorf("k6 checks are not enabled") } case sm.CheckTypeGrpc: - p, err = grpc.NewProber(ctx, check.Check, logger) + p, err = grpc.NewProber(ctx, check, logger) target = check.Target default: diff --git a/internal/prober/scripted/scripted.go b/internal/prober/scripted/scripted.go index da50456e..023d61b6 100644 --- a/internal/prober/scripted/scripted.go +++ b/internal/prober/scripted/scripted.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/grafana/synthetic-monitoring-agent/internal/k6runner" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "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" @@ -26,7 +27,7 @@ type Prober struct { processor *k6runner.Processor } -func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, runner k6runner.Runner) (Prober, error) { +func NewProber(ctx context.Context, check model.Check, logger zerolog.Logger, runner k6runner.Runner) (Prober, error) { var p Prober if check.Settings.Scripted == nil { @@ -40,7 +41,7 @@ func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger, runne Settings: k6runner.Settings{ Timeout: check.Timeout, }, - // TODO: Add metadata & features here. + CheckInfo: k6runner.CheckInfoFromSM(check), }, } diff --git a/internal/prober/scripted/scripted_test.go b/internal/prober/scripted/scripted_test.go index 2c39df83..5303795a 100644 --- a/internal/prober/scripted/scripted_test.go +++ b/internal/prober/scripted/scripted_test.go @@ -6,7 +6,8 @@ import ( "time" "github.com/grafana/synthetic-monitoring-agent/internal/k6runner" - "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" + "github.com/grafana/synthetic-monitoring-agent/internal/model" + sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/rs/zerolog" "github.com/stretchr/testify/require" ) @@ -18,33 +19,37 @@ func TestNewProber(t *testing.T) { logger := zerolog.New(zerolog.NewTestWriter(t)) testcases := map[string]struct { - check synthetic_monitoring.Check + check model.Check expectFailure bool }{ "valid": { expectFailure: false, - check: synthetic_monitoring.Check{ - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: synthetic_monitoring.CheckSettings{ - Scripted: &synthetic_monitoring.ScriptedSettings{ - Script: []byte("// test"), + check: model.Check{ + Check: sm.Check{ + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{ + Scripted: &sm.ScriptedSettings{ + Script: []byte("// test"), + }, }, }, }, }, "invalid": { expectFailure: true, - check: synthetic_monitoring.Check{ - Target: "http://www.example.org", - Job: "test", - Frequency: 10 * 1000, - Timeout: 10 * 1000, - Probes: []int64{1}, - Settings: synthetic_monitoring.CheckSettings{}, + check: model.Check{ + Check: sm.Check{ + Target: "http://www.example.org", + Job: "test", + Frequency: 10 * 1000, + Timeout: 10 * 1000, + Probes: []int64{1}, + Settings: sm.CheckSettings{}, + }, }, }, } diff --git a/internal/prober/tcp/tcp.go b/internal/prober/tcp/tcp.go index 0591011f..ce29854e 100644 --- a/internal/prober/tcp/tcp.go +++ b/internal/prober/tcp/tcp.go @@ -5,6 +5,7 @@ import ( "errors" "time" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" "github.com/grafana/synthetic-monitoring-agent/internal/tls" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" @@ -20,7 +21,7 @@ type Prober struct { config config.Module } -func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger) (Prober, error) { +func NewProber(ctx context.Context, check model.Check, logger zerolog.Logger) (Prober, error) { if check.Settings.Tcp == nil { return Prober{}, errUnsupportedCheck } diff --git a/internal/prober/tcp/tcp_test.go b/internal/prober/tcp/tcp_test.go index 1605818a..404ea7a0 100644 --- a/internal/prober/tcp/tcp_test.go +++ b/internal/prober/tcp/tcp_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/grafana/synthetic-monitoring-agent/internal/model" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/prometheus/blackbox_exporter/config" "github.com/rs/zerolog" @@ -19,15 +20,17 @@ func TestName(t *testing.T) { func TestNewProber(t *testing.T) { testcases := map[string]struct { - input sm.Check + input model.Check expected Prober ExpectError bool }{ "default": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Tcp: &sm.TcpSettings{}, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Tcp: &sm.TcpSettings{}, + }, }, }, expected: Prober{ @@ -44,10 +47,12 @@ func TestNewProber(t *testing.T) { ExpectError: false, }, "no-settings": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Tcp: nil, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Tcp: nil, + }, }, }, expected: Prober{}, diff --git a/internal/prober/traceroute/traceroute.go b/internal/prober/traceroute/traceroute.go index 695c870a..93510b72 100644 --- a/internal/prober/traceroute/traceroute.go +++ b/internal/prober/traceroute/traceroute.go @@ -9,6 +9,7 @@ import ( "time" "github.com/google/uuid" + "github.com/grafana/synthetic-monitoring-agent/internal/model" "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" @@ -36,7 +37,7 @@ type Prober struct { logger zerolog.Logger } -func NewProber(check sm.Check, logger zerolog.Logger) (Prober, error) { +func NewProber(check model.Check, logger zerolog.Logger) (Prober, error) { if check.Settings.Traceroute == nil { return Prober{}, errUnsupportedCheck } @@ -65,7 +66,6 @@ func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.R p.config.ringBufferSize, p.config.ptrLookup, ) - if err != nil { logErr := logger.Log(err) if logErr != nil { @@ -83,9 +83,8 @@ func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.R } } }(ch) - var success = true + success := true err = m.RunWithContext(ctx, p.config.count) - if err != nil { err = logger.Log("Level", "error", "msg", err.Error()) if err != nil { @@ -96,7 +95,7 @@ func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.R tracerouteID := uuid.New() totalPacketsLost := float64(0) totalPacketsSent := float64(0) - var hosts = make(map[int]string) + hosts := make(map[int]string) for ttl, hop := range m.Statistic { totalPacketsLost += float64(hop.Lost) totalPacketsSent += float64(hop.Sent) @@ -133,17 +132,17 @@ func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.R return false, 0 } - var traceHashGauge = prometheus.NewGauge(prometheus.GaugeOpts{ + traceHashGauge := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "probe_traceroute_route_hash", Help: "Hash of all the hosts in a traceroute path. Used to determine route volatility.", }) - var totalHopsGauge = prometheus.NewGauge(prometheus.GaugeOpts{ + totalHopsGauge := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "probe_traceroute_total_hops", Help: "Total hops to reach a traceroute destination", }) - var overallPacketLossGauge = prometheus.NewGauge(prometheus.GaugeOpts{ + overallPacketLossGauge := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "probe_traceroute_packet_loss_percent", Help: "Overall percentage of packet loss during the traceroute", }) diff --git a/internal/prober/traceroute/traceroute_test.go b/internal/prober/traceroute/traceroute_test.go index 59198c6c..0f1eee15 100644 --- a/internal/prober/traceroute/traceroute_test.go +++ b/internal/prober/traceroute/traceroute_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/grafana/synthetic-monitoring-agent/internal/model" sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" "github.com/rs/zerolog" "github.com/stretchr/testify/require" @@ -18,15 +19,17 @@ func TestName(t *testing.T) { func TestNewProber(t *testing.T) { logger := zerolog.New(io.Discard) testcases := map[string]struct { - input sm.Check + input model.Check expected Prober ExpectError bool }{ "default": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Traceroute: &sm.TracerouteSettings{}, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Traceroute: &sm.TracerouteSettings{}, + }, }, }, expected: Prober{ @@ -47,10 +50,12 @@ func TestNewProber(t *testing.T) { ExpectError: false, }, "no-settings": { - input: sm.Check{ - Target: "www.grafana.com", - Settings: sm.CheckSettings{ - Tcp: nil, + input: model.Check{ + Check: sm.Check{ + Target: "www.grafana.com", + Settings: sm.CheckSettings{ + Tcp: nil, + }, }, }, expected: Prober{}, diff --git a/internal/scraper/scraper_test.go b/internal/scraper/scraper_test.go index 009176d2..c4dd39a6 100644 --- a/internal/scraper/scraper_test.go +++ b/internal/scraper/scraper_test.go @@ -69,7 +69,7 @@ var updateGolden = flag.Bool("update-golden", false, "update golden files") // go test -v -race -run TestValidateMetrics ./internal/scraper/ func TestValidateMetrics(t *testing.T) { testcases := map[string]struct { - setup func(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) + setup func(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) }{ "ping": { setup: setupPingProbe, @@ -129,7 +129,7 @@ func TestValidateMetrics(t *testing.T) { func verifyProberMetrics( t *testing.T, name string, - setup func(context.Context, *testing.T) (prober.Prober, sm.Check, func()), + setup func(context.Context, *testing.T) (prober.Prober, model.Check, func()), basicMetricsOnly bool, ) { timeout := 10 * time.Second @@ -237,13 +237,15 @@ func readGoldenFile(fn string) (map[string]struct{}, error) { } } -func setupPingProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { - check := sm.Check{ - Target: "127.0.0.1", - Timeout: 2000, - Settings: sm.CheckSettings{ - Ping: &sm.PingSettings{ - IpVersion: sm.IpVersion_V4, +func setupPingProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { + check := model.Check{ + Check: sm.Check{ + Target: "127.0.0.1", + Timeout: 2000, + Settings: sm.CheckSettings{ + Ping: &sm.PingSettings{ + IpVersion: sm.IpVersion_V4, + }, }, }, } @@ -256,18 +258,20 @@ func setupPingProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, return prober, check, func() {} } -func setupHTTPProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupHTTPProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { httpSrv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) })) httpSrv.Start() - check := sm.Check{ - Target: httpSrv.URL, - Timeout: 2000, - Settings: sm.CheckSettings{ - Http: &sm.HttpSettings{ - IpVersion: sm.IpVersion_V4, + check := model.Check{ + Check: sm.Check{ + Target: httpSrv.URL, + Timeout: 2000, + Settings: sm.CheckSettings{ + Http: &sm.HttpSettings{ + IpVersion: sm.IpVersion_V4, + }, }, }, } @@ -285,20 +289,22 @@ func setupHTTPProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, return prober, check, httpSrv.Close } -func setupHTTPSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupHTTPSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { httpSrv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) })) httpSrv.StartTLS() - check := sm.Check{ - Target: httpSrv.URL, - Timeout: 2000, - Settings: sm.CheckSettings{ - Http: &sm.HttpSettings{ - IpVersion: sm.IpVersion_V4, - TlsConfig: &sm.TLSConfig{ - InsecureSkipVerify: true, + check := model.Check{ + Check: sm.Check{ + Target: httpSrv.URL, + Timeout: 2000, + Settings: sm.CheckSettings{ + Http: &sm.HttpSettings{ + IpVersion: sm.IpVersion_V4, + TlsConfig: &sm.TLSConfig{ + InsecureSkipVerify: true, + }, }, }, }, @@ -317,17 +323,19 @@ func setupHTTPSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Che return prober, check, httpSrv.Close } -func setupDNSProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupDNSProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { srv, clean := setupDNSServer(t) - check := sm.Check{ - Target: "example.org", - Timeout: 2000, - Settings: sm.CheckSettings{ - // target is "example.com" - Dns: &sm.DnsSettings{ - Server: srv, - IpVersion: sm.IpVersion_V4, - Protocol: sm.DnsProtocol_UDP, + check := model.Check{ + Check: sm.Check{ + Target: "example.org", + Timeout: 2000, + Settings: sm.CheckSettings{ + // target is "example.com" + Dns: &sm.DnsSettings{ + Server: srv, + IpVersion: sm.IpVersion_V4, + Protocol: sm.DnsProtocol_UDP, + }, }, }, } @@ -339,14 +347,16 @@ func setupDNSProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, return prober, check, clean } -func setupTCPProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupTCPProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { srv, clean := setupTCPServer(t) - check := sm.Check{ - Target: srv, - Timeout: 2000, - Settings: sm.CheckSettings{ - Tcp: &sm.TcpSettings{ - IpVersion: sm.IpVersion_V4, + check := model.Check{ + Check: sm.Check{ + Target: srv, + Timeout: 2000, + Settings: sm.CheckSettings{ + Tcp: &sm.TcpSettings{ + IpVersion: sm.IpVersion_V4, + }, }, }, } @@ -361,17 +371,19 @@ func setupTCPProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, return prober, check, clean } -func setupTCPSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupTCPSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { srv, clean := setupTCPServerWithSSL(t) - check := sm.Check{ - Target: srv, - Timeout: 2000, - Settings: sm.CheckSettings{ - Tcp: &sm.TcpSettings{ - IpVersion: sm.IpVersion_V4, - Tls: true, - TlsConfig: &sm.TLSConfig{ - InsecureSkipVerify: true, + check := model.Check{ + Check: sm.Check{ + Target: srv, + Timeout: 2000, + Settings: sm.CheckSettings{ + Tcp: &sm.TcpSettings{ + IpVersion: sm.IpVersion_V4, + Tls: true, + TlsConfig: &sm.TLSConfig{ + InsecureSkipVerify: true, + }, }, }, }, @@ -387,7 +399,7 @@ func setupTCPSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Chec return prober, check, clean } -func setupTracerouteProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupTracerouteProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { checkCap := func(set *cap.Set, v cap.Value) { if permitted, err := set.GetFlag(cap.Permitted, v); err != nil { t.Fatalf("cannot get %s flag: %s", v, err) @@ -399,10 +411,12 @@ func setupTracerouteProbe(ctx context.Context, t *testing.T) (prober.Prober, sm. checkCap(c, cap.NET_ADMIN) checkCap(c, cap.NET_RAW) - check := sm.Check{ - Target: "127.0.0.1", - Settings: sm.CheckSettings{ - Traceroute: &sm.TracerouteSettings{}, + check := model.Check{ + Check: sm.Check{ + Target: "127.0.0.1", + Settings: sm.CheckSettings{ + Traceroute: &sm.TracerouteSettings{}, + }, }, } @@ -414,18 +428,20 @@ func setupTracerouteProbe(ctx context.Context, t *testing.T) (prober.Prober, sm. return p, check, func() {} } -func setupScriptedProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupScriptedProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { httpSrv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) })) httpSrv.Start() - check := sm.Check{ - Target: httpSrv.URL, - Timeout: 2000, - Settings: sm.CheckSettings{ - Scripted: &sm.ScriptedSettings{ - Script: []byte(`export default function() {}`), + check := model.Check{ + Check: sm.Check{ + Target: httpSrv.URL, + Timeout: 2000, + Settings: sm.CheckSettings{ + Scripted: &sm.ScriptedSettings{ + Script: []byte(`export default function() {}`), + }, }, }, } @@ -454,22 +470,24 @@ func setupScriptedProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Ch return prober, check, httpSrv.Close } -func setupMultiHTTPProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupMultiHTTPProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { httpSrv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) })) httpSrv.Start() - check := sm.Check{ - Target: httpSrv.URL, - Timeout: 2000, - Settings: sm.CheckSettings{ - Multihttp: &sm.MultiHttpSettings{ - Entries: []*sm.MultiHttpEntry{ - { - Request: &sm.MultiHttpEntryRequest{ - Method: sm.HttpMethod_GET, - Url: httpSrv.URL, + check := model.Check{ + Check: sm.Check{ + Target: httpSrv.URL, + Timeout: 2000, + Settings: sm.CheckSettings{ + Multihttp: &sm.MultiHttpSettings{ + Entries: []*sm.MultiHttpEntry{ + { + Request: &sm.MultiHttpEntryRequest{ + Method: sm.HttpMethod_GET, + Url: httpSrv.URL, + }, }, }, }, @@ -502,18 +520,20 @@ func setupMultiHTTPProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.C return prober, check, httpSrv.Close } -func setupBrowserProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupBrowserProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { httpSrv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) })) httpSrv.Start() - check := sm.Check{ - Target: httpSrv.URL, - Timeout: 2000, - Settings: sm.CheckSettings{ - Browser: &sm.BrowserSettings{ - Script: []byte(`export default function() {}`), + check := model.Check{ + Check: sm.Check{ + Target: httpSrv.URL, + Timeout: 2000, + Settings: sm.CheckSettings{ + Browser: &sm.BrowserSettings{ + Script: []byte(`export default function() {}`), + }, }, }, } @@ -542,14 +562,16 @@ func setupBrowserProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Che return prober, check, httpSrv.Close } -func setupGRPCProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupGRPCProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { srv, clean := setupGRPCServer(t) - check := sm.Check{ - Target: srv, - Timeout: 2000, - Settings: sm.CheckSettings{ - Grpc: &sm.GrpcSettings{ - IpVersion: sm.IpVersion_V4, + check := model.Check{ + Check: sm.Check{ + Target: srv, + Timeout: 2000, + Settings: sm.CheckSettings{ + Grpc: &sm.GrpcSettings{ + IpVersion: sm.IpVersion_V4, + }, }, }, } @@ -564,17 +586,19 @@ func setupGRPCProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, return prober, check, clean } -func setupGRPCSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) { +func setupGRPCSSLProbe(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) { srv, clean := setupGRPCServerWithSSL(t) - check := sm.Check{ - Target: srv, - Timeout: 2000, - Settings: sm.CheckSettings{ - Grpc: &sm.GrpcSettings{ - IpVersion: sm.IpVersion_V4, - Tls: true, - TlsConfig: &sm.TLSConfig{ - InsecureSkipVerify: true, + check := model.Check{ + Check: sm.Check{ + Target: srv, + Timeout: 2000, + Settings: sm.CheckSettings{ + Grpc: &sm.GrpcSettings{ + IpVersion: sm.IpVersion_V4, + Tls: true, + TlsConfig: &sm.TLSConfig{ + InsecureSkipVerify: true, + }, }, }, }, @@ -815,7 +839,7 @@ func setupGRPCServerWithSSL(t *testing.T) (string, func()) { // be set without exceeding the Mimir and Loki limits. func TestValidateLabels(t *testing.T) { testCases := map[string]struct { - setup func(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) + setup func(ctx context.Context, t *testing.T) (prober.Prober, model.Check, func()) }{ "ping": { setup: setupPingProbe, @@ -937,9 +961,7 @@ func TestValidateLabels(t *testing.T) { }, summaries: make(map[uint64]prometheus.Summary), histograms: make(map[uint64]prometheus.Histogram), - check: model.Check{ - Check: check, - }, + check: check, probe: sm.Probe{ Id: 100, TenantId: 200,