Skip to content

Commit c7eba1d

Browse files
committed
Minimize new surface area
1 parent 95a3234 commit c7eba1d

File tree

10 files changed

+73
-44
lines changed

10 files changed

+73
-44
lines changed

Diff for: component/componentattribute/logger.go

+10-22
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,30 @@ var _ zapcore.Core = (*coreWithout)(nil)
1313

1414
type coreWithout struct {
1515
zapcore.Core
16-
from *zap.Logger
17-
attrs attribute.Set
16+
from zapcore.Core
17+
fields []zap.Field
1818
}
1919

20-
func NewLogger(from *zap.Logger, attrs *attribute.Set) *zap.Logger {
20+
func NewCore(from zapcore.Core, attrs *attribute.Set) zapcore.Core {
2121
fields := []zap.Field{}
2222
for _, kv := range attrs.ToSlice() {
2323
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
2424
}
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-
)
25+
return &coreWithout{Core: from.With(fields), from: from, fields: fields}
3126
}
3227

33-
func (l *coreWithout) Without(keys ...string) *zap.Logger {
28+
func (l *coreWithout) Without(keys ...string) zapcore.Core {
3429
excludeKeys := make(map[string]struct{})
3530
for _, key := range keys {
3631
excludeKeys[key] = struct{}{}
3732
}
3833

39-
newAttrs := []attribute.KeyValue{}
40-
fields := []zap.Field{}
41-
for _, kv := range l.attrs.ToSlice() {
42-
if _, excluded := excludeKeys[string(kv.Key)]; !excluded {
43-
newAttrs = append(newAttrs, kv)
44-
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
34+
fieldsWithout := []zap.Field{}
35+
for _, field := range l.fields {
36+
if _, excluded := excludeKeys[field.Key]; !excluded {
37+
fieldsWithout = append(fieldsWithout, field)
4538
}
4639
}
4740

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-
)
41+
return &coreWithout{Core: l.from.With(fieldsWithout), from: l.from, fields: fieldsWithout}
5442
}

Diff for: component/componentattribute/logger_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import (
1717
)
1818

1919
type loggerCore interface {
20-
zapcore.Core
21-
Without(fields ...string) *zap.Logger
20+
Without(fields ...string) zapcore.Core
2221
}
2322

24-
func TestLogger(t *testing.T) {
23+
func TestCore(t *testing.T) {
2524
core, observed := observer.New(zap.DebugLevel)
2625
logger := zap.New(core).With(zap.String("preexisting", "value"))
2726

@@ -30,9 +29,9 @@ func TestLogger(t *testing.T) {
3029
attribute.String(componentattribute.ComponentIDKey, "filelog"),
3130
)
3231

33-
parent := componentattribute.NewLogger(logger, &attrs)
32+
parent := zap.New(componentattribute.NewCore(logger.Core(), &attrs))
3433
parent.Info("test parent before child")
35-
child := parent.Core().(loggerCore).Without(string(componentattribute.SignalKey))
34+
child := zap.New(parent.Core().(loggerCore).Without(string(componentattribute.SignalKey)))
3635
child.Info("test child")
3736
parent.Info("test parent after child")
3837

Diff for: component/telemetry.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ type TelemetrySettings struct {
2929
}
3030

3131
func (ts *TelemetrySettings) LoggerWithout(fields ...string) *zap.Logger {
32-
type loggerCore interface {
33-
zapcore.Core
34-
Without(fields ...string) *zap.Logger
32+
type coreWithout interface {
33+
Without(fields ...string) zapcore.Core
3534
}
36-
if _, ok := ts.Logger.Core().(loggerCore); !ok {
35+
if _, ok := ts.Logger.Core().(coreWithout); !ok {
3736
return ts.Logger
3837
}
39-
return ts.Logger.Core().(loggerCore).Without(fields...)
38+
return ts.Logger.WithOptions(
39+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
40+
return from.(coreWithout).Without(fields...)
41+
}),
42+
)
4043
}

Diff for: processor/memorylimiterprocessor/factory_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func TestCreateProcessor(t *testing.T) {
5151
attribute.String(componentattribute.PipelineIDKey, "logs/foo"),
5252
)
5353
set := processortest.NewNopSettings()
54-
set.Logger = componentattribute.NewLogger(zap.New(core), &attrs)
54+
set.Logger = zap.New(core).WithOptions(
55+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
56+
return componentattribute.NewCore(from, &attrs)
57+
}),
58+
)
5559

5660
tp, err := factory.CreateTraces(context.Background(), set, cfg, consumertest.NewNop())
5761
require.NoError(t, err)

Diff for: receiver/otlpreceiver/factory_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ func TestCreateSameReceiver(t *testing.T) {
4646
attribute.String(componentattribute.ComponentIDKey, "otlp"),
4747
)
4848
creationSet := receivertest.NewNopSettings()
49-
creationSet.Logger = componentattribute.NewLogger(zap.New(core), &attrs)
49+
creationSet.Logger = zap.New(core).WithOptions(
50+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
51+
return componentattribute.NewCore(from, &attrs)
52+
}),
53+
)
5054
tReceiver, err := factory.CreateTraces(context.Background(), creationSet, cfg, consumertest.NewNop())
5155
assert.NotNil(t, tReceiver)
5256
require.NoError(t, err)

Diff for: service/extensions/extensions.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"go.uber.org/multierr"
1313
"go.uber.org/zap"
14+
"go.uber.org/zap/zapcore"
1415

1516
"go.opentelemetry.io/collector/component"
1617
"go.opentelemetry.io/collector/component/componentattribute"
@@ -39,9 +40,10 @@ type Extensions struct {
3940
func (bes *Extensions) Start(ctx context.Context, host component.Host) error {
4041
bes.telemetry.Logger.Info("Starting extensions...")
4142
for _, extID := range bes.extensionIDs {
42-
extLogger := componentattribute.NewLogger(
43-
bes.telemetry.Logger,
44-
attribute.Extension(extID).Set(),
43+
extLogger := bes.telemetry.Logger.WithOptions(
44+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
45+
return componentattribute.NewCore(from, attribute.Extension(extID).Set())
46+
}),
4547
)
4648
extLogger.Info("Extension is starting...")
4749
instanceID := bes.instanceIDs[extID]
@@ -220,9 +222,10 @@ func New(ctx context.Context, set Settings, cfg Config, options ...Option) (*Ext
220222
BuildInfo: set.BuildInfo,
221223
ModuleInfo: set.ModuleInfo,
222224
}
223-
extSet.TelemetrySettings.Logger = componentattribute.NewLogger(
224-
set.Telemetry.Logger,
225-
attribute.Extension(extID).Set(),
225+
extSet.TelemetrySettings.Logger = extSet.TelemetrySettings.Logger.WithOptions(
226+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
227+
return componentattribute.NewCore(from, attribute.Extension(extID).Set())
228+
}),
226229
)
227230

228231
ext, err := set.Extensions.Create(ctx, extSet)

Diff for: service/internal/graph/connector.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package graph // import "go.opentelemetry.io/collector/service/internal/graph"
66
import (
77
"context"
88

9+
"go.uber.org/zap"
10+
"go.uber.org/zap/zapcore"
11+
912
"go.opentelemetry.io/collector/component"
1013
"go.opentelemetry.io/collector/component/componentattribute"
1114
"go.opentelemetry.io/collector/connector"
@@ -49,7 +52,11 @@ func (n *connectorNode) buildComponent(
4952
builder *builders.ConnectorBuilder,
5053
nexts []baseConsumer,
5154
) error {
52-
tel.Logger = componentattribute.NewLogger(tel.Logger, n.Attributes.Set())
55+
tel.Logger = tel.Logger.WithOptions(
56+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
57+
return componentattribute.NewCore(from, n.Attributes.Set())
58+
}),
59+
)
5360
set := connector.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
5461
switch n.rcvrPipelineType {
5562
case pipeline.SignalTraces:

Diff for: service/internal/graph/exporter.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"context"
88
"fmt"
99

10+
"go.uber.org/zap"
11+
"go.uber.org/zap/zapcore"
12+
1013
"go.opentelemetry.io/collector/component"
1114
"go.opentelemetry.io/collector/component/componentattribute"
1215
"go.opentelemetry.io/collector/exporter"
@@ -45,7 +48,11 @@ func (n *exporterNode) buildComponent(
4548
info component.BuildInfo,
4649
builder *builders.ExporterBuilder,
4750
) error {
48-
tel.Logger = componentattribute.NewLogger(tel.Logger, n.Attributes.Set())
51+
tel.Logger = tel.Logger.WithOptions(
52+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
53+
return componentattribute.NewCore(from, n.Attributes.Set())
54+
}),
55+
)
4956
set := exporter.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
5057
var err error
5158
switch n.pipelineType {

Diff for: service/internal/graph/processor.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"context"
88
"fmt"
99

10+
"go.uber.org/zap"
11+
"go.uber.org/zap/zapcore"
12+
1013
"go.opentelemetry.io/collector/component"
1114
"go.opentelemetry.io/collector/component/componentattribute"
1215
"go.opentelemetry.io/collector/consumer"
@@ -47,7 +50,11 @@ func (n *processorNode) buildComponent(ctx context.Context,
4750
builder *builders.ProcessorBuilder,
4851
next baseConsumer,
4952
) error {
50-
tel.Logger = componentattribute.NewLogger(tel.Logger, n.Attributes.Set())
53+
tel.Logger = tel.Logger.WithOptions(
54+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
55+
return componentattribute.NewCore(from, n.Attributes.Set())
56+
}),
57+
)
5158
set := processor.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
5259
var err error
5360
switch n.pipelineID.Signal() {

Diff for: service/internal/graph/receiver.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"context"
88
"fmt"
99

10+
"go.uber.org/zap"
11+
"go.uber.org/zap/zapcore"
12+
1013
"go.opentelemetry.io/collector/component"
1114
"go.opentelemetry.io/collector/component/componentattribute"
1215
"go.opentelemetry.io/collector/consumer"
@@ -42,7 +45,11 @@ func (n *receiverNode) buildComponent(ctx context.Context,
4245
builder *builders.ReceiverBuilder,
4346
nexts []baseConsumer,
4447
) error {
45-
tel.Logger = componentattribute.NewLogger(tel.Logger, n.Attributes.Set())
48+
tel.Logger = tel.Logger.WithOptions(
49+
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
50+
return componentattribute.NewCore(from, n.Attributes.Set())
51+
}),
52+
)
4653
set := receiver.Settings{ID: n.componentID, TelemetrySettings: tel, BuildInfo: info}
4754
var err error
4855
switch n.pipelineType {

0 commit comments

Comments
 (0)