diff --git a/core/capabilities/compute/compute.go b/core/capabilities/compute/compute.go index 3d3a734bec9..89675c0e7cc 100644 --- a/core/capabilities/compute/compute.go +++ b/core/capabilities/compute/compute.go @@ -79,7 +79,7 @@ var _ capabilities.ActionCapability = (*Compute)(nil) type Compute struct { stopCh services.StopChan log logger.Logger - metrics computeMetricsLabeler + metrics *computeMetricsLabeler // emitter is used to emit messages from the WASM module to a configured collector. emitter custmsg.MessageEmitter @@ -248,11 +248,6 @@ func (c *Compute) Info(ctx context.Context) (capabilities.CapabilityInfo, error) func (c *Compute) Start(ctx context.Context) error { c.modules.start() - err := initMonitoringResources() - if err != nil { - return fmt.Errorf("failed to initialize monitoring resources: %w", err) - } - c.wg.Add(c.numWorkers) for i := 0; i < c.numWorkers; i++ { go func() { @@ -373,10 +368,14 @@ func NewAction( handler *webapi.OutgoingConnectorHandler, idGenerator func() string, opts ...func(*Compute), -) *Compute { +) (*Compute, error) { if config.NumWorkers == 0 { config.NumWorkers = defaultNumWorkers } + metricsLabeler, err := newComputeMetricsLabeler(metrics.NewLabeler().With("capability", CapabilityIDCompute)) + if err != nil { + return nil, fmt.Errorf("failed to create compute metrics labeler: %w", err) + } var ( lggr = logger.Named(log, "CustomCompute") labeler = custmsg.NewLabeler() @@ -384,7 +383,7 @@ func NewAction( stopCh: make(services.StopChan), log: lggr, emitter: labeler, - metrics: computeMetricsLabeler{metrics.NewLabeler().With("capability", CapabilityIDCompute)}, + metrics: metricsLabeler, registry: registry, modules: newModuleCache(clockwork.NewRealClock(), 1*time.Minute, 10*time.Minute, 3), transformer: NewTransformer(lggr, labeler), @@ -399,5 +398,5 @@ func NewAction( opt(compute) } - return compute + return compute, nil } diff --git a/core/capabilities/compute/monitoring.go b/core/capabilities/compute/monitoring.go index b2e0c110a8a..71354c014a7 100644 --- a/core/capabilities/compute/monitoring.go +++ b/core/capabilities/compute/monitoring.go @@ -14,26 +14,25 @@ import ( const timestampKey = "computeTimestamp" -var computeHTTPRequestCounter metric.Int64Counter +type computeMetricsLabeler struct { + metrics.Labeler + computeHTTPRequestCounter metric.Int64Counter +} -func initMonitoringResources() (err error) { - computeHTTPRequestCounter, err = beholder.GetMeter().Int64Counter("capabilities_compute_http_request_count") +func newComputeMetricsLabeler(l metrics.Labeler) (*computeMetricsLabeler, error) { + computeHTTPRequestCounter, err := beholder.GetMeter().Int64Counter("capabilities_compute_http_request_count") if err != nil { - return fmt.Errorf("failed to register compute http request counter: %w", err) + return nil, fmt.Errorf("failed to register compute http request counter: %w", err) } - return nil -} - -type computeMetricsLabeler struct { - metrics.Labeler + return &computeMetricsLabeler{Labeler: l, computeHTTPRequestCounter: computeHTTPRequestCounter}, nil } -func (c computeMetricsLabeler) with(keyValues ...string) computeMetricsLabeler { - return computeMetricsLabeler{c.With(keyValues...)} +func (c *computeMetricsLabeler) with(keyValues ...string) *computeMetricsLabeler { + return &computeMetricsLabeler{c.With(keyValues...), c.computeHTTPRequestCounter} } -func (c computeMetricsLabeler) incrementHTTPRequestCounter(ctx context.Context) { +func (c *computeMetricsLabeler) incrementHTTPRequestCounter(ctx context.Context) { otelLabels := localMonitoring.KvMapToOtelAttributes(c.Labels) - computeHTTPRequestCounter.Add(ctx, 1, metric.WithAttributes(otelLabels...)) + c.computeHTTPRequestCounter.Add(ctx, 1, metric.WithAttributes(otelLabels...)) }