Skip to content

Commit 1f629b7

Browse files
committed
Add TelemetrySettings.LoggerWithout for streamlined usage
1 parent 07a6e5b commit 1f629b7

File tree

5 files changed

+46
-30
lines changed

5 files changed

+46
-30
lines changed

Diff for: component/componentattribute/logger.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,46 @@ import (
99
"go.uber.org/zap/zapcore"
1010
)
1111

12-
var _ zapcore.Core = (*Core)(nil)
12+
var _ zapcore.Core = (*coreWithout)(nil)
1313

14-
type Core struct {
14+
type coreWithout struct {
1515
zapcore.Core
1616
from *zap.Logger
1717
attrs attribute.Set
1818
}
1919

2020
func NewLogger(from *zap.Logger, attrs *attribute.Set) *zap.Logger {
21-
withAttributes := from
21+
fields := []zap.Field{}
2222
for _, kv := range attrs.ToSlice() {
23-
withAttributes = withAttributes.With(zap.String(string(kv.Key), kv.Value.AsString()))
23+
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
2424
}
25-
return zap.New(&Core{
26-
Core: withAttributes.Core(),
27-
from: from,
28-
attrs: *attrs,
29-
})
25+
return from.WithOptions(
26+
zap.Fields(fields...),
27+
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
28+
return &coreWithout{Core: core, from: from, attrs: *attrs}
29+
}),
30+
)
3031
}
3132

32-
func (l *Core) Without(keys ...string) *zap.Logger {
33+
func (l *coreWithout) Without(keys ...string) *zap.Logger {
3334
excludeKeys := make(map[string]struct{})
3435
for _, key := range keys {
3536
excludeKeys[key] = struct{}{}
3637
}
3738

3839
newAttrs := []attribute.KeyValue{}
39-
withAttributes := l.from
40+
fields := []zap.Field{}
4041
for _, kv := range l.attrs.ToSlice() {
4142
if _, excluded := excludeKeys[string(kv.Key)]; !excluded {
4243
newAttrs = append(newAttrs, kv)
43-
withAttributes = withAttributes.With(zap.String(string(kv.Key), kv.Value.AsString()))
44+
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
4445
}
4546
}
4647

47-
return zap.New(&Core{
48-
Core: withAttributes.Core(),
49-
from: l.from,
50-
attrs: attribute.NewSet(newAttrs...),
51-
})
48+
return l.from.WithOptions(
49+
zap.Fields(fields...),
50+
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
51+
return &coreWithout{Core: core, from: l.from, attrs: attribute.NewSet(newAttrs...)}
52+
}),
53+
)
5254
}

Diff for: component/componentattribute/logger_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import (
99
"github.com/stretchr/testify/require"
1010
"go.opentelemetry.io/otel/attribute"
1111
"go.uber.org/zap"
12+
"go.uber.org/zap/zapcore"
1213
"go.uber.org/zap/zaptest/observer"
1314

1415
"go.opentelemetry.io/collector/component/componentattribute"
1516
"go.opentelemetry.io/collector/pipeline"
1617
)
1718

19+
type loggerCore interface {
20+
zapcore.Core
21+
Without(fields ...string) *zap.Logger
22+
}
23+
1824
func TestLogger(t *testing.T) {
1925
core, observed := observer.New(zap.DebugLevel)
2026
logger := zap.New(core).With(zap.String("preexisting", "value"))
@@ -26,7 +32,7 @@ func TestLogger(t *testing.T) {
2632

2733
parent := componentattribute.NewLogger(logger, &attrs)
2834
parent.Info("test parent before child")
29-
child := parent.Core().(*componentattribute.Core).Without(string(componentattribute.SignalKey))
35+
child := parent.Core().(loggerCore).Without(string(componentattribute.SignalKey))
3036
child.Info("test child")
3137
parent.Info("test parent after child")
3238

Diff for: component/telemetry.go

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"go.opentelemetry.io/otel/metric"
88
"go.opentelemetry.io/otel/trace"
99
"go.uber.org/zap"
10+
"go.uber.org/zap/zapcore"
1011

1112
"go.opentelemetry.io/collector/pdata/pcommon"
1213
)
@@ -26,3 +27,14 @@ type TelemetrySettings struct {
2627
// Resource contains the resource attributes for the collector's telemetry.
2728
Resource pcommon.Resource
2829
}
30+
31+
func (ts *TelemetrySettings) LoggerWithout(fields ...string) *zap.Logger {
32+
type loggerCore interface {
33+
zapcore.Core
34+
Without(fields ...string) *zap.Logger
35+
}
36+
if _, ok := ts.Logger.Core().(loggerCore); !ok {
37+
return ts.Logger
38+
}
39+
return ts.Logger.Core().(loggerCore).Without(fields...)
40+
}

Diff for: processor/memorylimiterprocessor/factory.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,12 @@ func (f *factory) getMemoryLimiter(set processor.Settings, cfg component.Config)
106106
return memLimiter, nil
107107
}
108108

109-
if c, ok := set.Logger.Core().(*componentattribute.Core); ok {
110-
set.Logger = c.Without(
111-
componentattribute.SignalKey,
112-
componentattribute.PipelineIDKey,
113-
componentattribute.ComponentIDKey,
114-
)
115-
set.Logger.Debug("created singleton logger")
116-
}
109+
set.Logger = set.TelemetrySettings.LoggerWithout(
110+
componentattribute.SignalKey,
111+
componentattribute.PipelineIDKey,
112+
componentattribute.ComponentIDKey,
113+
)
114+
set.Logger.Debug("created singleton logger")
117115

118116
memLimiter, err := newMemoryLimiterProcessor(set, cfg.(*Config))
119117
if err != nil {

Diff for: receiver/otlpreceiver/otlp.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ type otlpReceiver struct {
5353
// responsibility to invoke the respective Start*Reception methods as well
5454
// as the various Stop*Reception methods to end it.
5555
func newOtlpReceiver(cfg *Config, set *receiver.Settings) (*otlpReceiver, error) {
56-
if c, ok := set.Logger.Core().(*componentattribute.Core); ok {
57-
set.Logger = c.Without(componentattribute.SignalKey)
58-
set.Logger.Debug("created signal-agnostic logger")
59-
}
56+
set.Logger = set.TelemetrySettings.LoggerWithout(componentattribute.SignalKey)
57+
set.Logger.Debug("created signal-agnostic logger")
6058
r := &otlpReceiver{
6159
cfg: cfg,
6260
nextTraces: nil,

0 commit comments

Comments
 (0)