-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1468 from openmeterio/refactor-collector-meter
refactor: collector meter
- Loading branch information
Showing
3 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ingestadapter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cloudevents/sdk-go/v2/event" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
|
||
"github.com/openmeterio/openmeter/openmeter/ingest" | ||
) | ||
|
||
// collectorMetrics emits metrics for ingested events. | ||
type collectorMetrics struct { | ||
collector ingest.Collector | ||
|
||
ingestEventsCounter metric.Int64Counter | ||
ingestErrorsCounter 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", | ||
metric.WithDescription("Number of events ingested"), | ||
metric.WithUnit("{event}"), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create events counter: %w", err) | ||
} | ||
|
||
ingestErrorsCounter, err := metricMeter.Int64Counter( | ||
"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) | ||
} | ||
|
||
return collectorMetrics{ | ||
collector: collector, | ||
|
||
ingestEventsCounter: ingestEventsCounter, | ||
ingestErrorsCounter: ingestErrorsCounter, | ||
}, nil | ||
} | ||
|
||
// Ingest implements the [ingest.Collector] interface. | ||
func (c collectorMetrics) Ingest(ctx context.Context, namespace string, ev event.Event) error { | ||
namespaceAttr := attribute.String("namespace", namespace) | ||
|
||
err := c.collector.Ingest(ctx, namespace, ev) | ||
if err != nil { | ||
c.ingestErrorsCounter.Add(ctx, 1, metric.WithAttributes(namespaceAttr)) | ||
|
||
return err | ||
} | ||
|
||
c.ingestEventsCounter.Add(ctx, 1, metric.WithAttributes(namespaceAttr)) | ||
|
||
return nil | ||
} | ||
|
||
// Close implements the [ingest.Collector] interface. | ||
func (c collectorMetrics) Close() { | ||
c.collector.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters