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 21, 2025
1 parent 819f5a6 commit 98fe10f
Show file tree
Hide file tree
Showing 35 changed files with 799 additions and 450 deletions.
25 changes: 25 additions & 0 deletions .chloggen/singleton-flags-and-attributes-exporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: exporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `Metadata` method to `Factory` interface.

# One or more tracking issues or pull requests related to the change
issues: [12057]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
25 changes: 25 additions & 0 deletions .chloggen/singleton-flags-and-attributes-receiver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: receiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `Metadata` method to `Factory` interface.

# One or more tracking issues or pull requests related to the change
issues: [12057]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
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
23 changes: 23 additions & 0 deletions service/internal/attribute/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"hash/fnv"

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

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pipeline"
Expand Down Expand Up @@ -47,6 +48,14 @@ 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, component.KindReceiver.String()),
Expand All @@ -55,6 +64,13 @@ func Receiver(pipelineType pipeline.Signal, id component.ID) *Attributes {
)
}

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, component.KindProcessor.String()),
Expand All @@ -72,6 +88,13 @@ func Exporter(pipelineType pipeline.Signal, id component.ID) *Attributes {
)
}

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, component.KindConnector.String()),
Expand Down
38 changes: 38 additions & 0 deletions service/internal/attribute/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ func TestReceiver(t *testing.T) {
}
}

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.String(), 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 {
Expand Down Expand Up @@ -96,6 +109,19 @@ func TestExporter(t *testing.T) {
}
}

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.String(), 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 {
Expand Down Expand Up @@ -155,6 +181,12 @@ func createExampleSets() []*Attributes {
sets = append(sets, Receiver(sig, id))
}
}
<<<<<<< HEAD

Check failure on line 184 in service/internal/attribute/attribute_test.go

View workflow job for this annotation

GitHub Actions / checks

expected statement, found '<<'
=======
for _, id := range cIDs {
sets = append(sets, ReceiverSingleton(id))
}
>>>>>>> c498d3e44 (Use attributes in loggers)

Check failure on line 189 in service/internal/attribute/attribute_test.go

View workflow job for this annotation

GitHub Actions / checks

expected statement, found '>>'

// Processor examples.
for _, pID := range pIDs {
Expand All @@ -169,6 +201,12 @@ func createExampleSets() []*Attributes {
sets = append(sets, Exporter(sig, id))
}
}
<<<<<<< HEAD

Check failure on line 204 in service/internal/attribute/attribute_test.go

View workflow job for this annotation

GitHub Actions / checks

expected statement, found '<<'
=======
for _, id := range cIDs {
sets = append(sets, ExporterSingleton(id))
}
>>>>>>> c498d3e44 (Use attributes in loggers)

Check failure on line 209 in service/internal/attribute/attribute_test.go

View workflow job for this annotation

GitHub Actions / checks

expected statement, found '>>'

// Connector examples.
for _, exprSig := range signals {
Expand Down
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
2 changes: 1 addition & 1 deletion service/internal/builders/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (b *ProcessorBuilder) CreateProfiles(ctx context.Context, set processor.Set
return f.CreateProfiles(ctx, set, cfg, next)
}

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

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

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

Expand Down
Loading

0 comments on commit 98fe10f

Please sign in to comment.