-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
c8b9393
a9d57b9
07a6e5b
1f629b7
95a3234
662c1db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] |
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: [] |
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: [] |
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: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../Makefile.Common |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not couple this change with breaking the keys.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you're suggesting |
||
) |
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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} | ||
} |
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking change.