Skip to content

Commit

Permalink
Introduce component logger with appropriate attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Feb 4, 2025
1 parent 477e4d3 commit c8b9393
Show file tree
Hide file tree
Showing 34 changed files with 440 additions and 202 deletions.
25 changes: 25 additions & 0 deletions .chloggen/component-logger-api.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: component/componentattribute

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: New module codifies component attributes and provides a zap.Logger for components.

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

# (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/component-logger-memory.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: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Logger no longer attributes to single signal, pipeline, or component.

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

# (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: []
25 changes: 25 additions & 0 deletions .chloggen/component-logger-otlp.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: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Logger no longer attributes to random signal when receiving multiple signals.

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

# (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: []
26 changes: 26 additions & 0 deletions .chloggen/component-logger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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: service

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Align component logger attributes with those defined in RFC

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

# (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: |
See [Pipeline Component Telemetry RFC](https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/rfcs/component-universal-telemetry.md#attributes)
# 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: []
1 change: 1 addition & 0 deletions cmd/otelcorecol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ replaces:
- go.opentelemetry.io/collector/client => ../../client
- go.opentelemetry.io/collector/component => ../../component
- go.opentelemetry.io/collector/component/componenttest => ../../component/componenttest
- go.opentelemetry.io/collector/component/componentattribute => ../../component/componentattribute
- go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus
- go.opentelemetry.io/collector/config/configauth => ../../config/configauth
- go.opentelemetry.io/collector/config/configcompression => ../../config/configcompression
Expand Down
5 changes: 4 additions & 1 deletion cmd/otelcorecol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module go.opentelemetry.io/collector/cmd/otelcorecol

go 1.22.0

toolchain go1.22.11
toolchain go1.23.5

require (
go.opentelemetry.io/collector/component v0.119.0
Expand Down Expand Up @@ -82,6 +82,7 @@ require (
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector v0.119.0 // indirect
go.opentelemetry.io/collector/client v1.25.0 // indirect
go.opentelemetry.io/collector/component/componentattribute v0.119.0 // indirect
go.opentelemetry.io/collector/component/componentstatus v0.119.0 // indirect
go.opentelemetry.io/collector/component/componenttest v0.119.0 // indirect
go.opentelemetry.io/collector/config/configauth v0.119.0 // indirect
Expand Down Expand Up @@ -168,6 +169,8 @@ replace go.opentelemetry.io/collector/component => ../../component

replace go.opentelemetry.io/collector/component/componenttest => ../../component/componenttest

replace go.opentelemetry.io/collector/component/componentattribute => ../../component/componentattribute

replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus

replace go.opentelemetry.io/collector/config/configauth => ../../config/configauth
Expand Down
1 change: 1 addition & 0 deletions component/componentattribute/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
12 changes: 12 additions & 0 deletions component/componentattribute/attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package componentattribute // import "go.opentelemetry.io/collector/component/componentattribute"

const (
ComponentKindKey = "otelcol.component.kind"
ComponentIDKey = "otelcol.component.id"
PipelineIDKey = "otelcol.pipeline.id"
SignalKey = "otelcol.signal"
SignalOutputKey = "otelcol.signal.output"
)
30 changes: 30 additions & 0 deletions component/componentattribute/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module go.opentelemetry.io/collector/component/componentattribute

go 1.22.0

require (
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/collector/pipeline v0.119.0
go.opentelemetry.io/otel v1.34.0
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/collector/config/configtelemetry => ../../config/configtelemetry

replace go.opentelemetry.io/collector/component => ../

replace go.opentelemetry.io/collector/pdata => ../../pdata

replace go.opentelemetry.io/collector/pipeline => ../../pipeline

replace go.opentelemetry.io/collector/pipeline/xpipeline => ../../pipeline/xpipeline
33 changes: 33 additions & 0 deletions component/componentattribute/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions component/componentattribute/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package componentattribute // import "go.opentelemetry.io/collector/component/componentattribute"

import (
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var _ zapcore.Core = (*Core)(nil)

type Core struct {
zapcore.Core
from *zap.Logger
attrs attribute.Set
}

func NewLogger(from *zap.Logger, attrs *attribute.Set) *zap.Logger {
withAttributes := from
for _, kv := range attrs.ToSlice() {
withAttributes = withAttributes.With(zap.String(string(kv.Key), kv.Value.AsString()))
}
return zap.New(&Core{
Core: withAttributes.Core(),
from: from,
attrs: *attrs,
})
}

func (l *Core) Without(keys ...string) *zap.Logger {
excludeKeys := make(map[string]struct{})
for _, key := range keys {
excludeKeys[key] = struct{}{}
}

newAttrs := []attribute.KeyValue{}
withAttributes := l.from
for _, kv := range l.attrs.ToSlice() {
if _, excluded := excludeKeys[string(kv.Key)]; !excluded {
newAttrs = append(newAttrs, kv)
withAttributes = withAttributes.With(zap.String(string(kv.Key), kv.Value.AsString()))
}
}

return zap.New(&Core{
Core: withAttributes.Core(),
from: l.from,
attrs: attribute.NewSet(newAttrs...),
})
}
63 changes: 63 additions & 0 deletions component/componentattribute/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package componentattribute_test

import (
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"

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

func TestLogger(t *testing.T) {
core, observed := observer.New(zap.DebugLevel)
logger := zap.New(core).With(zap.String("preexisting", "value"))

attrs := attribute.NewSet(
attribute.String(componentattribute.SignalKey, pipeline.SignalLogs.String()),
attribute.String(componentattribute.ComponentIDKey, "filelog"),
)

parent := componentattribute.NewLogger(logger, &attrs)
parent.Info("test parent before child")
child := parent.Core().(*componentattribute.Core).Without(string(componentattribute.SignalKey))
child.Info("test child")
parent.Info("test parent after child")

observedLogs := observed.All()
require.Len(t, observedLogs, 3)

parentContext := map[string]string{
"preexisting": "value",
componentattribute.SignalKey: pipeline.SignalLogs.String(),
componentattribute.ComponentIDKey: "filelog",
}
childContext := map[string]string{
"preexisting": "value",
componentattribute.ComponentIDKey: "filelog",
}

require.Equal(t, "test parent before child", observedLogs[0].Message)
require.Len(t, observedLogs[0].Context, len(parentContext))
for _, field := range observedLogs[0].Context {
require.Equal(t, parentContext[field.Key], field.String)
}

require.Equal(t, "test child", observedLogs[1].Message)
require.Len(t, observedLogs[1].Context, len(childContext))
for _, field := range observedLogs[1].Context {
require.Equal(t, childContext[field.Key], field.String)
}

require.Equal(t, "test parent after child", observedLogs[2].Message)
require.Len(t, observedLogs[2].Context, len(parentContext))
for _, field := range observedLogs[2].Context {
require.Equal(t, parentContext[field.Key], field.String)
}
}
3 changes: 3 additions & 0 deletions internal/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ require (
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/client v1.25.0 // indirect
go.opentelemetry.io/collector/component/componentattribute v0.119.0 // indirect
go.opentelemetry.io/collector/config/configauth v0.119.0 // indirect
go.opentelemetry.io/collector/config/configcompression v1.25.0 // indirect
go.opentelemetry.io/collector/config/confignet v1.25.0 // indirect
Expand Down Expand Up @@ -242,3 +243,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../../extension
replace go.opentelemetry.io/collector/extension/auth/authtest => ../../extension/auth/authtest

replace go.opentelemetry.io/collector/extension/xextension => ../../extension/xextension

replace go.opentelemetry.io/collector/component/componentattribute => ../../component/componentattribute
3 changes: 3 additions & 0 deletions otelcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/component/componentattribute v0.119.0 // indirect
go.opentelemetry.io/collector/component/componenttest v0.119.0 // indirect
go.opentelemetry.io/collector/connector/xconnector v0.119.0 // indirect
go.opentelemetry.io/collector/consumer v1.25.0 // indirect
Expand Down Expand Up @@ -204,3 +205,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../extension/ex
replace go.opentelemetry.io/collector/extension/auth/authtest => ../extension/auth/authtest

replace go.opentelemetry.io/collector/extension/xextension => ../extension/xextension

replace go.opentelemetry.io/collector/component/componentattribute => ../component/componentattribute
3 changes: 3 additions & 0 deletions otelcol/otelcoltest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/component/componentattribute v0.119.0 // indirect
go.opentelemetry.io/collector/component/componentstatus v0.119.0 // indirect
go.opentelemetry.io/collector/component/componenttest v0.119.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.119.0 // indirect
Expand Down Expand Up @@ -219,3 +220,5 @@ replace go.opentelemetry.io/collector/confmap => ../../confmap
replace go.opentelemetry.io/collector/processor/processortest => ../../processor/processortest

replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata

replace go.opentelemetry.io/collector/component/componentattribute => ../../component/componentattribute
Loading

0 comments on commit c8b9393

Please sign in to comment.