Skip to content

Commit 90a4238

Browse files
committed
Use attributes in loggers
1 parent e742a18 commit 90a4238

34 files changed

+1158
-380
lines changed

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/graph/attribute/attribute.go service/internal/attribute/attribute.go

+44-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package attribute // import "go.opentelemetry.io/collector/service/internal/graph/attribute"
4+
package attribute // import "go.opentelemetry.io/collector/service/internal/attribute"
55

66
import (
7-
"fmt"
87
"hash/fnv"
98

109
"go.opentelemetry.io/otel/attribute"
10+
"go.uber.org/zap"
1111

1212
"go.opentelemetry.io/collector/component"
1313
"go.opentelemetry.io/collector/pipeline"
@@ -20,10 +20,6 @@ const (
2020
signalKey = "otelcol.signal"
2121
signalOutputKey = "otelcol.signal.output"
2222

23-
receiverKind = "receiver"
24-
processorKind = "processor"
25-
exporterKind = "exporter"
26-
connectorKind = "connector"
2723
capabiltiesKind = "capabilities"
2824
fanoutKind = "fanout"
2925
)
@@ -52,17 +48,32 @@ func (a Attributes) ID() int64 {
5248
return a.id
5349
}
5450

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+
5559
func Receiver(pipelineType pipeline.Signal, id component.ID) *Attributes {
5660
return newAttributes(
57-
attribute.String(componentKindKey, receiverKind),
61+
attribute.String(componentKindKey, component.KindReceiver.String()),
5862
attribute.String(signalKey, pipelineType.String()),
5963
attribute.String(componentIDKey, id.String()),
6064
)
6165
}
6266

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+
6374
func Processor(pipelineID pipeline.ID, id component.ID) *Attributes {
6475
return newAttributes(
65-
attribute.String(componentKindKey, processorKind),
76+
attribute.String(componentKindKey, component.KindProcessor.String()),
6677
attribute.String(signalKey, pipelineID.Signal().String()),
6778
attribute.String(pipelineIDKey, pipelineID.String()),
6879
attribute.String(componentIDKey, id.String()),
@@ -71,16 +82,31 @@ func Processor(pipelineID pipeline.ID, id component.ID) *Attributes {
7182

7283
func Exporter(pipelineType pipeline.Signal, id component.ID) *Attributes {
7384
return newAttributes(
74-
attribute.String(componentKindKey, exporterKind),
85+
attribute.String(componentKindKey, component.KindExporter.String()),
7586
attribute.String(signalKey, pipelineType.String()),
7687
attribute.String(componentIDKey, id.String()),
7788
)
7889
}
7990

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+
8098
func Connector(exprPipelineType, rcvrPipelineType pipeline.Signal, id component.ID) *Attributes {
8199
return newAttributes(
82-
attribute.String(componentKindKey, connectorKind),
83-
attribute.String(signalKey, fmt.Sprintf("%s_to_%s", exprPipelineType.String(), rcvrPipelineType.String())),
100+
attribute.String(componentKindKey, component.KindConnector.String()),
101+
attribute.String(signalKey, exprPipelineType.String()),
102+
attribute.String(signalOutputKey, rcvrPipelineType.String()),
103+
attribute.String(componentIDKey, id.String()),
104+
)
105+
}
106+
107+
func ConnectorSingleton(id component.ID) *Attributes {
108+
return newAttributes(
109+
attribute.String(componentKindKey, component.KindConnector.String()),
84110
attribute.String(componentIDKey, id.String()),
85111
)
86112
}
@@ -98,3 +124,10 @@ func Fanout(pipelineID pipeline.ID) *Attributes {
98124
attribute.String(pipelineIDKey, pipelineID.String()),
99125
)
100126
}
127+
128+
func Extension(id component.ID) *Attributes {
129+
return newAttributes(
130+
attribute.String(componentKindKey, component.KindExtension.String()),
131+
attribute.String(componentIDKey, id.String()),
132+
)
133+
}

0 commit comments

Comments
 (0)