Skip to content

Commit

Permalink
context propagation: SendMetrics, SendUsageMetrics, plugin config
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 24, 2024
1 parent 95ed419 commit af21892
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
12 changes: 4 additions & 8 deletions pkg/apiserver/apic_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ func (a *apic) fetchMachineIDs(ctx context.Context) ([]string, error) {
// Metrics are sent at start, then at the randomized metricsIntervalFirst,
// then at regular metricsInterval. If a change is detected in the list
// of machines, the next metrics are sent immediately.
func (a *apic) SendMetrics(stop chan (bool)) {
func (a *apic) SendMetrics(ctx context.Context, stop chan (bool)) {
defer trace.CatchPanic("lapi/metricsToAPIC")

ctx := context.TODO()

// verify the list of machines every <checkInt> interval
const checkInt = 20 * time.Second

Expand Down Expand Up @@ -321,7 +319,7 @@ func (a *apic) SendMetrics(stop chan (bool)) {
if metrics != nil {
log.Info("capi metrics: sending")

_, _, err = a.apiClient.Metrics.Add(context.Background(), metrics)
_, _, err = a.apiClient.Metrics.Add(ctx, metrics)
if err != nil {
log.Errorf("capi metrics: failed: %s", err)
}
Expand All @@ -339,11 +337,9 @@ func (a *apic) SendMetrics(stop chan (bool)) {
}
}

func (a *apic) SendUsageMetrics() {
func (a *apic) SendUsageMetrics(ctx context.Context) {
defer trace.CatchPanic("lapi/usageMetricsToAPIC")

ctx := context.TODO()

firstRun := true

log.Debugf("Start sending usage metrics to CrowdSec Central API (interval: %s once, then %s)", a.usageMetricsIntervalFirst, a.usageMetricsInterval)
Expand All @@ -368,7 +364,7 @@ func (a *apic) SendUsageMetrics() {
continue
}

_, resp, err := a.apiClient.UsageMetrics.Add(context.Background(), metrics)
_, resp, err := a.apiClient.UsageMetrics.Add(ctx, metrics)

Check warning on line 367 in pkg/apiserver/apic_metrics.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apic_metrics.go#L367

Added line #L367 was not covered by tests
if err != nil {
log.Errorf("unable to send usage metrics: %s", err)

Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ func (s *APIServer) initAPIC(ctx context.Context) {
}

s.apic.metricsTomb.Go(func() error {
s.apic.SendMetrics(make(chan bool))
s.apic.SendMetrics(ctx, make(chan bool))
return nil
})

s.apic.metricsTomb.Go(func() error {
s.apic.SendUsageMetrics()
s.apic.SendUsageMetrics(ctx)
return nil
})
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/csplugin/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ func (m *GRPCClient) Notify(ctx context.Context, notification *protobufs.Notific
}

func (m *GRPCClient) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
_, err := m.client.Configure(
context.Background(), config,
)
_, err := m.client.Configure(ctx, config)
return &protobufs.Empty{}, err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/protobufs/plugin_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ type NotifierPlugin struct {
type GRPCClient struct{ client NotifierClient }

func (m *GRPCClient) Notify(ctx context.Context, notification *Notification) (*Empty, error) {
_, err := m.client.Notify(context.Background(), notification)
_, err := m.client.Notify(ctx, notification)

Check warning on line 27 in pkg/protobufs/plugin_interface.go

View check run for this annotation

Codecov / codecov/patch

pkg/protobufs/plugin_interface.go#L27

Added line #L27 was not covered by tests
return &Empty{}, err
}

func (m *GRPCClient) Configure(ctx context.Context, config *Config) (*Empty, error) {
_, err := m.client.Configure(context.Background(), config)
_, err := m.client.Configure(ctx, config)

Check warning on line 32 in pkg/protobufs/plugin_interface.go

View check run for this annotation

Codecov / codecov/patch

pkg/protobufs/plugin_interface.go#L32

Added line #L32 was not covered by tests
return &Empty{}, err
}

Expand Down

0 comments on commit af21892

Please sign in to comment.