Skip to content

Commit 52007de

Browse files
yaten2302jade-guiton-ddcodeboten
authored
Add option to disable redundant 132B in log lines (#14050)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR adds an option to disable the resource attribute in logs. <!-- Issue number if applicable --> #### Link to tracking issue Fixes #13869 <!--Describe what testing was performed and which tests were added.--> #### Testing Added unit tests in - `TestCreateLogger()`. <!--Please delete paragraphs that you did not use before submitting.--> --------- Signed-off-by: Yaten <[email protected]> Co-authored-by: Jade Guiton <[email protected]> Co-authored-by: Alex Boten <[email protected]>
1 parent 1197fba commit 52007de

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

.chloggen/main.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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: "enhancement"
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: "pkg/service"
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Add support to disabling adding resource attributes as zap fields in internal logging"
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13869]
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: |
19+
Note that this does not affect logs exported through OTLP.
20+
21+
# Optional: The change log or logs in which this entry should be included.
22+
# e.g. '[user]' or '[user, api]'
23+
# Include 'user' if the change is relevant to end users.
24+
# Include 'api' if there is a change to a library API.
25+
# Default: '[user]'
26+
change_logs: []

service/telemetry/internal/migration/v0.3.0.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ type LogsConfigV030 struct {
162162
// Processors allow configuration of log record processors to emit logs to
163163
// any number of supported backends.
164164
Processors []config.LogRecordProcessor `mapstructure:"processors,omitempty"`
165+
166+
// DisableZapResource disables adding resource attributes to logs exported through Zap. This does not affect logs exported through OTLP.
167+
DisableZapResource bool `mapstructure:"disable_zap_resource,omitempty"`
165168
}
166169

167170
// LogsSamplingConfig sets a sampling strategy for the logger. Sampling caps the

service/telemetry/otelconftelemetry/factory.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ func createDefaultConfig() component.Config {
5151
Initial: 10,
5252
Thereafter: 100,
5353
},
54-
OutputPaths: []string{"stderr"},
55-
ErrorOutputPaths: []string{"stderr"},
56-
DisableCaller: false,
57-
DisableStacktrace: false,
58-
InitialFields: map[string]any(nil),
54+
OutputPaths: []string{"stderr"},
55+
ErrorOutputPaths: []string{"stderr"},
56+
DisableCaller: false,
57+
DisableStacktrace: false,
58+
InitialFields: map[string]any(nil),
59+
DisableZapResource: false,
5960
},
6061
Metrics: MetricsConfig{
6162
Level: configtelemetry.LevelNormal,

service/telemetry/otelconftelemetry/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func createLogger(
6363
// add them to the logger using With, because that would apply to all logs,
6464
// even ones exported through the core that wraps the LoggerProvider,
6565
// meaning that the attributes would be exported twice.
66-
if res != nil && len(res.Attributes()) > 0 {
66+
if !cfg.Logs.DisableZapResource && res != nil && len(res.Attributes()) > 0 {
6767
logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
6868
var fields []zap.Field
6969
for _, attr := range res.Attributes() {

service/telemetry/otelconftelemetry/logger_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ func TestCreateLogger(t *testing.T) {
129129
},
130130
},
131131
},
132+
{
133+
name: "log config with `disable_resource_attributes` enabled",
134+
cfg: Config{
135+
Logs: LogsConfig{
136+
Level: zapcore.InfoLevel,
137+
Development: false,
138+
Encoding: "console",
139+
OutputPaths: []string{"stderr"},
140+
ErrorOutputPaths: []string{"stderr"},
141+
DisableCaller: false,
142+
DisableStacktrace: false,
143+
InitialFields: map[string]any(nil),
144+
DisableZapResource: true,
145+
},
146+
},
147+
},
132148
}
133149
for _, tt := range tests {
134150
t.Run(tt.name, func(t *testing.T) {
@@ -159,6 +175,7 @@ func TestCreateLoggerWithResource(t *testing.T) {
159175
buildInfo component.BuildInfo
160176
resourceConfig map[string]*string
161177
wantFields map[string]string
178+
setConfig func(cfg *Config)
162179
}{
163180
{
164181
name: "auto-populated fields only",
@@ -228,18 +245,34 @@ func TestCreateLoggerWithResource(t *testing.T) {
228245
string(semconv.ServiceInstanceIDKey): "", // Just check presence
229246
},
230247
},
248+
{
249+
name: "validate `DisableResourceAttributes=true` shouldn't add resource fields",
250+
buildInfo: component.BuildInfo{
251+
Command: "mycommand",
252+
Version: "1.0.0",
253+
},
254+
resourceConfig: map[string]*string{},
255+
wantFields: map[string]string{},
256+
setConfig: func(cfg *Config) {
257+
cfg.Logs.DisableZapResource = true
258+
},
259+
},
231260
}
232261

233262
for _, tt := range tests {
234263
t.Run(tt.name, func(t *testing.T) {
235264
core, observedLogs := observer.New(zapcore.DebugLevel)
265+
236266
cfg := &Config{
237267
Logs: LogsConfig{
238268
Level: zapcore.InfoLevel,
239269
Encoding: "json",
240270
},
241271
Resource: tt.resourceConfig,
242272
}
273+
if tt.setConfig != nil {
274+
tt.setConfig(cfg)
275+
}
243276

244277
resource, err := createResource(t.Context(), telemetry.Settings{BuildInfo: tt.buildInfo}, cfg)
245278
require.NoError(t, err)
@@ -262,7 +295,8 @@ func TestCreateLoggerWithResource(t *testing.T) {
262295
require.Len(t, observedLogs.All(), 1)
263296

264297
entry := observedLogs.All()[0]
265-
if tt.wantFields == nil {
298+
// treat empty map as "no expected fields"
299+
if len(tt.wantFields) == 0 {
266300
assert.Empty(t, entry.Context)
267301
return
268302
}

0 commit comments

Comments
 (0)