Skip to content

Commit

Permalink
Fix: parametrize the k6 runner
Browse files Browse the repository at this point in the history
The k6 runner was left hard-coded because I forgot about it.

This is a really noisy change that is adding a parameter to specifying
the runner that should be used. The goal is to be able to write tests
that don't actually run k6 scripts, but instead use pre-defined
responses to make it predictable. This also helps with the accounting
package that needs to actually run the prober to figure out the number
of metrics it produces.

Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
  • Loading branch information
mem committed Jun 5, 2023
1 parent 9f632d1 commit 72586a6
Show file tree
Hide file tree
Showing 20 changed files with 1,198 additions and 454 deletions.
5 changes: 5 additions & 0 deletions cmd/synthetic-monitoring-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/grafana/synthetic-monitoring-agent/internal/checks"
"github.com/grafana/synthetic-monitoring-agent/internal/feature"
"github.com/grafana/synthetic-monitoring-agent/internal/http"
"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/pusher"
"github.com/grafana/synthetic-monitoring-agent/internal/version"
"github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
Expand Down Expand Up @@ -164,6 +165,8 @@ func run(args []string, stdout io.Writer) error {
}
defer conn.Close()

k6Runner := k6runner.New("k6") // FIXME(mem): get this from options

checksUpdater, err := checks.NewUpdater(checks.UpdaterOptions{
Conn: conn,
Logger: zl.With().Str("subsystem", "updater").Logger(),
Expand All @@ -173,6 +176,7 @@ func run(args []string, stdout io.Writer) error {
IsConnected: readynessHandler.Set,
PromRegisterer: promRegisterer,
Features: features,
K6Runner: k6Runner,
})
if err != nil {
return fmt.Errorf("Cannot create checks updater: %w", err)
Expand All @@ -191,6 +195,7 @@ func run(args []string, stdout io.Writer) error {
TenantCh: tenantCh,
PromRegisterer: promRegisterer,
Features: features,
K6Runner: k6Runner,
})
if err != nil {
return fmt.Errorf("Cannot create ad-hoc checks handler: %w", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/jpillora/backoff v1.0.0
github.com/mccutchen/go-httpbin/v2 v2.8.0
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/spf13/afero v1.9.5
kernel.org/pub/linux/libs/security/libcap/cap v1.2.69
)

Expand Down
393 changes: 393 additions & 0 deletions go.sum

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion internal/adhoc/adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/grafana/synthetic-monitoring-agent/internal/feature"
"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/pkg/logproto"
"github.com/grafana/synthetic-monitoring-agent/internal/prober"
"github.com/grafana/synthetic-monitoring-agent/internal/pusher"
Expand Down Expand Up @@ -37,6 +38,7 @@ type Handler struct {
tenantCh chan<- sm.Tenant
runnerFactory func(context.Context, *sm.AdHocRequest) (*runner, error)
grpcAdhocChecksClientFactory func(conn ClientConn) (sm.AdHocChecksClient, error)
proberFactory prober.ProberFactory
}

// Error represents errors returned from this package.
Expand Down Expand Up @@ -102,6 +104,7 @@ type HandlerOpts struct {
TenantCh chan<- sm.Tenant
PromRegisterer prometheus.Registerer
Features feature.Collection
K6Runner k6runner.Runner

// these two fields exists so that tests can pass alternate
// implementations, they are unexported so that clients of this
Expand Down Expand Up @@ -139,6 +142,7 @@ func NewHandler(opts HandlerOpts) (*Handler, error) {
tenantCh: opts.TenantCh,
runnerFactory: opts.runnerFactory,
grpcAdhocChecksClientFactory: opts.grpcAdhocChecksClientFactory,
proberFactory: prober.NewProberFactory(opts.K6Runner),
api: apiInfo{
conn: opts.Conn,
},
Expand Down Expand Up @@ -384,7 +388,7 @@ func (h *Handler) defaultRunnerFactory(ctx context.Context, req *sm.AdHocRequest
Settings: req.AdHocCheck.Settings,
}

p, target, err := prober.NewFromCheck(ctx, h.logger, check)
p, target, err := h.proberFactory.New(ctx, h.logger, check)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions internal/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/grafana/synthetic-monitoring-agent/internal/feature"
"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/pkg/logproto"
"github.com/grafana/synthetic-monitoring-agent/internal/pusher"
"github.com/grafana/synthetic-monitoring-agent/internal/scraper"
Expand Down Expand Up @@ -71,7 +72,8 @@ type Updater struct {
scrapersMutex sync.Mutex
scrapers map[int64]*scraper.Scraper
metrics metrics
scraperFactory func(context.Context, sm.Check, chan<- pusher.Payload, sm.Probe, zerolog.Logger, prometheus.Counter, *prometheus.CounterVec) (*scraper.Scraper, error)
k6Runner k6runner.Runner
scraperFactory func(context.Context, sm.Check, chan<- pusher.Payload, sm.Probe, zerolog.Logger, prometheus.Counter, *prometheus.CounterVec, k6runner.Runner) (*scraper.Scraper, error)
}

type apiInfo struct {
Expand Down Expand Up @@ -100,7 +102,8 @@ type UpdaterOptions struct {
IsConnected func(bool)
PromRegisterer prometheus.Registerer
Features feature.Collection
ScraperFactory func(context.Context, sm.Check, chan<- pusher.Payload, sm.Probe, zerolog.Logger, prometheus.Counter, *prometheus.CounterVec) (*scraper.Scraper, error)
K6Runner k6runner.Runner
ScraperFactory func(context.Context, sm.Check, chan<- pusher.Payload, sm.Probe, zerolog.Logger, prometheus.Counter, *prometheus.CounterVec, k6runner.Runner) (*scraper.Scraper, error)
}

func NewUpdater(opts UpdaterOptions) (*Updater, error) {
Expand Down Expand Up @@ -215,6 +218,7 @@ func NewUpdater(opts UpdaterOptions) (*Updater, error) {
tenantCh: opts.TenantCh,
IsConnected: opts.IsConnected,
scrapers: make(map[int64]*scraper.Scraper),
k6Runner: opts.K6Runner,
scraperFactory: scraperFactory,
metrics: metrics{
changeErrorsCounter: changeErrorsCounter,
Expand Down Expand Up @@ -816,7 +820,7 @@ func (c *Updater) addAndStartScraperWithLock(ctx context.Context, check sm.Check
return err
}

scraper, err := c.scraperFactory(ctx, check, c.publishCh, *c.probe, c.logger, scrapeCounter, scrapeErrorCounter)
scraper, err := c.scraperFactory(ctx, check, c.publishCh, *c.probe, c.logger, scrapeCounter, scrapeErrorCounter, c.k6Runner)
if err != nil {
return fmt.Errorf("cannot create new scraper: %w", err)
}
Expand Down
10 changes: 7 additions & 3 deletions internal/checks/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/grafana/synthetic-monitoring-agent/internal/feature"
"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/prober"
"github.com/grafana/synthetic-monitoring-agent/internal/prober/logger"
"github.com/grafana/synthetic-monitoring-agent/internal/pusher"
Expand Down Expand Up @@ -271,18 +272,21 @@ func (testProber) Probe(ctx context.Context, target string, registry *prometheus
return false
}

func testProbeFactory(ctx context.Context, logger zerolog.Logger, check sm.Check) (prober.Prober, string, error) {
type testProbeFactory struct {
}

func (f testProbeFactory) New(ctx context.Context, logger zerolog.Logger, check sm.Check) (prober.Prober, string, error) {
return testProber{}, check.Target, nil
}

func testScraperFactory(ctx context.Context, check sm.Check, payloadCh chan<- pusher.Payload, _ sm.Probe, logger zerolog.Logger, scrapeCounter prometheus.Counter, scrapeErrorCounter *prometheus.CounterVec) (*scraper.Scraper, error) {
func testScraperFactory(ctx context.Context, check sm.Check, payloadCh chan<- pusher.Payload, _ sm.Probe, logger zerolog.Logger, scrapeCounter prometheus.Counter, scrapeErrorCounter *prometheus.CounterVec, k6Runner k6runner.Runner) (*scraper.Scraper, error) {
return scraper.NewWithOpts(
ctx,
check,
scraper.ScraperOpts{
ErrorCounter: scrapeErrorCounter,
Logger: logger,
ProbeFactory: testProbeFactory,
ProbeFactory: testProbeFactory{},
PublishCh: payloadCh,
ScrapeCounter: scrapeCounter,
},
Expand Down

0 comments on commit 72586a6

Please sign in to comment.