Skip to content

Commit 033e1b4

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 8d971b7 commit 033e1b4

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

config/o11y/otel.go

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

6464
// Override the default writer for text span output
6565
Writer io.Writer
66+
67+
// ProviderFunc is used to provide a custom provider.
68+
ProviderFunc func(conf otel.Config) (o11y.Provider, error)
6669
}
6770

6871
// Otel is the primary entrypoint to initialize the o11y system for otel.
@@ -77,7 +80,10 @@ func Otel(ctx context.Context, o OtelConfig) (context.Context, func(context.Cont
7780
}
7881
cfg.Metrics = mProv
7982

80-
o11yProvider, err := otel.New(cfg)
83+
if o.ProviderFunc == nil {
84+
o.ProviderFunc = otel.New
85+
}
86+
o11yProvider, err := o.ProviderFunc(cfg)
8187
if err != nil {
8288
return ctx, nil, err
8389
}

go.mod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ require (
7575
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
7676
github.com/facebookgo/limitgroup v0.0.0-20150612190941-6abd8d71ec01 // indirect
7777
github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52 // indirect
78-
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
78+
github.com/gabriel-vasile/mimetype v1.4.10 // indirect
7979
github.com/gin-contrib/sse v1.1.0 // indirect
8080
github.com/go-jose/go-jose/v4 v4.1.3 // indirect
8181
github.com/go-logr/logr v1.4.3 // indirect
8282
github.com/go-logr/stdr v1.2.2 // indirect
8383
github.com/go-playground/locales v0.14.1 // indirect
8484
github.com/go-playground/universal-translator v0.18.1 // indirect
85-
github.com/go-playground/validator/v10 v10.27.0 // indirect
86-
github.com/goccy/go-json v0.10.2 // indirect
85+
github.com/go-playground/validator/v10 v10.28.0 // indirect
86+
github.com/goccy/go-json v0.10.5 // indirect
8787
github.com/goccy/go-yaml v1.18.0 // indirect
8888
github.com/golang/snappy v1.0.0 // indirect
8989
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 // indirect
@@ -135,3 +135,5 @@ require (
135135
google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8 // indirect
136136
gopkg.in/alexcesaro/statsd.v2 v2.0.0 // indirect
137137
)
138+
139+
replace github.com/circleci/backplane-go-x => /Users/danmux/Developer/code/github.com/circleci/backplane-go-x

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
9393
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
9494
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
9595
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
96-
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
97-
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
96+
github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0=
97+
github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
9898
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
9999
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
100100
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
@@ -112,15 +112,15 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
112112
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
113113
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
114114
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
115-
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
116-
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
115+
github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0ktULL6FgHdG688=
116+
github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU=
117117
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
118118
github.com/go-sql-driver/mysql v1.9.1 h1:FrjNGn/BsJQjVRuSa8CBrM5BWA9BWoXXat3KrtSb/iI=
119119
github.com/go-sql-driver/mysql v1.9.1/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
120120
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
121121
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
122-
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
123-
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
122+
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
123+
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
124124
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
125125
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
126126
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=

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
@@ -21,6 +21,7 @@ import (
2121
sdktrace "go.opentelemetry.io/otel/sdk/trace"
2222
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
2323
"go.opentelemetry.io/otel/trace"
24+
"go.opentelemetry.io/otel/trace/noop"
2425

2526
"github.com/circleci/ex/config/secret"
2627
"github.com/circleci/ex/o11y"
@@ -117,6 +118,15 @@ func New(conf Config) (o11y.Provider, error) {
117118
}, nil
118119
}
119120

121+
// NewMetricsOnly returns a metrics only provider, to capture the span metrics behavior.
122+
// This can be used to compose with a custom provider to access span based metrics.
123+
func NewMetricsOnly(metrics o11y.ClosableMetricsProvider) o11y.Provider {
124+
return &Provider{
125+
metricsProvider: metrics,
126+
tracer: noop.NewTracerProvider().Tracer(""),
127+
}
128+
}
129+
120130
func newHttpExporter(conf Config) (*otlptrace.Exporter, error) {
121131
var serviceName, serviceVersion string
122132
for _, a := range conf.ResourceAttributes {
@@ -322,7 +332,9 @@ func (o Provider) Log(ctx context.Context, name string, fields ...o11y.Pair) {
322332

323333
func (o Provider) Close(ctx context.Context) {
324334
// TODO Handle these errors in a sensible manner where possible
325-
_ = o.tp.Shutdown(ctx)
335+
if o.tp != nil {
336+
_ = o.tp.Shutdown(ctx)
337+
}
326338
if o.metricsProvider != nil {
327339
_ = o.metricsProvider.Close()
328340
}

0 commit comments

Comments
 (0)