Skip to content

Commit 3f0fcf8

Browse files
committed
Re-enable zap fileds for the default service configuration
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 093b1e9 commit 3f0fcf8

File tree

7 files changed

+135
-0
lines changed

7 files changed

+135
-0
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: telemetry
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Re-enable zap fileds for the default service configuration
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12869]
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+
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: [user]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package componentattribute // import "go.opentelemetry.io/collector/internal/telemetry/componentattribute"
5+
6+
import (
7+
"go.opentelemetry.io/otel/attribute"
8+
"go.uber.org/zap"
9+
"go.uber.org/zap/zapcore"
10+
)
11+
12+
var _ zapcore.Core = (*coreWithout)(nil)
13+
14+
type coreWithout struct {
15+
zapcore.Core
16+
from zapcore.Core
17+
fields []zap.Field
18+
}
19+
20+
func NewLogger(logger *zap.Logger, attrs *attribute.Set) *zap.Logger {
21+
var fields []zap.Field
22+
for _, kv := range attrs.ToSlice() {
23+
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
24+
}
25+
return logger.WithOptions(
26+
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
27+
return &coreWithout{Core: core.With(fields), from: core, fields: fields}
28+
}),
29+
)
30+
}
31+
32+
func (l *coreWithout) Without(keys ...string) zapcore.Core {
33+
excludeKeys := make(map[string]struct{})
34+
for _, key := range keys {
35+
excludeKeys[key] = struct{}{}
36+
}
37+
38+
var fieldsWithout []zap.Field
39+
for _, field := range l.fields {
40+
if _, excluded := excludeKeys[field.Key]; !excluded {
41+
fieldsWithout = append(fieldsWithout, field)
42+
}
43+
}
44+
45+
return &coreWithout{Core: l.from.With(fieldsWithout), from: l.from, fields: fieldsWithout}
46+
}

internal/telemetry/componentattribute/logger_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,55 @@ func TestCore(t *testing.T) {
132132
})
133133
}
134134
}
135+
136+
type loggerCore interface {
137+
Without(fields ...string) zapcore.Core
138+
}
139+
140+
func TestLegacyCore(t *testing.T) {
141+
core, observed := observer.New(zap.DebugLevel)
142+
logger := zap.New(core).With(zap.String("preexisting", "value"))
143+
144+
attrs := attribute.NewSet(
145+
attribute.String(componentattribute.SignalKey, pipeline.SignalLogs.String()),
146+
attribute.String(componentattribute.ComponentIDKey, "filelog"),
147+
)
148+
149+
parent := componentattribute.NewLogger(logger, &attrs)
150+
parent.Info("test parent before child")
151+
childCore := parent.Core().(loggerCore).Without(string(componentattribute.SignalKey))
152+
child := zap.New(childCore)
153+
child.Info("test child")
154+
parent.Info("test parent after child")
155+
156+
observedLogs := observed.All()
157+
require.Len(t, observedLogs, 3)
158+
159+
parentContext := map[string]string{
160+
"preexisting": "value",
161+
componentattribute.SignalKey: pipeline.SignalLogs.String(),
162+
componentattribute.ComponentIDKey: "filelog",
163+
}
164+
childContext := map[string]string{
165+
"preexisting": "value",
166+
componentattribute.ComponentIDKey: "filelog",
167+
}
168+
169+
require.Equal(t, "test parent before child", observedLogs[0].Message)
170+
require.Len(t, observedLogs[0].Context, len(parentContext))
171+
for _, field := range observedLogs[0].Context {
172+
require.Equal(t, parentContext[field.Key], field.String)
173+
}
174+
175+
require.Equal(t, "test child", observedLogs[1].Message)
176+
require.Len(t, observedLogs[1].Context, len(childContext))
177+
for _, field := range observedLogs[1].Context {
178+
require.Equal(t, childContext[field.Key], field.String)
179+
}
180+
181+
require.Equal(t, "test parent after child", observedLogs[2].Message)
182+
require.Len(t, observedLogs[2].Context, len(parentContext))
183+
for _, field := range observedLogs[2].Context {
184+
require.Equal(t, parentContext[field.Key], field.String)
185+
}
186+
}

service/internal/graph/connector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.opentelemetry.io/collector/consumer"
1313
"go.opentelemetry.io/collector/consumer/xconsumer"
1414
"go.opentelemetry.io/collector/internal/telemetry"
15+
"go.opentelemetry.io/collector/internal/telemetry/componentattribute"
1516
"go.opentelemetry.io/collector/pipeline"
1617
"go.opentelemetry.io/collector/pipeline/xpipeline"
1718
"go.opentelemetry.io/collector/service/internal/attribute"
@@ -52,6 +53,8 @@ func (n *connectorNode) buildComponent(
5253
set := connector.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
5354
if telemetry.NewPipelineTelemetryGate.IsEnabled() {
5455
set.TelemetrySettings = telemetry.WithAttributeSet(set.TelemetrySettings, *n.Set())
56+
} else {
57+
set.Logger = componentattribute.NewLogger(tel.Logger, n.Set())
5558
}
5659
switch n.rcvrPipelineType {
5760
case pipeline.SignalTraces:

service/internal/graph/exporter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go.opentelemetry.io/collector/component"
1111
"go.opentelemetry.io/collector/exporter"
1212
"go.opentelemetry.io/collector/internal/telemetry"
13+
"go.opentelemetry.io/collector/internal/telemetry/componentattribute"
1314
"go.opentelemetry.io/collector/pipeline"
1415
"go.opentelemetry.io/collector/pipeline/xpipeline"
1516
"go.opentelemetry.io/collector/service/internal/attribute"
@@ -48,6 +49,8 @@ func (n *exporterNode) buildComponent(
4849
set := exporter.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
4950
if telemetry.NewPipelineTelemetryGate.IsEnabled() {
5051
set.TelemetrySettings = telemetry.WithAttributeSet(set.TelemetrySettings, *n.Set())
52+
} else {
53+
set.Logger = componentattribute.NewLogger(tel.Logger, n.Set())
5154
}
5255
var err error
5356
switch n.pipelineType {

service/internal/graph/processor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"go.opentelemetry.io/collector/consumer"
1212
"go.opentelemetry.io/collector/consumer/xconsumer"
1313
"go.opentelemetry.io/collector/internal/telemetry"
14+
"go.opentelemetry.io/collector/internal/telemetry/componentattribute"
1415
"go.opentelemetry.io/collector/pipeline"
1516
"go.opentelemetry.io/collector/pipeline/xpipeline"
1617
"go.opentelemetry.io/collector/processor"
@@ -50,6 +51,8 @@ func (n *processorNode) buildComponent(ctx context.Context,
5051
set := processor.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
5152
if telemetry.NewPipelineTelemetryGate.IsEnabled() {
5253
set.TelemetrySettings = telemetry.WithAttributeSet(set.TelemetrySettings, *n.Set())
54+
} else {
55+
set.Logger = componentattribute.NewLogger(tel.Logger, n.Set())
5356
}
5457
var err error
5558
switch n.pipelineID.Signal() {

service/internal/graph/receiver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.opentelemetry.io/collector/consumer/xconsumer"
1313
"go.opentelemetry.io/collector/internal/fanoutconsumer"
1414
"go.opentelemetry.io/collector/internal/telemetry"
15+
"go.opentelemetry.io/collector/internal/telemetry/componentattribute"
1516
"go.opentelemetry.io/collector/pipeline"
1617
"go.opentelemetry.io/collector/pipeline/xpipeline"
1718
"go.opentelemetry.io/collector/receiver"
@@ -45,6 +46,8 @@ func (n *receiverNode) buildComponent(ctx context.Context,
4546
set := receiver.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
4647
if telemetry.NewPipelineTelemetryGate.IsEnabled() {
4748
set.TelemetrySettings = telemetry.WithAttributeSet(set.TelemetrySettings, *n.Set())
49+
} else {
50+
set.Logger = componentattribute.NewLogger(tel.Logger, n.Set())
4851
}
4952
var err error
5053
switch n.pipelineType {

0 commit comments

Comments
 (0)