Skip to content

Commit 0717a5d

Browse files
committed
Use attributes in loggers
1 parent e742a18 commit 0717a5d

18 files changed

+361
-235
lines changed

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 renamed to 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package attribute
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
11+
"go.opentelemetry.io/collector/component"
12+
"go.opentelemetry.io/collector/pipeline"
13+
"go.opentelemetry.io/collector/pipeline/xpipeline"
14+
)
15+
16+
var (
17+
signals = []pipeline.Signal{
18+
pipeline.SignalTraces,
19+
pipeline.SignalMetrics,
20+
pipeline.SignalLogs,
21+
xpipeline.SignalProfiles,
22+
}
23+
24+
cIDs = []component.ID{
25+
component.MustNewID("foo"),
26+
component.MustNewID("foo2"),
27+
component.MustNewID("bar"),
28+
}
29+
30+
pIDs = []pipeline.ID{
31+
pipeline.MustNewID("traces"),
32+
pipeline.MustNewIDWithName("traces", "2"),
33+
pipeline.MustNewID("metrics"),
34+
pipeline.MustNewIDWithName("metrics", "2"),
35+
pipeline.MustNewID("logs"),
36+
pipeline.MustNewIDWithName("logs", "2"),
37+
pipeline.MustNewID("profiles"),
38+
pipeline.MustNewIDWithName("profiles", "2"),
39+
}
40+
)
41+
42+
func TestReceiver(t *testing.T) {
43+
for _, sig := range signals {
44+
for _, id := range cIDs {
45+
r := Receiver(sig, id)
46+
componentKind, ok := r.Attributes().Value(componentKindKey)
47+
require.True(t, ok)
48+
require.Equal(t, component.KindReceiver, componentKind.AsString())
49+
50+
signal, ok := r.Attributes().Value(signalKey)
51+
require.True(t, ok)
52+
require.Equal(t, sig.String(), signal.AsString())
53+
54+
componentID, ok := r.Attributes().Value(componentIDKey)
55+
require.True(t, ok)
56+
require.Equal(t, id.String(), componentID.AsString())
57+
}
58+
}
59+
}
60+
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, 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+
74+
func TestProcessor(t *testing.T) {
75+
for _, pID := range pIDs {
76+
for _, id := range cIDs {
77+
p := Processor(pID, id)
78+
componentKind, ok := p.Attributes().Value(componentKindKey)
79+
require.True(t, ok)
80+
require.Equal(t, component.KindProcessor, componentKind.AsString())
81+
82+
pipelineID, ok := p.Attributes().Value(pipelineIDKey)
83+
require.True(t, ok)
84+
require.Equal(t, pID.String(), pipelineID.AsString())
85+
86+
componentID, ok := p.Attributes().Value(componentIDKey)
87+
require.True(t, ok)
88+
require.Equal(t, id.String(), componentID.AsString())
89+
}
90+
}
91+
}
92+
93+
func TestExporter(t *testing.T) {
94+
for _, sig := range signals {
95+
for _, id := range cIDs {
96+
e := Exporter(sig, id)
97+
componentKind, ok := e.Attributes().Value(componentKindKey)
98+
require.True(t, ok)
99+
require.Equal(t, component.KindExporter, componentKind.AsString())
100+
101+
signal, ok := e.Attributes().Value(signalKey)
102+
require.True(t, ok)
103+
require.Equal(t, sig.String(), signal.AsString())
104+
105+
componentID, ok := e.Attributes().Value(componentIDKey)
106+
require.True(t, ok)
107+
require.Equal(t, id.String(), componentID.AsString())
108+
}
109+
}
110+
}
111+
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, 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+
125+
func TestConnector(t *testing.T) {
126+
for _, exprSig := range signals {
127+
for _, rcvrSig := range signals {
128+
for _, id := range cIDs {
129+
c := Connector(exprSig, rcvrSig, id)
130+
componentKind, ok := c.Attributes().Value(componentKindKey)
131+
require.True(t, ok)
132+
require.Equal(t, component.KindConnector, componentKind.AsString())
133+
134+
signal, ok := c.Attributes().Value(signalKey)
135+
require.True(t, ok)
136+
require.Equal(t, exprSig.String(), signal.AsString())
137+
138+
signalOutput, ok := c.Attributes().Value(signalOutputKey)
139+
require.True(t, ok)
140+
require.Equal(t, rcvrSig.String(), signalOutput.AsString())
141+
142+
componentID, ok := c.Attributes().Value(componentIDKey)
143+
require.True(t, ok)
144+
require.Equal(t, id.String(), componentID.AsString())
145+
}
146+
}
147+
}
148+
}
149+
150+
func TestConnectorSingleton(t *testing.T) {
151+
for _, id := range cIDs {
152+
c := ConnectorSingleton(id)
153+
componentKind, ok := c.Attributes().Value(componentKindKey)
154+
require.True(t, ok)
155+
require.Equal(t, component.KindConnector, componentKind.AsString())
156+
157+
componentID, ok := c.Attributes().Value(componentIDKey)
158+
require.True(t, ok)
159+
require.Equal(t, id.String(), componentID.AsString())
160+
}
161+
}
162+
163+
func TestExtension(t *testing.T) {
164+
e := Extension(component.MustNewID("foo"))
165+
componentKind, ok := e.Attributes().Value(componentKindKey)
166+
require.True(t, ok)
167+
require.Equal(t, component.KindExtension, componentKind.AsString())
168+
}
169+
170+
func TestSetEquality(t *testing.T) {
171+
// The sets are created independently but should be exactly equivalent.
172+
// We will ensure that corresponding elements are equal and that
173+
// non-corresponding elements are not equal.
174+
setI, setJ := createExampleSets(), createExampleSets()
175+
for i, ei := range setI {
176+
for j, ej := range setJ {
177+
if i == j {
178+
require.Equal(t, ei.ID(), ej.ID())
179+
require.True(t, ei.Attributes().Equals(ej.Attributes()))
180+
} else {
181+
require.NotEqual(t, ei.ID(), ej.ID())
182+
require.False(t, ei.Attributes().Equals(ej.Attributes()))
183+
}
184+
}
185+
}
186+
}
187+
188+
func createExampleSets() []*Attributes {
189+
sets := []*Attributes{}
190+
191+
// Receiver examples.
192+
for _, sig := range signals {
193+
for _, id := range cIDs {
194+
sets = append(sets, Receiver(sig, id))
195+
}
196+
}
197+
for _, id := range cIDs {
198+
sets = append(sets, ReceiverSingleton(id))
199+
}
200+
201+
// Processor examples.
202+
for _, pID := range pIDs {
203+
for _, cID := range cIDs {
204+
sets = append(sets, Processor(pID, cID))
205+
}
206+
}
207+
208+
// Exporter examples.
209+
for _, sig := range signals {
210+
for _, id := range cIDs {
211+
sets = append(sets, Exporter(sig, id))
212+
}
213+
}
214+
for _, id := range cIDs {
215+
sets = append(sets, ExporterSingleton(id))
216+
}
217+
218+
// Connector examples.
219+
for _, exprSig := range signals {
220+
for _, rcvrSig := range signals {
221+
for _, id := range cIDs {
222+
sets = append(sets, Connector(exprSig, rcvrSig, id))
223+
}
224+
}
225+
}
226+
for _, id := range cIDs {
227+
sets = append(sets, ConnectorSingleton(id))
228+
}
229+
230+
// Capabilities examples.
231+
for _, pID := range pIDs {
232+
sets = append(sets, Capabilities(pID))
233+
}
234+
235+
// Fanout examples.
236+
for _, pID := range pIDs {
237+
sets = append(sets, Fanout(pID))
238+
}
239+
240+
return sets
241+
}

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

0 commit comments

Comments
 (0)