Skip to content

Commit

Permalink
refactor(ingest): prefix metrics with openmeter
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <[email protected]>
  • Loading branch information
sagikazarmark committed Oct 25, 2024
1 parent 4143b1d commit c65ca95
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion openmeter/ingest/ingestadapter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ type collectorMetrics struct {

ingestEventsCounter metric.Int64Counter
ingestErrorsCounter metric.Int64Counter

// TODO: remove after deprecation period
ingestEventsCounterOld metric.Int64Counter
ingestErrorsCounterOld metric.Int64Counter
}

// WithMetrics wraps an [ingest.Collector] and emits metrics for ingested events.
func WithMetrics(collector ingest.Collector, metricMeter metric.Meter) (ingest.Collector, error) {
ingestEventsCounter, err := metricMeter.Int64Counter(
"ingest.events",
"openmeter.ingest.events",
metric.WithDescription("Number of events ingested"),
metric.WithUnit("{event}"),
)
Expand All @@ -31,6 +35,24 @@ func WithMetrics(collector ingest.Collector, metricMeter metric.Meter) (ingest.C
}

ingestErrorsCounter, err := metricMeter.Int64Counter(
"openmeter.ingest.errors",
metric.WithDescription("Number of failed event ingests"),
metric.WithUnit("{error}"),
)
if err != nil {
return nil, fmt.Errorf("failed to create errors counter: %w", err)
}

ingestEventsCounterOld, err := metricMeter.Int64Counter(
"ingest.events",
metric.WithDescription("Number of events ingested"),
metric.WithUnit("{event}"),
)
if err != nil {
return nil, fmt.Errorf("failed to create events counter: %w", err)
}

ingestErrorsCounterOld, err := metricMeter.Int64Counter(
"ingest.errors",
metric.WithDescription("Number of failed event ingests"),
metric.WithUnit("{error}"),
Expand All @@ -44,6 +66,9 @@ func WithMetrics(collector ingest.Collector, metricMeter metric.Meter) (ingest.C

ingestEventsCounter: ingestEventsCounter,
ingestErrorsCounter: ingestErrorsCounter,

ingestEventsCounterOld: ingestEventsCounterOld,
ingestErrorsCounterOld: ingestErrorsCounterOld,
}, nil
}

Expand All @@ -54,11 +79,13 @@ func (c collectorMetrics) Ingest(ctx context.Context, namespace string, ev event
err := c.collector.Ingest(ctx, namespace, ev)
if err != nil {
c.ingestErrorsCounter.Add(ctx, 1, metric.WithAttributes(namespaceAttr))
c.ingestErrorsCounterOld.Add(ctx, 1, metric.WithAttributes(namespaceAttr))

return err
}

c.ingestEventsCounter.Add(ctx, 1, metric.WithAttributes(namespaceAttr))
c.ingestEventsCounterOld.Add(ctx, 1, metric.WithAttributes(namespaceAttr))

return nil
}
Expand Down

0 comments on commit c65ca95

Please sign in to comment.