Skip to content

Commit a1f04f1

Browse files
committed
[service] Ensure a noop tracer provider is returned if no processors are configured
Previously the service was returning an instance of a SDK tracer provider regardless of whether there were any processors configured causing resources to be consumed unnecessarily. Fixes open-telemetry#10858 Signed-off-by: Alex Boten <[email protected]>
1 parent 643c17e commit a1f04f1

File tree

3 files changed

+95
-23
lines changed

3 files changed

+95
-23
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: service
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Ensure a noop tracer provider is returned if no processors are configured
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [10858]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: Previously the service was returning an instance of a SDK tracer provider regardless of whether there were any processors configured causing resources to be consumed unnecessarily.
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

service/telemetry/tracer.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,38 @@ func newTracerProvider(ctx context.Context, set Settings, cfg Config) (trace.Tra
5151
Attributes: attributes(set, cfg),
5252
}
5353

54+
var tracerProviderCfg *config.TracerProvider
55+
if len(cfg.Traces.Processors) > 0 {
56+
tracerProviderCfg = &config.TracerProvider{
57+
Processors: cfg.Traces.Processors,
58+
// TODO: once https://github.com/open-telemetry/opentelemetry-configuration/issues/83 is resolved,
59+
// configuration for sampler should be done here via something like the following:
60+
//
61+
// Sampler: &config.Sampler{
62+
// ParentBased: &config.SamplerParentBased{
63+
// LocalParentSampled: &config.Sampler{
64+
// AlwaysOn: config.SamplerAlwaysOn{},
65+
// },
66+
// LocalParentNotSampled: &config.Sampler{
67+
// RecordOnly: config.SamplerRecordOnly{},
68+
// },
69+
// RemoteParentSampled: &config.Sampler{
70+
// AlwaysOn: config.SamplerAlwaysOn{},
71+
// },
72+
// RemoteParentNotSampled: &config.Sampler{
73+
// RecordOnly: config.SamplerRecordOnly{},
74+
// },
75+
// },
76+
// },
77+
}
78+
}
79+
5480
sdk, err := config.NewSDK(
5581
config.WithContext(ctx),
5682
config.WithOpenTelemetryConfiguration(
5783
config.OpenTelemetryConfiguration{
58-
Resource: &res,
59-
TracerProvider: &config.TracerProvider{
60-
Processors: cfg.Traces.Processors,
61-
// TODO: once https://github.com/open-telemetry/opentelemetry-configuration/issues/83 is resolved,
62-
// configuration for sampler should be done here via something like the following:
63-
//
64-
// Sampler: &config.Sampler{
65-
// ParentBased: &config.SamplerParentBased{
66-
// LocalParentSampled: &config.Sampler{
67-
// AlwaysOn: config.SamplerAlwaysOn{},
68-
// },
69-
// LocalParentNotSampled: &config.Sampler{
70-
// RecordOnly: config.SamplerRecordOnly{},
71-
// },
72-
// RemoteParentSampled: &config.Sampler{
73-
// AlwaysOn: config.SamplerAlwaysOn{},
74-
// },
75-
// RemoteParentNotSampled: &config.Sampler{
76-
// RecordOnly: config.SamplerRecordOnly{},
77-
// },
78-
// },
79-
// },
80-
},
84+
Resource: &res,
85+
TracerProvider: tracerProviderCfg,
8186
},
8287
),
8388
)

service/telemetry/tracer_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
package telemetry // import "go.opentelemetry.io/collector/service/telemetry"
55

66
import (
7+
"context"
78
"testing"
89

910
"github.com/stretchr/testify/require"
1011

1112
"go.opentelemetry.io/collector/component"
1213
"go.opentelemetry.io/collector/service/telemetry/internal"
14+
"go.opentelemetry.io/contrib/config"
15+
sdktrace "go.opentelemetry.io/otel/sdk/trace"
16+
"go.opentelemetry.io/otel/trace/noop"
1317
)
1418

1519
func TestAttributes(t *testing.T) {
@@ -56,6 +60,44 @@ func TestAttributes(t *testing.T) {
5660
}
5761
}
5862

63+
func TestNewTracerProvider(t *testing.T) {
64+
tests := []struct {
65+
name string
66+
cfg Config
67+
wantTracerProvider any
68+
}{
69+
{
70+
name: "no processors",
71+
wantTracerProvider: noop.TracerProvider{},
72+
},
73+
{
74+
name: "some processors",
75+
cfg: Config{
76+
Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")},
77+
Traces: TracesConfig{
78+
Processors: []config.SpanProcessor{
79+
{
80+
Simple: &config.SimpleSpanProcessor{
81+
Exporter: config.SpanExporter{
82+
Console: config.Console{},
83+
},
84+
},
85+
},
86+
},
87+
},
88+
},
89+
wantTracerProvider: &sdktrace.TracerProvider{},
90+
},
91+
}
92+
for _, tt := range tests {
93+
t.Run(tt.name, func(t *testing.T) {
94+
provider, err := newTracerProvider(context.TODO(), internal.Settings{}, tt.cfg)
95+
require.NoError(t, err)
96+
require.IsType(t, tt.wantTracerProvider, provider)
97+
})
98+
}
99+
}
100+
59101
func ptr[T any](v T) *T {
60102
return &v
61103
}

0 commit comments

Comments
 (0)