From 106a943af64584b0eb488e800994a20480560715 Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 24 Sep 2024 15:20:14 +0200 Subject: [PATCH] context propagation: SendMetrics, SendUsageMetrics, plugin config --- pkg/apiserver/apic_metrics.go | 12 ++++-------- pkg/csplugin/notifier.go | 4 +--- pkg/protobufs/plugin_interface.go | 4 ++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/pkg/apiserver/apic_metrics.go b/pkg/apiserver/apic_metrics.go index 16b2328dbe9..3d9e7b28a79 100644 --- a/pkg/apiserver/apic_metrics.go +++ b/pkg/apiserver/apic_metrics.go @@ -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 interval const checkInt = 20 * time.Second @@ -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) } @@ -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) @@ -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) diff --git a/pkg/csplugin/notifier.go b/pkg/csplugin/notifier.go index 2b5d57fbcff..ed4a4cc4149 100644 --- a/pkg/csplugin/notifier.go +++ b/pkg/csplugin/notifier.go @@ -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 } diff --git a/pkg/protobufs/plugin_interface.go b/pkg/protobufs/plugin_interface.go index fc89b2fa009..baa76c8941c 100644 --- a/pkg/protobufs/plugin_interface.go +++ b/pkg/protobufs/plugin_interface.go @@ -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 }