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 106a943
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 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)
if err != nil {
log.Errorf("unable to send usage metrics: %s", err)

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)
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)
return &Empty{}, err
}

Expand Down

0 comments on commit 106a943

Please sign in to comment.