-
Notifications
You must be signed in to change notification settings - Fork 258
feat(nodemetadataenricher): Add log signal support #2095
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
Open
miconeilaws
wants to merge
4
commits into
main
Choose a base branch
from
miconeil/nodemetadataenricher-log-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5d08899
feat(nodemetadataenricher): Add log signal support
miconeilaws 13c8e83
test(nodemetadataenricher): Add log signal test coverage
louisall c9acaeb
Merge branch 'main' into miconeil/nodemetadataenricher-log-support
movence 0224480
refactor(nodemetadataenricher): Extract shared enrichment helpers and…
louisall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,92 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package nodemetadataenricher | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/consumer/consumertest" | ||
| "go.opentelemetry.io/collector/processor/processortest" | ||
| ) | ||
|
|
||
| // dummyConfig is an alternate component.Config used to exercise the | ||
| // configuration-type assertion error path in createMetricsProcessor and | ||
| // createLogsProcessor. It does NOT embed the processor's *Config type, so the | ||
| // `cfg.(*Config)` assertion in the factory functions should fail. | ||
| type dummyConfig struct{} | ||
|
|
||
| func TestNewFactory(t *testing.T) { | ||
| factory := NewFactory() | ||
|
|
||
| require.NotNil(t, factory) | ||
| assert.Equal(t, TypeStr, factory.Type()) | ||
|
|
||
| // Both metric and log signal stability levels should be set. | ||
| assert.Equal(t, stability, factory.MetricsStability()) | ||
| assert.Equal(t, stability, factory.LogsStability()) | ||
| } | ||
|
|
||
| func TestCreateDefaultConfig(t *testing.T) { | ||
| cfg := createDefaultConfig() | ||
|
|
||
| require.NotNil(t, cfg) | ||
| _, ok := cfg.(*Config) | ||
| assert.True(t, ok, "createDefaultConfig should return *Config") | ||
| } | ||
|
|
||
| func TestCreateMetricsProcessor(t *testing.T) { | ||
| factory := NewFactory() | ||
| cfg := factory.CreateDefaultConfig() | ||
|
|
||
| p, err := factory.CreateMetrics( | ||
| context.Background(), | ||
| processortest.NewNopSettings(factory.Type()), | ||
| cfg, | ||
| consumertest.NewNop(), | ||
| ) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, p) | ||
| assert.True(t, p.Capabilities().MutatesData, "processor should declare it mutates data") | ||
| } | ||
|
|
||
| func TestCreateMetricsProcessor_BadConfigType(t *testing.T) { | ||
| p, err := createMetricsProcessor( | ||
| context.Background(), | ||
| processortest.NewNopSettings(component.MustNewType("nodemetadataenricher")), | ||
| &dummyConfig{}, | ||
| consumertest.NewNop(), | ||
| ) | ||
| assert.Error(t, err, "createMetricsProcessor should reject a config of the wrong type") | ||
| assert.Nil(t, p) | ||
| } | ||
|
|
||
| func TestCreateLogsProcessor(t *testing.T) { | ||
| factory := NewFactory() | ||
| cfg := factory.CreateDefaultConfig() | ||
|
|
||
| p, err := factory.CreateLogs( | ||
| context.Background(), | ||
| processortest.NewNopSettings(factory.Type()), | ||
| cfg, | ||
| consumertest.NewNop(), | ||
| ) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, p) | ||
| assert.True(t, p.Capabilities().MutatesData, "processor should declare it mutates data") | ||
| } | ||
|
|
||
| func TestCreateLogsProcessor_BadConfigType(t *testing.T) { | ||
| p, err := createLogsProcessor( | ||
| context.Background(), | ||
| processortest.NewNopSettings(component.MustNewType("nodemetadataenricher")), | ||
| &dummyConfig{}, | ||
| consumertest.NewNop(), | ||
| ) | ||
| assert.Error(t, err, "createLogsProcessor should reject a config of the wrong type") | ||
| assert.Nil(t, p) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import ( | |
| "context" | ||
| "sync/atomic" | ||
|
|
||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| "go.opentelemetry.io/collector/pdata/plog" | ||
| "go.opentelemetry.io/collector/pdata/pmetric" | ||
| "go.uber.org/zap" | ||
|
|
||
|
|
@@ -37,38 +39,68 @@ func newNodeMetadataEnricherProcessor(logger *zap.Logger) *nodeMetadataEnricherP | |
| return p | ||
| } | ||
|
|
||
| func (p *nodeMetadataEnricherProcessor) processMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) { | ||
| // resolveCache returns the node metadata cache, lazily initializing the | ||
| // processor's reference if the cache extension was not ready at construction | ||
| // time. Returns nil if the cache is still unavailable. | ||
| func (p *nodeMetadataEnricherProcessor) resolveCache() *nodemetadatacache.NodeMetadataCache { | ||
| cache := p.cache.Load() | ||
| if cache != nil { | ||
| return cache | ||
| } | ||
| cache = nodemetadatacache.GetNodeMetadataCache() | ||
| if cache != nil { | ||
| p.logger.Debug("Lazily initialized node metadata cache reference") | ||
| p.cache.Store(cache) | ||
| } | ||
| return cache | ||
| } | ||
|
|
||
| // enrichResource applies node metadata attributes to a single resource if it | ||
| // has a non-empty k8s.node.name attribute and a corresponding entry exists in | ||
| // the cache. Resources without a node name or with a cache miss are left | ||
| // untouched. | ||
| func (p *nodeMetadataEnricherProcessor) enrichResource(resource pcommon.Resource, cache *nodemetadatacache.NodeMetadataCache) { | ||
| nodeNameVal, exists := resource.Attributes().Get(attrNodeName) | ||
| if !exists || nodeNameVal.Str() == "" { | ||
| return | ||
| } | ||
|
|
||
| metadata := cache.Get(nodeNameVal.Str()) | ||
| if metadata == nil { | ||
| return | ||
| } | ||
|
|
||
| resource.Attributes().PutStr(attrHostID, metadata.HostID) | ||
| resource.Attributes().PutStr(attrHostName, metadata.HostName) | ||
| resource.Attributes().PutStr(attrHostType, metadata.HostType) | ||
| resource.Attributes().PutStr(attrHostImageID, metadata.HostImageID) | ||
| resource.Attributes().PutStr(attrAvailabilityZone, metadata.AvailabilityZone) | ||
| } | ||
|
|
||
| func (p *nodeMetadataEnricherProcessor) processMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) { | ||
| cache := p.resolveCache() | ||
| if cache == nil { | ||
| // Extension may not have been ready at creation time — retry. | ||
| cache = nodemetadatacache.GetNodeMetadataCache() | ||
| if cache != nil { | ||
| p.logger.Debug("Lazily initialized node metadata cache reference") | ||
| p.cache.Store(cache) | ||
| } else { | ||
| return md, nil | ||
| } | ||
| return md, nil | ||
| } | ||
|
|
||
| rms := md.ResourceMetrics() | ||
| for i := 0; i < rms.Len(); i++ { | ||
| resource := rms.At(i).Resource() | ||
| nodeNameVal, exists := resource.Attributes().Get(attrNodeName) | ||
| if !exists || nodeNameVal.Str() == "" { | ||
| continue | ||
| } | ||
|
|
||
| metadata := cache.Get(nodeNameVal.Str()) | ||
| if metadata == nil { | ||
| continue | ||
| } | ||
|
|
||
| resource.Attributes().PutStr(attrHostID, metadata.HostID) | ||
| resource.Attributes().PutStr(attrHostName, metadata.HostName) | ||
| resource.Attributes().PutStr(attrHostType, metadata.HostType) | ||
| resource.Attributes().PutStr(attrHostImageID, metadata.HostImageID) | ||
| resource.Attributes().PutStr(attrAvailabilityZone, metadata.AvailabilityZone) | ||
| p.enrichResource(rms.At(i).Resource(), cache) | ||
| } | ||
|
|
||
| return md, nil | ||
| } | ||
|
|
||
| func (p *nodeMetadataEnricherProcessor) processLogs(_ context.Context, ld plog.Logs) (plog.Logs, error) { | ||
|
sky333999 marked this conversation as resolved.
Contributor
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. seems like this is an almost duplicate of processMetrics, can we extract the common functionality into a util function and use it inside both processing functions ? |
||
| cache := p.resolveCache() | ||
| if cache == nil { | ||
| return ld, nil | ||
| } | ||
|
|
||
| rls := ld.ResourceLogs() | ||
| for i := 0; i < rls.Len(); i++ { | ||
| p.enrichResource(rls.At(i).Resource(), cache) | ||
| } | ||
|
|
||
| return ld, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
missing unit tests for this function and maybe this file as well