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 10, 2025
1 parent e742a18 commit 7772955
Show file tree
Hide file tree
Showing 34 changed files with 1,131 additions and 379 deletions.
21 changes: 0 additions & 21 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,9 @@ type Factory interface {
LogsToMetricsStability() component.StabilityLevel
LogsToLogsStability() component.StabilityLevel

// Metadata returns the metadata describing the receiver.
Metadata() Metadata

unexportedFactoryFunc()
}

// Metadata contains metadata describing the component that is created by the factory.
type Metadata struct {
SingletonInstance bool
}

// FactoryOption applies changes to Factory.
type FactoryOption interface {
// apply applies the option.
Expand Down Expand Up @@ -309,13 +301,6 @@ func WithLogsToLogs(createLogsToLogs CreateLogsToLogsFunc, sl component.Stabilit
})
}

// AsSingletonInstance indicates that the factory always returns the same instance of the component.
func AsSingletonInstance() FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.metadata.SingletonInstance = true
})
}

// factory implements the Factory interface.
type factory struct {
cfgType component.Type
Expand Down Expand Up @@ -344,8 +329,6 @@ type factory struct {
logsToTracesStabilityLevel component.StabilityLevel
logsToMetricsStabilityLevel component.StabilityLevel
logsToLogsStabilityLevel component.StabilityLevel

metadata Metadata
}

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

func (f *factory) Metadata() Metadata {
return f.metadata
}

// NewFactory returns a Factory.
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
f := &factory{
Expand Down
7 changes: 1 addition & 6 deletions connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@ func TestNewFactoryNoOptions(t *testing.T) {
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalMetrics))
_, err = factory.CreateLogsToLogs(context.Background(), Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalLogs))

assert.False(t, factory.Metadata().SingletonInstance)
}

func TestNewFactoryWithSameTypes(t *testing.T) {
defaultCfg := struct{}{}
factory := NewFactory(testType, func() component.Config { return &defaultCfg },
WithTracesToTraces(createTracesToTraces, component.StabilityLevelAlpha),
WithMetricsToMetrics(createMetricsToMetrics, component.StabilityLevelBeta),
WithLogsToLogs(createLogsToLogs, component.StabilityLevelUnmaintained),
AsSingletonInstance())
WithLogsToLogs(createLogsToLogs, component.StabilityLevelUnmaintained))
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())

Expand Down Expand Up @@ -88,8 +85,6 @@ func TestNewFactoryWithSameTypes(t *testing.T) {
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalTraces))
_, err = factory.CreateLogsToMetrics(context.Background(), Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
assert.Equal(t, err, internal.ErrDataTypes(testID, pipeline.SignalLogs, pipeline.SignalMetrics))

assert.True(t, factory.Metadata().SingletonInstance)
}

func TestNewFactoryWithTranslateTypes(t *testing.T) {
Expand Down
7 changes: 0 additions & 7 deletions connector/xconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ func WithProfilesToLogs(createProfilesToLogs CreateProfilesToLogsFunc, sl compon
})
}

// AsSingletonInstance sets the connector as a singleton instance.
func AsSingletonInstance() FactoryOption {
return factoryOptionFunc(func(o *factoryOpts) {
o.opts = append(o.opts, connector.AsSingletonInstance())
})
}

// factory implements the Factory interface.
type factory struct {
connector.Factory
Expand Down
5 changes: 0 additions & 5 deletions connector/xconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,12 @@ func TestNewFactoryNoOptions(t *testing.T) {
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalMetrics))
_, err = factory.CreateProfilesToLogs(context.Background(), connector.Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalLogs))

assert.False(t, factory.Metadata().SingletonInstance)
}

func TestNewFactoryWithSameTypes(t *testing.T) {
defaultCfg := struct{}{}
factory := NewFactory(testType, func() component.Config { return &defaultCfg },
WithProfilesToProfiles(createProfilesToProfiles, component.StabilityLevelAlpha),
AsSingletonInstance(),
)
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
Expand All @@ -67,8 +64,6 @@ func TestNewFactoryWithSameTypes(t *testing.T) {
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalMetrics))
_, err = factory.CreateProfilesToLogs(context.Background(), connector.Settings{ID: testID}, &defaultCfg, consumertest.NewNop())
assert.Equal(t, err, internal.ErrDataTypes(testID, xpipeline.SignalProfiles, pipeline.SignalLogs))

assert.True(t, factory.Metadata().SingletonInstance)
}

func TestNewFactoryWithTranslateTypes(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestNewFactoryWithOptions(t *testing.T) {

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

assert.True(t, f.Metadata().SingletonInstance)
}
Expand Down
1 change: 1 addition & 0 deletions exporter/xexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestNewFactoryWithProfiles(t *testing.T) {
testType,
func() component.Config { return &defaultCfg },
WithProfiles(createProfiles, component.StabilityLevelDevelopment),
AsSingletonInstance(),
)
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
Expand Down
2 changes: 1 addition & 1 deletion receiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestNewFactoryWithOptions(t *testing.T) {

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

assert.True(t, f.Metadata().SingletonInstance)
}
Expand Down
1 change: 1 addition & 0 deletions receiver/xreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestNewFactoryWithProfiles(t *testing.T) {
testType,
func() component.Config { return &defaultCfg },
WithProfiles(createProfiles, component.StabilityLevelAlpha),
AsSingletonInstance(),
)
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
Expand Down
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,24 @@ 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()),
)
}
Expand All @@ -98,3 +117,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()),
)
}
Loading

0 comments on commit 7772955

Please sign in to comment.