Skip to content

Commit 2b6e42b

Browse files
committed
Make it easier to use a custom provider
Add a NewMetricsOnly provider constructor, so a custom provider can be composed of this provider that provides access to the span based metrics. Add a GlobalFieldsAnnotator to expose the global fields mechanism.
1 parent 1cd37d5 commit 2b6e42b

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

config/o11y/otel.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ type OtelConfig struct {
7575

7676
// SpanExporters allows you explicitly provide a set of exporters, as an advanced use-case.
7777
SpanExporters []sdktrace.SpanExporter
78+
79+
// ProviderFunc is used to provide a custom provider.
80+
ProviderFunc func(conf otel.Config) (o11y.Provider, error)
7881
}
7982

8083
// Otel is the primary entrypoint to initialize the o11y system for otel.
@@ -90,7 +93,10 @@ func Otel(ctx context.Context, o OtelConfig) (context.Context, func(context.Cont
9093
}
9194
cfg.Metrics = mProv
9295

93-
o11yProvider, err := otel.New(cfg)
96+
if o.ProviderFunc == nil {
97+
o.ProviderFunc = otel.New
98+
}
99+
o11yProvider, err := o.ProviderFunc(cfg)
94100
if err != nil {
95101
return ctx, nil, err
96102
}

o11y/otel/custom.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import (
99

1010
var globalFields = Annotator{}
1111

12+
// GlobalFieldsAnnotator exposes the global fields annotator. When used in conjunction with a Provider, such as the
13+
// MetricsOnly Provider a custom provider can implement the global fields behaviour.
14+
func GlobalFieldsAnnotator() *Annotator {
15+
return &globalFields
16+
}
17+
18+
var _ sdktrace.SpanProcessor = &Annotator{}
19+
1220
// Annotator is a SpanProcessor that adds attributes to all started spans.
1321
type Annotator struct {
1422
attrs []attribute.KeyValue

o11y/otel/otel.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
sdktrace "go.opentelemetry.io/otel/sdk/trace"
2323
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
2424
"go.opentelemetry.io/otel/trace"
25+
"go.opentelemetry.io/otel/trace/noop"
2526

2627
"github.com/circleci/ex/config/secret"
2728
"github.com/circleci/ex/o11y"
@@ -121,6 +122,15 @@ func New(conf Config) (o11y.Provider, error) {
121122
}, nil
122123
}
123124

125+
// NewMetricsOnly returns a metrics only provider, to capture the span metrics behavior.
126+
// This can be used to compose with a custom provider to access span based metrics.
127+
func NewMetricsOnly(metrics o11y.ClosableMetricsProvider) o11y.Provider {
128+
return &Provider{
129+
metricsProvider: metrics,
130+
tracer: noop.NewTracerProvider().Tracer(""),
131+
}
132+
}
133+
124134
func NewHttpExporter(conf Config) (*otlptrace.Exporter, error) {
125135
var serviceName, serviceVersion string
126136
for _, a := range conf.ResourceAttributes {
@@ -326,7 +336,9 @@ func (o Provider) Log(ctx context.Context, name string, fields ...o11y.Pair) {
326336

327337
func (o Provider) Close(ctx context.Context) {
328338
// TODO Handle these errors in a sensible manner where possible
329-
_ = o.tp.Shutdown(ctx)
339+
if o.tp != nil {
340+
_ = o.tp.Shutdown(ctx)
341+
}
330342
if o.metricsProvider != nil {
331343
_ = o.metricsProvider.Close()
332344
}

0 commit comments

Comments
 (0)