Skip to content

Commit

Permalink
Use attributes in loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Jan 8, 2025
1 parent e742a18 commit 0717a5d
Show file tree
Hide file tree
Showing 18 changed files with 361 additions and 235 deletions.
6 changes: 3 additions & 3 deletions service/extensions/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/extension/extensioncapabilities"
"go.opentelemetry.io/collector/service/internal/attribute"
"go.opentelemetry.io/collector/service/internal/builders"
"go.opentelemetry.io/collector/service/internal/components"
"go.opentelemetry.io/collector/service/internal/status"
"go.opentelemetry.io/collector/service/internal/zpages"
)
Expand All @@ -38,7 +38,7 @@ type Extensions struct {
func (bes *Extensions) Start(ctx context.Context, host component.Host) error {
bes.telemetry.Logger.Info("Starting extensions...")
for _, extID := range bes.extensionIDs {
extLogger := components.ExtensionLogger(bes.telemetry.Logger, extID)
extLogger := attribute.Extension(extID).Logger(bes.telemetry.Logger)
extLogger.Info("Extension is starting...")
instanceID := bes.instanceIDs[extID]
ext := bes.extMap[extID]
Expand Down Expand Up @@ -216,7 +216,7 @@ func New(ctx context.Context, set Settings, cfg Config, options ...Option) (*Ext
BuildInfo: set.BuildInfo,
ModuleInfo: set.ModuleInfo,
}
extSet.TelemetrySettings.Logger = components.ExtensionLogger(set.Telemetry.Logger, extID)
extSet.TelemetrySettings.Logger = attribute.Extension(extID).Logger(set.Telemetry.Logger)

ext, err := set.Extensions.Create(ctx, extSet)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package attribute // import "go.opentelemetry.io/collector/service/internal/graph/attribute"
package attribute // import "go.opentelemetry.io/collector/service/internal/attribute"

import (
"fmt"
"hash/fnv"

"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pipeline"
Expand All @@ -20,10 +20,6 @@ const (
signalKey = "otelcol.signal"
signalOutputKey = "otelcol.signal.output"

receiverKind = "receiver"
processorKind = "processor"
exporterKind = "exporter"
connectorKind = "connector"
capabiltiesKind = "capabilities"
fanoutKind = "fanout"
)
Expand Down Expand Up @@ -52,17 +48,32 @@ func (a Attributes) ID() int64 {
return a.id
}

func (a Attributes) Logger(logger *zap.Logger) *zap.Logger {
fields := make([]zap.Field, 0, a.set.Len())
for _, kv := range a.set.ToSlice() {
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
}
return logger.With(fields...)
}

func Receiver(pipelineType pipeline.Signal, id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, receiverKind),
attribute.String(componentKindKey, component.KindReceiver.String()),
attribute.String(signalKey, pipelineType.String()),
attribute.String(componentIDKey, id.String()),
)
}

func ReceiverSingleton(id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, component.KindReceiver.String()),
attribute.String(componentIDKey, id.String()),
)
}

func Processor(pipelineID pipeline.ID, id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, processorKind),
attribute.String(componentKindKey, component.KindProcessor.String()),
attribute.String(signalKey, pipelineID.Signal().String()),
attribute.String(pipelineIDKey, pipelineID.String()),
attribute.String(componentIDKey, id.String()),
Expand All @@ -71,16 +82,31 @@ func Processor(pipelineID pipeline.ID, id component.ID) *Attributes {

func Exporter(pipelineType pipeline.Signal, id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, exporterKind),
attribute.String(componentKindKey, component.KindExporter.String()),
attribute.String(signalKey, pipelineType.String()),
attribute.String(componentIDKey, id.String()),
)
}

func ExporterSingleton(id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, component.KindExporter.String()),
attribute.String(componentIDKey, id.String()),
)
}

func Connector(exprPipelineType, rcvrPipelineType pipeline.Signal, id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, connectorKind),
attribute.String(signalKey, fmt.Sprintf("%s_to_%s", exprPipelineType.String(), rcvrPipelineType.String())),
attribute.String(componentKindKey, component.KindConnector.String()),
attribute.String(signalKey, exprPipelineType.String()),
attribute.String(signalOutputKey, rcvrPipelineType.String()),
attribute.String(componentIDKey, id.String()),
)
}

func ConnectorSingleton(id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, component.KindConnector.String()),
attribute.String(componentIDKey, id.String()),
)
}
Expand All @@ -98,3 +124,10 @@ func Fanout(pipelineID pipeline.ID) *Attributes {
attribute.String(pipelineIDKey, pipelineID.String()),
)
}

func Extension(id component.ID) *Attributes {
return newAttributes(
attribute.String(componentKindKey, component.KindExtension.String()),
attribute.String(componentIDKey, id.String()),
)
}
241 changes: 241 additions & 0 deletions service/internal/attribute/attribute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package attribute

import (
"testing"

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pipeline"
"go.opentelemetry.io/collector/pipeline/xpipeline"
)

var (
signals = []pipeline.Signal{
pipeline.SignalTraces,
pipeline.SignalMetrics,
pipeline.SignalLogs,
xpipeline.SignalProfiles,
}

cIDs = []component.ID{
component.MustNewID("foo"),
component.MustNewID("foo2"),
component.MustNewID("bar"),
}

pIDs = []pipeline.ID{
pipeline.MustNewID("traces"),
pipeline.MustNewIDWithName("traces", "2"),
pipeline.MustNewID("metrics"),
pipeline.MustNewIDWithName("metrics", "2"),
pipeline.MustNewID("logs"),
pipeline.MustNewIDWithName("logs", "2"),
pipeline.MustNewID("profiles"),
pipeline.MustNewIDWithName("profiles", "2"),
}
)

func TestReceiver(t *testing.T) {
for _, sig := range signals {
for _, id := range cIDs {
r := Receiver(sig, id)
componentKind, ok := r.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindReceiver, componentKind.AsString())

signal, ok := r.Attributes().Value(signalKey)
require.True(t, ok)
require.Equal(t, sig.String(), signal.AsString())

componentID, ok := r.Attributes().Value(componentIDKey)
require.True(t, ok)
require.Equal(t, id.String(), componentID.AsString())
}
}
}

func TestReceiverSingleton(t *testing.T) {
for _, id := range cIDs {
r := ReceiverSingleton(id)
componentKind, ok := r.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindReceiver, componentKind.AsString())

componentID, ok := r.Attributes().Value(componentIDKey)
require.True(t, ok)
require.Equal(t, id.String(), componentID.AsString())
}
}

func TestProcessor(t *testing.T) {
for _, pID := range pIDs {
for _, id := range cIDs {
p := Processor(pID, id)
componentKind, ok := p.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindProcessor, componentKind.AsString())

pipelineID, ok := p.Attributes().Value(pipelineIDKey)
require.True(t, ok)
require.Equal(t, pID.String(), pipelineID.AsString())

componentID, ok := p.Attributes().Value(componentIDKey)
require.True(t, ok)
require.Equal(t, id.String(), componentID.AsString())
}
}
}

func TestExporter(t *testing.T) {
for _, sig := range signals {
for _, id := range cIDs {
e := Exporter(sig, id)
componentKind, ok := e.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindExporter, componentKind.AsString())

signal, ok := e.Attributes().Value(signalKey)
require.True(t, ok)
require.Equal(t, sig.String(), signal.AsString())

componentID, ok := e.Attributes().Value(componentIDKey)
require.True(t, ok)
require.Equal(t, id.String(), componentID.AsString())
}
}
}

func TestExporterSingleton(t *testing.T) {
for _, id := range cIDs {
e := ExporterSingleton(id)
componentKind, ok := e.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindExporter, componentKind.AsString())

componentID, ok := e.Attributes().Value(componentIDKey)
require.True(t, ok)
require.Equal(t, id.String(), componentID.AsString())
}
}

func TestConnector(t *testing.T) {
for _, exprSig := range signals {
for _, rcvrSig := range signals {
for _, id := range cIDs {
c := Connector(exprSig, rcvrSig, id)
componentKind, ok := c.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindConnector, componentKind.AsString())

signal, ok := c.Attributes().Value(signalKey)
require.True(t, ok)
require.Equal(t, exprSig.String(), signal.AsString())

signalOutput, ok := c.Attributes().Value(signalOutputKey)
require.True(t, ok)
require.Equal(t, rcvrSig.String(), signalOutput.AsString())

componentID, ok := c.Attributes().Value(componentIDKey)
require.True(t, ok)
require.Equal(t, id.String(), componentID.AsString())
}
}
}
}

func TestConnectorSingleton(t *testing.T) {
for _, id := range cIDs {
c := ConnectorSingleton(id)
componentKind, ok := c.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindConnector, componentKind.AsString())

componentID, ok := c.Attributes().Value(componentIDKey)
require.True(t, ok)
require.Equal(t, id.String(), componentID.AsString())
}
}

func TestExtension(t *testing.T) {
e := Extension(component.MustNewID("foo"))
componentKind, ok := e.Attributes().Value(componentKindKey)
require.True(t, ok)
require.Equal(t, component.KindExtension, componentKind.AsString())
}

func TestSetEquality(t *testing.T) {
// The sets are created independently but should be exactly equivalent.
// We will ensure that corresponding elements are equal and that
// non-corresponding elements are not equal.
setI, setJ := createExampleSets(), createExampleSets()
for i, ei := range setI {
for j, ej := range setJ {
if i == j {
require.Equal(t, ei.ID(), ej.ID())
require.True(t, ei.Attributes().Equals(ej.Attributes()))
} else {
require.NotEqual(t, ei.ID(), ej.ID())
require.False(t, ei.Attributes().Equals(ej.Attributes()))
}
}
}
}

func createExampleSets() []*Attributes {
sets := []*Attributes{}

// Receiver examples.
for _, sig := range signals {
for _, id := range cIDs {
sets = append(sets, Receiver(sig, id))
}
}
for _, id := range cIDs {
sets = append(sets, ReceiverSingleton(id))
}

// Processor examples.
for _, pID := range pIDs {
for _, cID := range cIDs {
sets = append(sets, Processor(pID, cID))
}
}

// Exporter examples.
for _, sig := range signals {
for _, id := range cIDs {
sets = append(sets, Exporter(sig, id))
}
}
for _, id := range cIDs {
sets = append(sets, ExporterSingleton(id))
}

// Connector examples.
for _, exprSig := range signals {
for _, rcvrSig := range signals {
for _, id := range cIDs {
sets = append(sets, Connector(exprSig, rcvrSig, id))
}
}
}
for _, id := range cIDs {
sets = append(sets, ConnectorSingleton(id))
}

// Capabilities examples.
for _, pID := range pIDs {
sets = append(sets, Capabilities(pID))
}

// Fanout examples.
for _, pID := range pIDs {
sets = append(sets, Fanout(pID))
}

return sets
}
2 changes: 1 addition & 1 deletion service/internal/builders/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (b *ConnectorBuilder) IsConfigured(componentID component.ID) bool {
return ok
}

func (b *ConnectorBuilder) Factory(componentType component.Type) component.Factory {
func (b *ConnectorBuilder) Factory(componentType component.Type) connector.Factory {
return b.factories[componentType]
}

Expand Down
2 changes: 1 addition & 1 deletion service/internal/builders/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (b *ExporterBuilder) CreateProfiles(ctx context.Context, set exporter.Setti
return f.CreateProfiles(ctx, set, cfg)
}

func (b *ExporterBuilder) Factory(componentType component.Type) component.Factory {
func (b *ExporterBuilder) Factory(componentType component.Type) exporter.Factory {
return b.factories[componentType]
}

Expand Down
Loading

0 comments on commit 0717a5d

Please sign in to comment.