Skip to content

Commit c72b516

Browse files
committed
Use attributes in loggers
1 parent 819f5a6 commit c72b516

35 files changed

+793
-450
lines changed
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: exporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add `Metadata` method to `Factory` interface.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12057]
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: [api]
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: receiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add `Metadata` method to `Factory` interface.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12057]
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: [api]

connector/connector.go

-21
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,9 @@ type Factory interface {
112112
LogsToMetricsStability() component.StabilityLevel
113113
LogsToLogsStability() component.StabilityLevel
114114

115-
// Metadata returns the metadata describing the receiver.
116-
Metadata() Metadata
117-
118115
unexportedFactoryFunc()
119116
}
120117

121-
// Metadata contains metadata describing the component that is created by the factory.
122-
type Metadata struct {
123-
SingletonInstance bool
124-
}
125-
126118
// FactoryOption applies changes to Factory.
127119
type FactoryOption interface {
128120
// apply applies the option.
@@ -309,13 +301,6 @@ func WithLogsToLogs(createLogsToLogs CreateLogsToLogsFunc, sl component.Stabilit
309301
})
310302
}
311303

312-
// AsSingletonInstance indicates that the factory always returns the same instance of the component.
313-
func AsSingletonInstance() FactoryOption {
314-
return factoryOptionFunc(func(o *factory) {
315-
o.metadata.SingletonInstance = true
316-
})
317-
}
318-
319304
// factory implements the Factory interface.
320305
type factory struct {
321306
cfgType component.Type
@@ -344,8 +329,6 @@ type factory struct {
344329
logsToTracesStabilityLevel component.StabilityLevel
345330
logsToMetricsStabilityLevel component.StabilityLevel
346331
logsToLogsStabilityLevel component.StabilityLevel
347-
348-
metadata Metadata
349332
}
350333

351334
// Type returns the type of component.
@@ -391,10 +374,6 @@ func (f *factory) LogsToLogsStability() component.StabilityLevel {
391374
return f.logsToLogsStabilityLevel
392375
}
393376

394-
func (f *factory) Metadata() Metadata {
395-
return f.metadata
396-
}
397-
398377
// NewFactory returns a Factory.
399378
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
400379
f := &factory{

connector/connector_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,14 @@ func TestNewFactoryNoOptions(t *testing.T) {
4848
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalMetrics))
4949
_, err = factory.CreateLogsToLogs(context.Background(), Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
5050
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalLogs))
51-
52-
assert.False(t, factory.Metadata().SingletonInstance)
5351
}
5452

5553
func TestNewFactoryWithSameTypes(t *testing.T) {
5654
defaultCfg := struct{}{}
5755
factory := NewFactory(testType, func() component.Config { return &defaultCfg },
5856
WithTracesToTraces(createTracesToTraces, component.StabilityLevelAlpha),
5957
WithMetricsToMetrics(createMetricsToMetrics, component.StabilityLevelBeta),
60-
WithLogsToLogs(createLogsToLogs, component.StabilityLevelUnmaintained),
61-
AsSingletonInstance())
58+
WithLogsToLogs(createLogsToLogs, component.StabilityLevelUnmaintained))
6259
assert.EqualValues(t, testType, factory.Type())
6360
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
6461

@@ -88,8 +85,6 @@ func TestNewFactoryWithSameTypes(t *testing.T) {
8885
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalTraces))
8986
_, err = factory.CreateLogsToMetrics(context.Background(), Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
9087
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalMetrics))
91-
92-
assert.True(t, factory.Metadata().SingletonInstance)
9388
}
9489

9590
func TestNewFactoryWithTranslateTypes(t *testing.T) {

connector/xconnector/connector.go

-7
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,6 @@ func WithProfilesToLogs(createProfilesToLogs CreateProfilesToLogsFunc, sl compon
269269
})
270270
}
271271

272-
// AsSingletonInstance sets the connector as a singleton instance.
273-
func AsSingletonInstance() FactoryOption {
274-
return factoryOptionFunc(func(o *factoryOpts) {
275-
o.opts = append(o.opts, connector.AsSingletonInstance())
276-
})
277-
}
278-
279272
// factory implements the Factory interface.
280273
type factory struct {
281274
connector.Factory

connector/xconnector/connector_test.go

-5
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,12 @@ func TestNewFactoryNoOptions(t *testing.T) {
4444
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalMetrics))
4545
_, err = factory.CreateProfilesToLogs(context.Background(), connector.Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
4646
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalLogs))
47-
48-
assert.False(t, factory.Metadata().SingletonInstance)
4947
}
5048

5149
func TestNewFactoryWithSameTypes(t *testing.T) {
5250
defaultCfg := struct{}{}
5351
factory := NewFactory(testType, func() component.Config { return &defaultCfg },
5452
WithProfilesToProfiles(createProfilesToProfiles, component.StabilityLevelAlpha),
55-
AsSingletonInstance(),
5653
)
5754
assert.EqualValues(t, testType, factory.Type())
5855
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
@@ -67,8 +64,6 @@ func TestNewFactoryWithSameTypes(t *testing.T) {
6764
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalMetrics))
6865
_, err = factory.CreateProfilesToLogs(context.Background(), connector.Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
6966
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalLogs))
70-
71-
assert.True(t, factory.Metadata().SingletonInstance)
7267
}
7368

7469
func TestNewFactoryWithTranslateTypes(t *testing.T) {

exporter/exporter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestNewFactoryWithOptions(t *testing.T) {
5555

5656
assert.Equal(t, component.StabilityLevelDeprecated, f.LogsStability())
5757
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg)
58-
assert.NoError(t, err)
58+
require.NoError(t, err)
5959

6060
assert.True(t, f.Metadata().SingletonInstance)
6161
}

exporter/xexporter/exporter_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestNewFactoryWithProfiles(t *testing.T) {
2121
testType,
2222
func() component.Config { return &defaultCfg },
2323
WithProfiles(createProfiles, component.StabilityLevelDevelopment),
24+
AsSingletonInstance(),
2425
)
2526
assert.EqualValues(t, testType, factory.Type())
2627
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())

receiver/receiver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestNewFactoryWithOptions(t *testing.T) {
5656

5757
assert.Equal(t, component.StabilityLevelStable, f.LogsStability())
5858
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg, nil)
59-
assert.NoError(t, err)
59+
require.NoError(t, err)
6060

6161
assert.True(t, f.Metadata().SingletonInstance)
6262
}

receiver/xreceiver/receiver_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestNewFactoryWithProfiles(t *testing.T) {
2222
testType,
2323
func() component.Config { return &defaultCfg },
2424
WithProfiles(createProfiles, component.StabilityLevelAlpha),
25+
AsSingletonInstance(),
2526
)
2627
assert.EqualValues(t, testType, factory.Type())
2728
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())

service/extensions/extensions.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"go.opentelemetry.io/collector/confmap"
1818
"go.opentelemetry.io/collector/extension"
1919
"go.opentelemetry.io/collector/extension/extensioncapabilities"
20+
"go.opentelemetry.io/collector/service/internal/attribute"
2021
"go.opentelemetry.io/collector/service/internal/builders"
21-
"go.opentelemetry.io/collector/service/internal/components"
2222
"go.opentelemetry.io/collector/service/internal/status"
2323
"go.opentelemetry.io/collector/service/internal/zpages"
2424
)
@@ -38,7 +38,7 @@ type Extensions struct {
3838
func (bes *Extensions) Start(ctx context.Context, host component.Host) error {
3939
bes.telemetry.Logger.Info("Starting extensions...")
4040
for _, extID := range bes.extensionIDs {
41-
extLogger := components.ExtensionLogger(bes.telemetry.Logger, extID)
41+
extLogger := attribute.Extension(extID).Logger(bes.telemetry.Logger)
4242
extLogger.Info("Extension is starting...")
4343
instanceID := bes.instanceIDs[extID]
4444
ext := bes.extMap[extID]
@@ -216,7 +216,7 @@ func New(ctx context.Context, set Settings, cfg Config, options ...Option) (*Ext
216216
BuildInfo: set.BuildInfo,
217217
ModuleInfo: set.ModuleInfo,
218218
}
219-
extSet.TelemetrySettings.Logger = components.ExtensionLogger(set.Telemetry.Logger, extID)
219+
extSet.TelemetrySettings.Logger = attribute.Extension(extID).Logger(set.Telemetry.Logger)
220220

221221
ext, err := set.Extensions.Create(ctx, extSet)
222222
if err != nil {

service/internal/attribute/attribute.go

+23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"hash/fnv"
88

99
"go.opentelemetry.io/otel/attribute"
10+
"go.uber.org/zap"
1011

1112
"go.opentelemetry.io/collector/component"
1213
"go.opentelemetry.io/collector/pipeline"
@@ -47,6 +48,14 @@ func (a Attributes) ID() int64 {
4748
return a.id
4849
}
4950

51+
func (a Attributes) Logger(logger *zap.Logger) *zap.Logger {
52+
fields := make([]zap.Field, 0, a.set.Len())
53+
for _, kv := range a.set.ToSlice() {
54+
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
55+
}
56+
return logger.With(fields...)
57+
}
58+
5059
func Receiver(pipelineType pipeline.Signal, id component.ID) *Attributes {
5160
return newAttributes(
5261
attribute.String(componentKindKey, component.KindReceiver.String()),
@@ -55,6 +64,13 @@ func Receiver(pipelineType pipeline.Signal, id component.ID) *Attributes {
5564
)
5665
}
5766

67+
func ReceiverSingleton(id component.ID) *Attributes {
68+
return newAttributes(
69+
attribute.String(componentKindKey, component.KindReceiver.String()),
70+
attribute.String(componentIDKey, id.String()),
71+
)
72+
}
73+
5874
func Processor(pipelineID pipeline.ID, id component.ID) *Attributes {
5975
return newAttributes(
6076
attribute.String(componentKindKey, component.KindProcessor.String()),
@@ -72,6 +88,13 @@ func Exporter(pipelineType pipeline.Signal, id component.ID) *Attributes {
7288
)
7389
}
7490

91+
func ExporterSingleton(id component.ID) *Attributes {
92+
return newAttributes(
93+
attribute.String(componentKindKey, component.KindExporter.String()),
94+
attribute.String(componentIDKey, id.String()),
95+
)
96+
}
97+
7598
func Connector(exprPipelineType, rcvrPipelineType pipeline.Signal, id component.ID) *Attributes {
7699
return newAttributes(
77100
attribute.String(componentKindKey, component.KindConnector.String()),

service/internal/attribute/attribute_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ func TestReceiver(t *testing.T) {
5858
}
5959
}
6060

61+
func TestReceiverSingleton(t *testing.T) {
62+
for _, id := range cIDs {
63+
r := ReceiverSingleton(id)
64+
componentKind, ok := r.Attributes().Value(componentKindKey)
65+
require.True(t, ok)
66+
require.Equal(t, component.KindReceiver.String(), componentKind.AsString())
67+
68+
componentID, ok := r.Attributes().Value(componentIDKey)
69+
require.True(t, ok)
70+
require.Equal(t, id.String(), componentID.AsString())
71+
}
72+
}
73+
6174
func TestProcessor(t *testing.T) {
6275
for _, pID := range pIDs {
6376
for _, id := range cIDs {
@@ -96,6 +109,19 @@ func TestExporter(t *testing.T) {
96109
}
97110
}
98111

112+
func TestExporterSingleton(t *testing.T) {
113+
for _, id := range cIDs {
114+
e := ExporterSingleton(id)
115+
componentKind, ok := e.Attributes().Value(componentKindKey)
116+
require.True(t, ok)
117+
require.Equal(t, component.KindExporter.String(), componentKind.AsString())
118+
119+
componentID, ok := e.Attributes().Value(componentIDKey)
120+
require.True(t, ok)
121+
require.Equal(t, id.String(), componentID.AsString())
122+
}
123+
}
124+
99125
func TestConnector(t *testing.T) {
100126
for _, exprSig := range signals {
101127
for _, rcvrSig := range signals {
@@ -155,6 +181,9 @@ func createExampleSets() []*Attributes {
155181
sets = append(sets, Receiver(sig, id))
156182
}
157183
}
184+
for _, id := range cIDs {
185+
sets = append(sets, ReceiverSingleton(id))
186+
}
158187

159188
// Processor examples.
160189
for _, pID := range pIDs {
@@ -169,6 +198,9 @@ func createExampleSets() []*Attributes {
169198
sets = append(sets, Exporter(sig, id))
170199
}
171200
}
201+
for _, id := range cIDs {
202+
sets = append(sets, ExporterSingleton(id))
203+
}
172204

173205
// Connector examples.
174206
for _, exprSig := range signals {

service/internal/builders/connector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func (b *ConnectorBuilder) IsConfigured(componentID component.ID) bool {
376376
return ok
377377
}
378378

379-
func (b *ConnectorBuilder) Factory(componentType component.Type) component.Factory {
379+
func (b *ConnectorBuilder) Factory(componentType component.Type) connector.Factory {
380380
return b.factories[componentType]
381381
}
382382

service/internal/builders/exporter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (b *ExporterBuilder) CreateProfiles(ctx context.Context, set exporter.Setti
9494
return f.CreateProfiles(ctx, set, cfg)
9595
}
9696

97-
func (b *ExporterBuilder) Factory(componentType component.Type) component.Factory {
97+
func (b *ExporterBuilder) Factory(componentType component.Type) exporter.Factory {
9898
return b.factories[componentType]
9999
}
100100

service/internal/builders/processor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (b *ProcessorBuilder) CreateProfiles(ctx context.Context, set processor.Set
108108
return f.CreateProfiles(ctx, set, cfg, next)
109109
}
110110

111-
func (b *ProcessorBuilder) Factory(componentType component.Type) component.Factory {
111+
func (b *ProcessorBuilder) Factory(componentType component.Type) processor.Factory {
112112
return b.factories[componentType]
113113
}
114114

service/internal/builders/receiver.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (b *ReceiverBuilder) CreateProfiles(ctx context.Context, set receiver.Setti
110110
return f.CreateProfiles(ctx, set, cfg, next)
111111
}
112112

113-
func (b *ReceiverBuilder) Factory(componentType component.Type) component.Factory {
113+
func (b *ReceiverBuilder) Factory(componentType component.Type) receiver.Factory {
114114
return b.factories[componentType]
115115
}
116116

0 commit comments

Comments
 (0)