-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce component logger with appropriate attributes
- Loading branch information
1 parent
477e4d3
commit c8b9393
Showing
34 changed files
with
440 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../Makefile.Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.