Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce component logger with appropriate attributes #12259

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking change.


# 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: []
3 changes: 2 additions & 1 deletion cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ require (
var replaceModules = []string{
"",
"/component",
"/component/componenttest",
"/component/componentattribute",
"/component/componentstatus",
"/component/componenttest",
"/client",
"/config/configauth",
"/config/configcompression",
Expand Down
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
3 changes: 3 additions & 0 deletions cmd/otelcorecol/go.mod
Original file line number Diff line number Diff line change
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 @@ -169,6 +170,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"
Comment on lines +7 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not couple this change with breaking the keys.

	zapKindKey            = "kind"
	zapNameKey            = "name"
	zapDataTypeKey        = "data_type"
	zapStabilityKey       = "stability"
	zapPipelineKey        = "pipeline"
	zapExporterInPipeline = "exporter_in_pipeline"
	zapReceiverInPipeline = "receiver_in_pipeline"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you're suggesting

)
22 changes: 22 additions & 0 deletions component/componentattribute/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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/pipeline => ../../pipeline
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.

46 changes: 46 additions & 0 deletions component/componentattribute/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 = (*coreWithout)(nil)

type coreWithout struct {
zapcore.Core
from zapcore.Core
fields []zap.Field
}

func NewLogger(logger *zap.Logger, attrs *attribute.Set) *zap.Logger {
fields := []zap.Field{}
for _, kv := range attrs.ToSlice() {
fields = append(fields, zap.String(string(kv.Key), kv.Value.AsString()))
}
return logger.WithOptions(
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return &coreWithout{Core: core.With(fields), from: core, fields: fields}
}),
)
}

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

fieldsWithout := []zap.Field{}
for _, field := range l.fields {
if _, excluded := excludeKeys[field.Key]; !excluded {
fieldsWithout = append(fieldsWithout, field)
}
}

return &coreWithout{Core: l.from.With(fieldsWithout), from: l.from, fields: fieldsWithout}
}
69 changes: 69 additions & 0 deletions component/componentattribute/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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/zapcore"
"go.uber.org/zap/zaptest/observer"

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

type loggerCore interface {
Without(fields ...string) zapcore.Core
}

func TestCore(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")
childCore := parent.Core().(loggerCore).Without(string(componentattribute.SignalKey))
child := zap.New(childCore)
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)
}
}
15 changes: 15 additions & 0 deletions component/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/pdata/pcommon"
)
Expand All @@ -26,3 +27,17 @@
// Resource contains the resource attributes for the collector's telemetry.
Resource pcommon.Resource
}

func (ts *TelemetrySettings) LoggerWithout(fields ...string) *zap.Logger {
type coreWithout interface {
Without(fields ...string) zapcore.Core
}
if _, ok := ts.Logger.Core().(coreWithout); !ok {
return ts.Logger
}
return ts.Logger.WithOptions(
zap.WrapCore(func(from zapcore.Core) zapcore.Core {
return from.(coreWithout).Without(fields...)
}),

Check warning on line 41 in component/telemetry.go

View check run for this annotation

Codecov / codecov/patch

component/telemetry.go#L31-L41

Added lines #L31 - L41 were not covered by tests
)
}
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 @@ -244,3 +245,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 @@ -71,6 +71,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 @@ -207,3 +208,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 @@ -66,6 +66,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 @@ -222,3 +223,5 @@ replace go.opentelemetry.io/collector/confmap/xconfmap => ../../confmap/xconfmap
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
Loading