Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0
go.opentelemetry.io/otel v1.43.0
go.opentelemetry.io/otel/metric v1.43.0
go.opentelemetry.io/otel/sdk v1.43.0
go.opentelemetry.io/otel/sdk/metric v1.43.0
go.opentelemetry.io/otel/trace v1.43.0
google.golang.org/genproto v0.0.0-20260420184626-e10c466a9529
google.golang.org/protobuf v1.36.11
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
Expand Down
14 changes: 11 additions & 3 deletions opentelemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import (
"go.opentelemetry.io/otel/trace"
)

const instrumentationName = "github.com/get-eventually/go-eventually/opentelemetry"
// InstrumentationName is the instrumentation scope name used by the Tracer
// and Meter exposed by this package.
const InstrumentationName = "github.com/get-eventually/go-eventually/opentelemetry"

// MetricUnitMilliseconds is the [OpenTelemetry-compatible unit] used by the
// duration histograms exposed by this package.
//
// [OpenTelemetry-compatible unit]: https://ucum.org/ucum
const MetricUnitMilliseconds = "ms"

type config struct {
MeterProvider metric.MeterProvider
TracerProvider trace.TracerProvider
}

func (c config) meter() metric.Meter {
return c.MeterProvider.Meter(instrumentationName)
return c.MeterProvider.Meter(InstrumentationName)
}

func (c config) tracer() trace.Tracer {
return c.TracerProvider.Tracer(instrumentationName)
return c.TracerProvider.Tracer(InstrumentationName)
}

// Option specifies instrumentation configuration options.
Expand Down
48 changes: 48 additions & 0 deletions opentelemetry/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package opentelemetry_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/get-eventually/go-eventually/event"
otelex "github.com/get-eventually/go-eventually/opentelemetry"
"github.com/get-eventually/go-eventually/version"
)

func TestWithTracerProvider_OverridesDefault(t *testing.T) {
h := newHarness(t)

ies, err := otelex.NewInstrumentedEventStore(
event.NewInMemoryStore(),
otelex.WithTracerProvider(h.tracer),
otelex.WithMeterProvider(h.meter),
)
require.NoError(t, err)

_, err = ies.Append(t.Context(), "stream", version.Any)
require.NoError(t, err)

spans := h.endedSpans()
require.Len(t, spans, 1, "expected a span to be recorded by the supplied tracer provider")
assert.Equal(t, otelex.InstrumentationName, spans[0].InstrumentationScope().Name)
}

func TestWithMeterProvider_OverridesDefault(t *testing.T) {
h := newHarness(t)

ies, err := otelex.NewInstrumentedEventStore(
event.NewInMemoryStore(),
otelex.WithTracerProvider(h.tracer),
otelex.WithMeterProvider(h.meter),
)
require.NoError(t, err)

_, err = ies.Append(t.Context(), "stream", version.Any)
require.NoError(t, err)

sm := h.collectScopeMetrics(t)
assert.Equal(t, otelex.InstrumentationName, sm.Scope.Name)
assert.NotEmpty(t, sm.Metrics, "expected metrics on the supplied meter provider")
}
31 changes: 23 additions & 8 deletions opentelemetry/event_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ const (
EventStoreNumEventsKey attribute.Key = "event_store.num_events"
)

// Span names emitted by the InstrumentedEventStore instrumentation.
const (
EventStoreStreamSpanName = "event.Store.Stream"
EventStoreAppendSpanName = "event.Store.Append"
)

// Metric names and descriptions exposed by the InstrumentedEventStore instrumentation.
const (
EventStoreStreamDurationMetricName = "eventually.event_store.stream.duration.milliseconds"
EventStoreStreamDurationMetricDescription = "Duration in milliseconds of event.Store.Stream operations performed."

EventStoreAppendDurationMetricName = "eventually.event_store.append.duration.milliseconds"
EventStoreAppendDurationMetricDescription = "Duration in milliseconds of event.Store.Append operations performed."
)

var _ event.Store = new(InstrumentedEventStore)

// InstrumentedEventStore is a wrapper type over an event.Store
Expand All @@ -41,17 +56,17 @@ func (ies *InstrumentedEventStore) registerMetrics(meter metric.Meter) error {
var err error

if ies.streamDuration, err = meter.Int64Histogram(
"eventually.event_store.stream.duration.milliseconds",
metric.WithUnit("ms"),
metric.WithDescription("Duration in milliseconds of event.Store.Stream operations performed."),
EventStoreStreamDurationMetricName,
metric.WithUnit(MetricUnitMilliseconds),
metric.WithDescription(EventStoreStreamDurationMetricDescription),
); err != nil {
return fmt.Errorf("opentelemetry.InstrumentedEventStore: failed to register metric, %w", err)
}

if ies.appendDuration, err = meter.Int64Histogram(
"eventually.event_store.append.duration.milliseconds",
metric.WithUnit("ms"),
metric.WithDescription("Duration in milliseconds of event.Store.Append operations performed."),
EventStoreAppendDurationMetricName,
metric.WithUnit(MetricUnitMilliseconds),
metric.WithDescription(EventStoreAppendDurationMetricDescription),
); err != nil {
return fmt.Errorf("opentelemetry.InstrumentedEventStore: failed to register metric, %w", err)
}
Expand Down Expand Up @@ -96,7 +111,7 @@ func (ies *InstrumentedEventStore) Stream(
}

return event.NewStream(func(yield func(event.Persisted) bool) error {
ctx, span := ies.tracer.Start(ctx, "event.Store.Stream", trace.WithAttributes(attributes...))
ctx, span := ies.tracer.Start(ctx, EventStoreStreamSpanName, trace.WithAttributes(attributes...))
start := time.Now()

inner := ies.eventStore.Stream(ctx, id, selector)
Expand Down Expand Up @@ -146,7 +161,7 @@ func (ies *InstrumentedEventStore) Append(
EventStoreNumEventsKey.Int(len(events)),
}

ctx, span := ies.tracer.Start(ctx, "event.Store.Append", trace.WithAttributes(attributes...))
ctx, span := ies.tracer.Start(ctx, EventStoreAppendSpanName, trace.WithAttributes(attributes...))
start := time.Now()

defer func() {
Expand Down
Loading
Loading