Skip to content

Commit

Permalink
[service] add level for trace configuration
Browse files Browse the repository at this point in the history
This allows users to configure a Noop TracerProvider if needed. Note that this
configuration option breaks the zpages extension, which relies on a SDK implementation.

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten committed Aug 16, 2024
1 parent 9fd8466 commit 0c62d38
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
25 changes: 25 additions & 0 deletions .chloggen/codeboten_trace-level.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: service

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Adds `level` configuration option to `service::telemetry::trace` to allow users to disable the default TracerProvider"

# One or more tracking issues or pull requests related to the change
issues: [10892]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: "This replaces the feature gate `service.noopTracerProvider` introduced in v0.107.0"

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
1 change: 1 addition & 0 deletions internal/globalgates/globalgates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ var DisableOpenCensusBridge = featuregate.GlobalRegistry().MustRegister("service
var NoopTracerProvider = featuregate.GlobalRegistry().MustRegister("service.noopTracerProvider",
featuregate.StageAlpha,
featuregate.WithRegisterFromVersion("v0.107.0"),
featuregate.WithRegisterToVersion("v0.109.0"),
featuregate.WithRegisterDescription("Sets a Noop OpenTelemetry TracerProvider to reduce memory allocations. This featuregate is incompatible with the zPages extension."))
4 changes: 4 additions & 0 deletions service/telemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ type MetricsConfig struct {
// TracesConfig exposes the common Telemetry configuration for collector's internal spans.
// Experimental: *NOTE* this structure is subject to change or removal in the future.
type TracesConfig struct {
// Level configures whether spans are emitted or not, the possible values are:
// - "none" indicates that no tracing data should be collected;
// - "basic" is the recommended and covers the basics of the service telemetry.
Level configtelemetry.Level `mapstructure:"level"`
// Propagators is a list of TextMapPropagators from the supported propagators list. Currently,
// tracecontext and b3 are supported. By default, the value is set to empty list and
// context propagation is disabled.
Expand Down
4 changes: 3 additions & 1 deletion service/telemetry/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"

"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/globalgates"
)

Expand Down Expand Up @@ -48,9 +49,10 @@ func attributes(set Settings, cfg Config) map[string]interface{} {

// New creates a new Telemetry from Config.
func newTracerProvider(ctx context.Context, set Settings, cfg Config) (trace.TracerProvider, error) {
if globalgates.NoopTracerProvider.IsEnabled() {
if globalgates.NoopTracerProvider.IsEnabled() || cfg.Traces.Level == configtelemetry.LevelNone {
return noop.NewTracerProvider(), nil
}

sch := semconv.SchemaURL
res := config.Resource{
SchemaUrl: &sch,
Expand Down
27 changes: 19 additions & 8 deletions service/telemetry/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/otel/trace/noop"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/internal/globalgates"
"go.opentelemetry.io/collector/service/telemetry/internal"
Expand Down Expand Up @@ -65,11 +66,22 @@ func TestNewTracerProvider(t *testing.T) {
tests := []struct {
name string
wantTracerProvider any
noopTracer bool
noopTracerGate bool
cfg Config
}{
{
name: "noop tracer provider",
noopTracer: true,
name: "trace level none",
cfg: Config{
Traces: TracesConfig{
Level: configtelemetry.LevelNone,
},
},
wantTracerProvider: noop.TracerProvider{},
},
{
name: "noop tracer feature gate",
cfg: Config{},
noopTracerGate: true,
wantTracerProvider: noop.TracerProvider{},
},
{
Expand All @@ -80,13 +92,12 @@ func TestNewTracerProvider(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
previousValue := globalgates.NoopTracerProvider.IsEnabled()
err := featuregate.GlobalRegistry().Set(globalgates.NoopTracerProvider.ID(), tt.noopTracer)
require.NoError(t, err)
// expect error due to deprecated flag
require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.NoopTracerProvider.ID(), tt.noopTracerGate))
defer func() {
err = featuregate.GlobalRegistry().Set(globalgates.NoopTracerProvider.ID(), previousValue)
require.NoError(t, err)
require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.NoopTracerProvider.ID(), previousValue))
}()
provider, err := newTracerProvider(context.TODO(), internal.Settings{}, Config{})
provider, err := newTracerProvider(context.TODO(), internal.Settings{}, tt.cfg)
require.NoError(t, err)
require.IsType(t, tt.wantTracerProvider, provider)
})
Expand Down

0 comments on commit 0c62d38

Please sign in to comment.