Skip to content

Commit 83e0050

Browse files
committed
chore: cleanup config
1 parent b2d3fe3 commit 83e0050

3 files changed

Lines changed: 61 additions & 45 deletions

File tree

uptrace/config.go

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1617
type 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

3738
func 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

5155
func (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

6266
func 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.
9398
func 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.
100105
func 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`.
108113
func 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 {
117122
func 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.
140145
func 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

161166
func (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.
172182
func 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.
179189
func 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.
187197
func 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.
195205
func 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

216226
func (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+
}

uptrace/tracing.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
)
1818

1919
func configureTracing(ctx context.Context, client *client, cfg *config) {
20-
provider := cfg.TracerProvider
20+
provider := cfg.tracerProvider
2121
if provider == nil {
2222
var opts []sdktrace.TracerProviderOption
2323

2424
if res := cfg.newResource(); res != nil {
2525
opts = append(opts, sdktrace.WithResource(res))
2626
}
27-
if cfg.TraceSampler != nil {
28-
opts = append(opts, sdktrace.WithSampler(cfg.TraceSampler))
27+
if cfg.traceSampler != nil {
28+
opts = append(opts, sdktrace.WithSampler(cfg.traceSampler))
2929
}
3030

3131
provider = sdktrace.NewTracerProvider(opts...)
@@ -46,7 +46,7 @@ func configureTracing(ctx context.Context, client *client, cfg *config) {
4646
)
4747
provider.RegisterSpanProcessor(bsp)
4848

49-
if cfg.PrettyPrint {
49+
if cfg.prettyPrint {
5050
exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
5151
if err != nil {
5252
internal.Logger.Printf(err.Error())

uptrace/uptrace.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func ConfigureOpentelemetry(opts ...Option) {
2727
ctx := context.TODO()
2828
cfg := newConfig(opts)
2929

30-
if cfg.TracingDisabled && cfg.MetricsDisabled {
30+
if !cfg.tracingEnabled && cfg.metricsEnabled {
3131
return
3232
}
3333

34-
dsn, err := internal.ParseDSN(cfg.DSN)
34+
dsn, err := internal.ParseDSN(cfg.dsn)
3535
if err != nil {
3636
internal.Logger.Printf("uptrace is disabled: %s", err)
3737
return
@@ -40,18 +40,18 @@ func ConfigureOpentelemetry(opts ...Option) {
4040
client := newClient(dsn)
4141

4242
configurePropagator(cfg)
43-
if !cfg.TracingDisabled {
43+
if cfg.tracingEnabled {
4444
configureTracing(ctx, client, cfg)
4545
}
46-
if !cfg.MetricsDisabled {
46+
if cfg.metricsEnabled {
4747
configureMetrics(ctx, client, cfg)
4848
}
4949

5050
atomicClient.Store(client)
5151
}
5252

5353
func configurePropagator(cfg *config) {
54-
textMapPropagator := cfg.TextMapPropagator
54+
textMapPropagator := cfg.textMapPropagator
5555
if textMapPropagator == nil {
5656
textMapPropagator = propagation.NewCompositeTextMapPropagator(
5757
propagation.TraceContext{},
@@ -62,6 +62,7 @@ func configurePropagator(cfg *config) {
6262
}
6363

6464
//------------------------------------------------------------------------------
65+
6566
var (
6667
fallbackClient = newClient(&internal.DSN{
6768
ProjectID: "<project_id>",

0 commit comments

Comments
 (0)