package otelsetup import ( "context" "sort" "testing" "time" "gitlab.com/unboundsoftware/eventsourced/eventsourced" "go.opentelemetry.io/otel" sdkmetric "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/metricdata" ) func TestNewEventsourcedMetrics_RecordsContract(t *testing.T) { reader := sdkmetric.NewManualReader() otel.SetMeterProvider(sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))) r, err := NewEventsourcedMetrics() if err != nil { t.Fatalf("NewEventsourcedMetrics returned error: %v", err) } if r == nil { t.Fatal("NewEventsourcedMetrics returned nil recorder") } // Recording every known metric type (and an unknown one) must not panic // and must emit the expected instruments. for _, m := range []eventsourced.Metric{ eventsourced.CommandDuration{CommandType: "AddEntry", Duration: time.Millisecond, Success: true}, eventsourced.EventStored{AggregateType: "Entry", EventType: "EntryAdded", Duration: time.Millisecond}, eventsourced.EventsLoaded{AggregateType: "Entry", EventCount: 3, Duration: time.Millisecond}, eventsourced.SnapshotStored{AggregateType: "Entry", Duration: time.Millisecond, Success: true}, eventsourced.SnapshotLoaded{AggregateType: "Entry", Found: false, Duration: time.Millisecond}, eventsourced.IdempotencyCheck{AggregateType: "Entry", Hit: true}, unknownMetric{}, } { r.Record(context.Background(), m) } var rm metricdata.ResourceMetrics if err := reader.Collect(context.Background(), &rm); err != nil { t.Fatalf("collect: %v", err) } got := map[string]bool{} for _, sm := range rm.ScopeMetrics { for _, md := range sm.Metrics { got[md.Name] = true } } want := []string{ "eventsourced.command.duration", "eventsourced.event.store.duration", "eventsourced.events.loaded", "eventsourced.event.load.duration", "eventsourced.snapshot.store.duration", "eventsourced.snapshot.load.duration", "eventsourced.idempotency.checks", } var missing []string for _, w := range want { if !got[w] { missing = append(missing, w) } } if len(missing) > 0 { sort.Strings(missing) t.Errorf("missing expected metrics: %v", missing) } } type unknownMetric struct{} func (unknownMetric) IsMetric() {}