diff --git a/.chloggen/main.yaml b/.chloggen/main.yaml new file mode 100644 index 00000000000..065fbad3d1c --- /dev/null +++ b/.chloggen/main.yaml @@ -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: "pkg/service" + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Add support to disabling adding resource attributes as zap fields in internal logging" + +# One or more tracking issues or pull requests related to the change +issues: [13869] + +# (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: + +# 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: [] diff --git a/service/telemetry/internal/migration/v0.3.0.go b/service/telemetry/internal/migration/v0.3.0.go index 6a1415cc753..b0bc51a81b5 100644 --- a/service/telemetry/internal/migration/v0.3.0.go +++ b/service/telemetry/internal/migration/v0.3.0.go @@ -162,6 +162,9 @@ type LogsConfigV030 struct { // Processors allow configuration of log record processors to emit logs to // any number of supported backends. Processors []config.LogRecordProcessor `mapstructure:"processors,omitempty"` + + // DisableResourceAttributes disables adding resource attributes to logs. + DisableResourceAttributes bool `mapstructure:"disable_resource_attributes,omitempty"` } // LogsSamplingConfig sets a sampling strategy for the logger. Sampling caps the diff --git a/service/telemetry/otelconftelemetry/factory.go b/service/telemetry/otelconftelemetry/factory.go index bffadf6be9d..ebd915a0f63 100644 --- a/service/telemetry/otelconftelemetry/factory.go +++ b/service/telemetry/otelconftelemetry/factory.go @@ -51,11 +51,12 @@ func createDefaultConfig() component.Config { Initial: 10, Thereafter: 100, }, - OutputPaths: []string{"stderr"}, - ErrorOutputPaths: []string{"stderr"}, - DisableCaller: false, - DisableStacktrace: false, - InitialFields: map[string]any(nil), + OutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{"stderr"}, + DisableCaller: false, + DisableStacktrace: false, + InitialFields: map[string]any(nil), + DisableResourceAttributes: false, }, Metrics: MetricsConfig{ Level: configtelemetry.LevelNormal, diff --git a/service/telemetry/otelconftelemetry/logger.go b/service/telemetry/otelconftelemetry/logger.go index b53a6d16b9c..ae3807a9e56 100644 --- a/service/telemetry/otelconftelemetry/logger.go +++ b/service/telemetry/otelconftelemetry/logger.go @@ -57,7 +57,7 @@ func createLogger( // add them to the logger using With, because that would apply to all logs, // even ones exported through the core that wraps the LoggerProvider, // meaning that the attributes would be exported twice. - if res != nil && len(res.Attributes()) > 0 { + if !cfg.Logs.DisableResourceAttributes && res != nil && len(res.Attributes()) > 0 { logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { var fields []zap.Field for _, attr := range res.Attributes() { diff --git a/service/telemetry/otelconftelemetry/logger_test.go b/service/telemetry/otelconftelemetry/logger_test.go index 98697b53f0f..78e1fbec776 100644 --- a/service/telemetry/otelconftelemetry/logger_test.go +++ b/service/telemetry/otelconftelemetry/logger_test.go @@ -129,6 +129,22 @@ func TestCreateLogger(t *testing.T) { }, }, }, + { + name: "log config with `disable_resource_attributes` enabled", + cfg: Config{ + Logs: LogsConfig{ + Level: zapcore.InfoLevel, + Development: false, + Encoding: "console", + OutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{"stderr"}, + DisableCaller: false, + DisableStacktrace: false, + InitialFields: map[string]any(nil), + DisableResourceAttributes: true, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -156,6 +172,7 @@ func TestCreateLoggerWithResource(t *testing.T) { buildInfo component.BuildInfo resourceConfig map[string]*string wantFields map[string]string + setConfig func(cfg *Config) }{ { name: "auto-populated fields only", @@ -225,11 +242,24 @@ func TestCreateLoggerWithResource(t *testing.T) { string(semconv.ServiceInstanceIDKey): "", // Just check presence }, }, + { + name: "validate `DisableResourceAttributes=true` shouldn't add resource fields", + buildInfo: component.BuildInfo{ + Command: "mycommand", + Version: "1.0.0", + }, + resourceConfig: map[string]*string{}, + wantFields: map[string]string{}, + setConfig: func(cfg *Config) { + cfg.Logs.DisableResourceAttributes = true + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { core, observedLogs := observer.New(zapcore.DebugLevel) + cfg := &Config{ Logs: LogsConfig{ Level: zapcore.InfoLevel, @@ -237,6 +267,9 @@ func TestCreateLoggerWithResource(t *testing.T) { }, Resource: tt.resourceConfig, } + if tt.setConfig != nil { + tt.setConfig(cfg) + } resource, err := createResource(t.Context(), telemetry.Settings{BuildInfo: tt.buildInfo}, cfg) require.NoError(t, err) @@ -259,7 +292,8 @@ func TestCreateLoggerWithResource(t *testing.T) { require.Len(t, observedLogs.All(), 1) entry := observedLogs.All()[0] - if tt.wantFields == nil { + // treat empty map as "no expected fields" + if len(tt.wantFields) == 0 { assert.Empty(t, entry.Context) return }