@@ -6,39 +6,43 @@ import (
66
77 "github.com/uptrace/uptrace-go/internal"
88
9+ "go.opentelemetry.io/otel"
910 "go.opentelemetry.io/otel/attribute"
1011 "go.opentelemetry.io/otel/propagation"
1112 "go.opentelemetry.io/otel/sdk/resource"
1213 sdktrace "go.opentelemetry.io/otel/sdk/trace"
13- semconv "go.opentelemetry.io/otel/semconv/v1.4 .0"
14+ semconv "go.opentelemetry.io/otel/semconv/v1.7 .0"
1415)
1516
1617type config struct {
17- DSN string
18+ dsn string
1819
1920 // Common options
2021
21- ResourceAttributes []attribute.KeyValue
22- Resource * resource.Resource
22+ resourceAttributes []attribute.KeyValue
23+ resource * resource.Resource
2324
2425 // Tracing options
2526
26- TracingDisabled bool
27- TextMapPropagator propagation.TextMapPropagator
28- TracerProvider * sdktrace.TracerProvider
29- TraceSampler sdktrace.Sampler
30- PrettyPrint bool
27+ tracingEnabled bool
28+ textMapPropagator propagation.TextMapPropagator
29+ tracerProvider * sdktrace.TracerProvider
30+ traceSampler sdktrace.Sampler
31+ prettyPrint bool
3132
3233 // Metrics options
3334
34- MetricsDisabled bool
35+ metricsEnabled bool
3536}
3637
3738func newConfig (opts []Option ) * config {
38- cfg := new (config )
39+ cfg := & config {
40+ tracingEnabled : true ,
41+ metricsEnabled : true ,
42+ }
3943
4044 if dsn , ok := os .LookupEnv ("UPTRACE_DSN" ); ok {
41- cfg .DSN = dsn
45+ cfg .dsn = dsn
4246 }
4347
4448 for _ , opt := range opts {
@@ -49,26 +53,27 @@ func newConfig(opts []Option) *config {
4953}
5054
5155func (cfg * config ) newResource () * resource.Resource {
52- if cfg .Resource != nil {
53- if len (cfg .ResourceAttributes ) > 0 {
56+ if cfg .resource != nil {
57+ if len (cfg .resourceAttributes ) > 0 {
5458 internal .Logger .Printf ("WithResource is used with other resource options (discarding %v)" ,
55- cfg .ResourceAttributes )
59+ cfg .resourceAttributes )
5660 }
57- return cfg .Resource
61+ return cfg .resource
5862 }
59- return buildResource (cfg .ResourceAttributes )
63+ return buildResource (cfg .resourceAttributes )
6064}
6165
6266func buildResource (attrs []attribute.KeyValue ) * resource.Resource {
6367 ctx := context .TODO ()
6468
65- res , _ := resource .New (ctx ,
69+ res , err := resource .New (ctx ,
6670 resource .WithFromEnv (),
6771 resource .WithTelemetrySDK (),
6872 resource .WithHost (),
6973 resource .WithSchemaURL (semconv .SchemaURL ),
7074 resource .WithAttributes (attrs ... ))
71- if res == nil {
75+ if err != nil {
76+ otel .Handle (err )
7277 return resource .Environment ()
7378 }
7479 return res
@@ -92,23 +97,23 @@ func (fn option) apply(cfg *config) {
9297// The default is to use UPTRACE_DSN environment variable.
9398func WithDSN (dsn string ) Option {
9499 return option (func (cfg * config ) {
95- cfg .DSN = dsn
100+ cfg .dsn = dsn
96101 })
97102}
98103
99104// WithServiceVersion configures `service.name` resource attribute.
100105func WithServiceName (serviceName string ) Option {
101106 return option (func (cfg * config ) {
102107 attr := semconv .ServiceNameKey .String (serviceName )
103- cfg .ResourceAttributes = append (cfg .ResourceAttributes , attr )
108+ cfg .resourceAttributes = append (cfg .resourceAttributes , attr )
104109 })
105110}
106111
107112// WithServiceVersion configures `service.version` resource attribute, for example, `1.0.0`.
108113func WithServiceVersion (serviceVersion string ) Option {
109114 return option (func (cfg * config ) {
110115 attr := semconv .ServiceVersionKey .String (serviceVersion )
111- cfg .ResourceAttributes = append (cfg .ResourceAttributes , attr )
116+ cfg .resourceAttributes = append (cfg .resourceAttributes , attr )
112117 })
113118}
114119
@@ -117,7 +122,7 @@ func WithServiceVersion(serviceVersion string) Option {
117122func WithDeploymentEnvironment (env string ) Option {
118123 return option (func (cfg * config ) {
119124 attr := semconv .DeploymentEnvironmentKey .String (env )
120- cfg .ResourceAttributes = append (cfg .ResourceAttributes , attr )
125+ cfg .resourceAttributes = append (cfg .resourceAttributes , attr )
121126 })
122127}
123128
@@ -126,9 +131,9 @@ func WithDeploymentEnvironment(env string) Option {
126131//
127132// The default is to use `OTEL_RESOURCE_ATTRIBUTES` env var, for example,
128133// `OTEL_RESOURCE_ATTRIBUTES=service.name=myservice,service.version=1.0.0`.
129- func WithResourceAttributes (resourceAttributes []attribute.KeyValue ) Option {
134+ func WithResourceAttributes (attrs []attribute.KeyValue ) Option {
130135 return option (func (cfg * config ) {
131- cfg .ResourceAttributes = resourceAttributes
136+ cfg .resourceAttributes = append ( cfg . resourceAttributes , attrs ... )
132137 })
133138}
134139
@@ -139,7 +144,7 @@ func WithResourceAttributes(resourceAttributes []attribute.KeyValue) Option {
139144// WithResource overrides and replaces any other resource attributes.
140145func WithResource (resource * resource.Resource ) Option {
141146 return option (func (cfg * config ) {
142- cfg .Resource = resource
147+ cfg .resource = resource
143148 })
144149}
145150
@@ -160,41 +165,46 @@ func (fn tracingOption) apply(cfg *config) {
160165
161166func (fn tracingOption ) tracing () {}
162167
163- // TracingDisabled can be used to skip tracing configuration .
164- func WithTracingDisabled ( ) TracingOption {
168+ // WithTracingEnabled can be used to enable/disable tracing.
169+ func WithTracingEnabled ( on bool ) TracingOption {
165170 return tracingOption (func (cfg * config ) {
166- cfg .TracingDisabled = true
171+ cfg .tracingEnabled = on
167172 })
168173}
169174
175+ // WithTracingDisabled disables tracing.
176+ func WithTracingDisabled () TracingOption {
177+ return WithTracingEnabled (false )
178+ }
179+
170180// TracerProvider overwrites the default Uptrace tracer provider.
171181// You can use it to configure Uptrace distro to use OTLP exporter.
172182func WithTracerProvider (provider * sdktrace.TracerProvider ) TracingOption {
173183 return tracingOption (func (cfg * config ) {
174- cfg .TracerProvider = provider
184+ cfg .tracerProvider = provider
175185 })
176186}
177187
178188// WithTraceSampler configures a span sampler.
179189func WithTraceSampler (sampler sdktrace.Sampler ) TracingOption {
180190 return tracingOption (func (cfg * config ) {
181- cfg .TraceSampler = sampler
191+ cfg .traceSampler = sampler
182192 })
183193}
184194
185195// WithTextMapPropagator sets the global TextMapPropagator used by OpenTelemetry.
186196// The default is propagation.TraceContext and propagation.Baggage.
187197func WithTextMapPropagator (propagator propagation.TextMapPropagator ) TracingOption {
188198 return tracingOption (func (cfg * config ) {
189- cfg .TextMapPropagator = propagator
199+ cfg .textMapPropagator = propagator
190200 })
191201}
192202
193203// WithPrettyPrintSpanExporter adds a span exproter that prints spans to stdout.
194204// It is useful for debugging or demonstration purposes.
195205func WithPrettyPrintSpanExporter () TracingOption {
196206 return tracingOption (func (cfg * config ) {
197- cfg .PrettyPrint = true
207+ cfg .prettyPrint = true
198208 })
199209}
200210
@@ -215,9 +225,14 @@ func (fn metricsOption) apply(cfg *config) {
215225
216226func (fn metricsOption ) metrics () {}
217227
218- // WithMetricsDisabled can be used to skip metrics configuration .
219- func WithMetricsDisabled ( ) MetricsOption {
228+ // WithMetricsEnabled can be used to enable/disable metrics.
229+ func WithMetricsEnabled ( on bool ) MetricsOption {
220230 return metricsOption (func (cfg * config ) {
221- cfg .MetricsDisabled = true
231+ cfg .metricsEnabled = on
222232 })
223233}
234+
235+ // WithMetricsDisabled disables metrics.
236+ func WithMetricsDisabled () MetricsOption {
237+ return WithMetricsEnabled (false )
238+ }
0 commit comments