From 0c6f515009f798c7fa62e9ed7dffce30fbd948d3 Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Wed, 16 Mar 2022 09:19:16 -0600 Subject: [PATCH 1/9] Added attribute filtering config and capability --- .../resourcedetectionprocessor/config.go | 3 ++ .../resourcedetectionprocessor/factory.go | 6 ++-- .../internal/resourcedetection.go | 32 ++++++++++++++++--- .../internal/resourcedetection_test.go | 10 +++--- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/processor/resourcedetectionprocessor/config.go b/processor/resourcedetectionprocessor/config.go index 22e2a07e575a..77dc74309280 100644 --- a/processor/resourcedetectionprocessor/config.go +++ b/processor/resourcedetectionprocessor/config.go @@ -39,6 +39,9 @@ type Config struct { // HTTP client settings for the detector // Timeout default is 5s confighttp.HTTPClientSettings `mapstructure:",squash"` + // Attributes is an allowlist of attributes to add. + // If a supplied attribute is not a valid atrtibute of a supplied detector it will be ignored. + Attributes []string `mapstructure:"attributes"` } // DetectorConfig contains user-specified configurations unique to all individual detectors diff --git a/processor/resourcedetectionprocessor/factory.go b/processor/resourcedetectionprocessor/factory.go index e36969519a7a..448d260744c8 100644 --- a/processor/resourcedetectionprocessor/factory.go +++ b/processor/resourcedetectionprocessor/factory.go @@ -96,6 +96,7 @@ func createDefaultConfig() config.Processor { Detectors: []string{env.TypeStr}, HTTPClientSettings: defaultHTTPClientSettings(), Override: true, + Attributes: nil, // TODO: Once issue(https://github.com/open-telemetry/opentelemetry-collector/issues/4001) gets resolved, // Set the default value of 'hostname_source' here instead of 'system' detector } @@ -170,7 +171,7 @@ func (f *factory) getResourceDetectionProcessor( ) (*resourceDetectionProcessor, error) { oCfg := cfg.(*Config) - provider, err := f.getResourceProvider(params, cfg.ID(), oCfg.HTTPClientSettings.Timeout, oCfg.Detectors, oCfg.DetectorConfig) + provider, err := f.getResourceProvider(params, cfg.ID(), oCfg.HTTPClientSettings.Timeout, oCfg.Detectors, oCfg.DetectorConfig, oCfg.Attributes) if err != nil { return nil, err } @@ -189,6 +190,7 @@ func (f *factory) getResourceProvider( timeout time.Duration, configuredDetectors []string, detectorConfigs DetectorConfig, + attributes []string, ) (*internal.ResourceProvider, error) { f.lock.Lock() defer f.lock.Unlock() @@ -202,7 +204,7 @@ func (f *factory) getResourceProvider( detectorTypes = append(detectorTypes, internal.DetectorType(strings.TrimSpace(key))) } - provider, err := f.resourceProviderFactory.CreateResourceProvider(params, timeout, &detectorConfigs, detectorTypes...) + provider, err := f.resourceProviderFactory.CreateResourceProvider(params, timeout, attributes, &detectorConfigs, detectorTypes...) if err != nil { return nil, err } diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection.go b/processor/resourcedetectionprocessor/internal/resourcedetection.go index 3c7be02c7b4c..aa56b8a3444e 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection.go @@ -54,6 +54,7 @@ func NewProviderFactory(detectors map[DetectorType]DetectorFactory) *ResourcePro func (f *ResourceProviderFactory) CreateResourceProvider( params component.ProcessorCreateSettings, timeout time.Duration, + attributes []string, detectorConfigs ResourceDetectorConfig, detectorTypes ...DetectorType) (*ResourceProvider, error) { detectors, err := f.getDetectors(params, detectorConfigs, detectorTypes) @@ -61,7 +62,16 @@ func (f *ResourceProviderFactory) CreateResourceProvider( return nil, err } - provider := NewResourceProvider(params.Logger, timeout, detectors...) + attributesMap := make(map[string]struct{}, len(attributes)) + if attributes != nil && len(attributes) > 0 { + for _, attribute := range attributes { + attributesMap[attribute] = struct{}{} + } + } else { + attributesMap = nil + } + + provider := NewResourceProvider(params.Logger, timeout, attributesMap, detectors...) return provider, nil } @@ -90,6 +100,7 @@ type ResourceProvider struct { detectors []Detector detectedResource *resourceResult once sync.Once + attributes map[string]struct{} } type resourceResult struct { @@ -98,11 +109,12 @@ type resourceResult struct { err error } -func NewResourceProvider(logger *zap.Logger, timeout time.Duration, detectors ...Detector) *ResourceProvider { +func NewResourceProvider(logger *zap.Logger, timeout time.Duration, attributes map[string]struct{}, detectors ...Detector) *ResourceProvider { return &ResourceProvider{ - logger: logger, - timeout: timeout, - detectors: detectors, + logger: logger, + timeout: timeout, + detectors: detectors, + attributes: attributes, } } @@ -131,6 +143,7 @@ func (p *ResourceProvider) detectResource(ctx context.Context) { p.logger.Warn("failed to detect resource", zap.Error(err)) } else { mergedSchemaURL = MergeSchemaURL(mergedSchemaURL, schemaURL) + FilterAttributes(res.Attributes(), p.attributes) MergeResource(res, r, false) } @@ -194,6 +207,15 @@ func MergeSchemaURL(currentSchemaURL string, newSchemaURL string) string { return currentSchemaURL } +func FilterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) { + if attributesToKeep != nil { + am.RemoveIf(func(k string, v pdata.AttributeValue) bool { + _, keep := attributesToKeep[k] + return !keep + }) + } +} + func MergeResource(to, from pdata.Resource, overrideTo bool) { if IsEmptyResource(from) { return diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go index a0dd71084234..00844523dadd 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go @@ -97,7 +97,7 @@ func TestDetect(t *testing.T) { } f := NewProviderFactory(mockDetectors) - p, err := f.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, &mockDetectorConfig{}, mockDetectorTypes...) + p, err := f.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, nil, &mockDetectorConfig{}, mockDetectorTypes...) require.NoError(t, err) got, _, err := p.Get(context.Background(), http.DefaultClient) @@ -113,7 +113,7 @@ func TestDetect(t *testing.T) { func TestDetectResource_InvalidDetectorType(t *testing.T) { mockDetectorKey := DetectorType("mock") p := NewProviderFactory(map[DetectorType]DetectorFactory{}) - _, err := p.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, &mockDetectorConfig{}, mockDetectorKey) + _, err := p.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, nil, &mockDetectorConfig{}, mockDetectorKey) require.EqualError(t, err, fmt.Sprintf("invalid detector key: %v", mockDetectorKey)) } @@ -124,7 +124,7 @@ func TestDetectResource_DetectoryFactoryError(t *testing.T) { return nil, errors.New("creation failed") }, }) - _, err := p.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, &mockDetectorConfig{}, mockDetectorKey) + _, err := p.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, nil, &mockDetectorConfig{}, mockDetectorKey) require.EqualError(t, err, fmt.Sprintf("failed creating detector type %q: %v", mockDetectorKey, "creation failed")) } @@ -135,7 +135,7 @@ func TestDetectResource_Error(t *testing.T) { md2 := &MockDetector{} md2.On("Detect").Return(pdata.NewResource(), errors.New("err1")) - p := NewResourceProvider(zap.NewNop(), time.Second, md1, md2) + p := NewResourceProvider(zap.NewNop(), time.Second, nil, md1, md2) _, _, err := p.Get(context.Background(), http.DefaultClient) require.NoError(t, err) } @@ -205,7 +205,7 @@ func TestDetectResource_Parallel(t *testing.T) { expectedResource := NewResource(map[string]interface{}{"a": "1", "b": "2", "c": "3"}) expectedResource.Attributes().Sort() - p := NewResourceProvider(zap.NewNop(), time.Second, md1, md2, md3) + p := NewResourceProvider(zap.NewNop(), time.Second, nil, md1, md2, md3) // call p.Get multiple times wg := &sync.WaitGroup{} From 67c141bee69c8e4ca717ddf72ada8deddb68585b Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Wed, 16 Mar 2022 16:13:31 -0600 Subject: [PATCH 2/9] Added/updated unit tests --- .../resourcedetectionprocessor/config_test.go | 1 + .../internal/resourcedetection.go | 26 +++-- .../internal/resourcedetection_test.go | 96 ++++++++++++++++++- .../testdata/config.yaml | 1 + 4 files changed, 109 insertions(+), 15 deletions(-) diff --git a/processor/resourcedetectionprocessor/config_test.go b/processor/resourcedetectionprocessor/config_test.go index 7cbbbabb28c0..5ec161b532f3 100644 --- a/processor/resourcedetectionprocessor/config_test.go +++ b/processor/resourcedetectionprocessor/config_test.go @@ -78,6 +78,7 @@ func TestLoadConfig(t *testing.T) { }, HTTPClientSettings: confighttp.HTTPClientSettings{Timeout: 2 * time.Second, MaxIdleConns: p2.MaxIdleConns, IdleConnTimeout: p2.IdleConnTimeout}, Override: false, + Attributes: []string{"a", "b"}, } assert.Equal(t, p4, p4e) } diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection.go b/processor/resourcedetectionprocessor/internal/resourcedetection.go index aa56b8a3444e..c1b3a825f391 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection.go @@ -62,16 +62,14 @@ func (f *ResourceProviderFactory) CreateResourceProvider( return nil, err } - attributesMap := make(map[string]struct{}, len(attributes)) + attributesToKeep := make(map[string]struct{}) if attributes != nil && len(attributes) > 0 { for _, attribute := range attributes { - attributesMap[attribute] = struct{}{} + attributesToKeep[attribute] = struct{}{} } - } else { - attributesMap = nil } - provider := NewResourceProvider(params.Logger, timeout, attributesMap, detectors...) + provider := NewResourceProvider(params.Logger, timeout, attributesToKeep, detectors...) return provider, nil } @@ -100,7 +98,7 @@ type ResourceProvider struct { detectors []Detector detectedResource *resourceResult once sync.Once - attributes map[string]struct{} + attributesToKeep map[string]struct{} } type resourceResult struct { @@ -109,12 +107,12 @@ type resourceResult struct { err error } -func NewResourceProvider(logger *zap.Logger, timeout time.Duration, attributes map[string]struct{}, detectors ...Detector) *ResourceProvider { +func NewResourceProvider(logger *zap.Logger, timeout time.Duration, attributesToKeep map[string]struct{}, detectors ...Detector) *ResourceProvider { return &ResourceProvider{ - logger: logger, - timeout: timeout, - detectors: detectors, - attributes: attributes, + logger: logger, + timeout: timeout, + detectors: detectors, + attributesToKeep: attributesToKeep, } } @@ -143,12 +141,12 @@ func (p *ResourceProvider) detectResource(ctx context.Context) { p.logger.Warn("failed to detect resource", zap.Error(err)) } else { mergedSchemaURL = MergeSchemaURL(mergedSchemaURL, schemaURL) - FilterAttributes(res.Attributes(), p.attributes) MergeResource(res, r, false) } - } + FilterAttributes(res.Attributes(), p.attributesToKeep) + p.logger.Info("detected resource information", zap.Any("resource", AttributesToMap(res.Attributes()))) p.detectedResource.resource = res @@ -208,7 +206,7 @@ func MergeSchemaURL(currentSchemaURL string, newSchemaURL string) string { } func FilterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) { - if attributesToKeep != nil { + if attributesToKeep != nil && len(attributesToKeep) > 0 { am.RemoveIf(func(k string, v pdata.AttributeValue) bool { _, keep := attributesToKeep[k] return !keep diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go index 00844523dadd..afe4d050c4e4 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go @@ -52,6 +52,7 @@ func TestDetect(t *testing.T) { name string detectedResources []pdata.Resource expectedResource pdata.Resource + attributes []string }{ { name: "Detect three resources", @@ -61,6 +62,7 @@ func TestDetect(t *testing.T) { NewResource(map[string]interface{}{"a": "12", "c": "3"}), }, expectedResource: NewResource(map[string]interface{}{"a": "1", "b": "2", "c": "3"}), + attributes: nil, }, { name: "Detect empty resources", detectedResources: []pdata.Resource{ @@ -69,6 +71,7 @@ func TestDetect(t *testing.T) { NewResource(map[string]interface{}{"a": "11"}), }, expectedResource: NewResource(map[string]interface{}{"a": "1", "b": "2"}), + attributes: nil, }, { name: "Detect non-string resources", detectedResources: []pdata.Resource{ @@ -77,6 +80,16 @@ func TestDetect(t *testing.T) { NewResource(map[string]interface{}{"a": "11"}), }, expectedResource: NewResource(map[string]interface{}{"a": "11", "bool": true, "int": int64(2), "double": 0.5}), + attributes: nil, + }, { + name: "Filter to one attribute", + detectedResources: []pdata.Resource{ + NewResource(map[string]interface{}{"a": "1", "b": "2"}), + NewResource(map[string]interface{}{"a": "11", "c": "3"}), + NewResource(map[string]interface{}{"a": "12", "c": "3"}), + }, + expectedResource: NewResource(map[string]interface{}{"a": "1"}), + attributes: []string{"a"}, }, } @@ -97,7 +110,7 @@ func TestDetect(t *testing.T) { } f := NewProviderFactory(mockDetectors) - p, err := f.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, nil, &mockDetectorConfig{}, mockDetectorTypes...) + p, err := f.CreateResourceProvider(componenttest.NewNopProcessorCreateSettings(), time.Second, tt.attributes, &mockDetectorConfig{}, mockDetectorTypes...) require.NoError(t, err) got, _, err := p.Get(context.Background(), http.DefaultClient) @@ -235,6 +248,87 @@ func TestDetectResource_Parallel(t *testing.T) { md3.AssertNumberOfCalls(t, "Detect", 1) } +func TestFilterAttributes_Match(t *testing.T) { + m := map[string]struct{}{ + "host.name": {}, + "host.id": {}, + } + attr := pdata.NewAttributeMap() + attr.InsertString("host.name", "test") + attr.InsertString("host.id", "test") + attr.InsertString("drop.this", "test") + + FilterAttributes(attr, m) + + _, ok := attr.Get("host.name") + assert.True(t, ok) + + _, ok = attr.Get("host.id") + assert.True(t, ok) + + _, ok = attr.Get("drop.this") + assert.False(t, ok) +} + +func TestFilterAttributes_NoMatch(t *testing.T) { + m := map[string]struct{}{ + "cloud.region": {}, + } + attr := pdata.NewAttributeMap() + attr.InsertString("host.name", "test") + attr.InsertString("host.id", "test") + attr.InsertString("drop.this", "test") + + FilterAttributes(attr, m) + + _, ok := attr.Get("host.name") + assert.False(t, ok) + + _, ok = attr.Get("host.id") + assert.False(t, ok) + + _, ok = attr.Get("drop.this") + assert.False(t, ok) +} + +func TestFilterAttributes_NilAttributes(t *testing.T) { + var m map[string]struct{} + attr := pdata.NewAttributeMap() + attr.InsertString("host.name", "test") + attr.InsertString("host.id", "test") + attr.InsertString("drop.this", "test") + + FilterAttributes(attr, m) + + _, ok := attr.Get("host.name") + assert.True(t, ok) + + _, ok = attr.Get("host.id") + assert.True(t, ok) + + _, ok = attr.Get("drop.this") + assert.True(t, ok) +} + +func TestFilterAttributes_NoAttributes(t *testing.T) { + m := make(map[string]struct{}) + attr := pdata.NewAttributeMap() + attr.InsertString("host.name", "test") + attr.InsertString("host.id", "test") + attr.InsertString("drop.this", "test") + + FilterAttributes(attr, m) + + _, ok := attr.Get("host.name") + assert.True(t, ok) + + _, ok = attr.Get("host.id") + assert.True(t, ok) + + _, ok = attr.Get("drop.this") + assert.True(t, ok) +} + func TestAttributesToMap(t *testing.T) { m := map[string]interface{}{ "str": "a", diff --git a/processor/resourcedetectionprocessor/testdata/config.yaml b/processor/resourcedetectionprocessor/testdata/config.yaml index f43e409f56f2..3b591fb74fbd 100644 --- a/processor/resourcedetectionprocessor/testdata/config.yaml +++ b/processor/resourcedetectionprocessor/testdata/config.yaml @@ -25,6 +25,7 @@ processors: override: false system: hostname_sources: [os] + attributes: ["a", "b"] resourcedetection/docker: detectors: [env, docker] timeout: 2s From dbc165b5f963614029e34a51321e2b43cecc3d99 Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Thu, 17 Mar 2022 08:04:37 -0600 Subject: [PATCH 3/9] Updated config documentation --- processor/resourcedetectionprocessor/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/processor/resourcedetectionprocessor/README.md b/processor/resourcedetectionprocessor/README.md index 1c6e64648a63..785b9b283bff 100644 --- a/processor/resourcedetectionprocessor/README.md +++ b/processor/resourcedetectionprocessor/README.md @@ -268,6 +268,8 @@ processors: detectors: [ ] # determines if existing resource attributes should be overridden or preserved, defaults to true override: +# When included, only attributes in the list will be appened. Applies to all detectors. +attributes: [ ] ``` ## Ordering From 9ee9df0b1798663e50cb91505a88cee8c4d0281a Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Thu, 17 Mar 2022 10:29:16 -0600 Subject: [PATCH 4/9] Updated unit tests --- .../internal/resourcedetection_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go index afe4d050c4e4..960b294ee66f 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go @@ -277,7 +277,6 @@ func TestFilterAttributes_NoMatch(t *testing.T) { attr := pdata.NewAttributeMap() attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - attr.InsertString("drop.this", "test") FilterAttributes(attr, m) @@ -286,9 +285,6 @@ func TestFilterAttributes_NoMatch(t *testing.T) { _, ok = attr.Get("host.id") assert.False(t, ok) - - _, ok = attr.Get("drop.this") - assert.False(t, ok) } func TestFilterAttributes_NilAttributes(t *testing.T) { @@ -296,7 +292,6 @@ func TestFilterAttributes_NilAttributes(t *testing.T) { attr := pdata.NewAttributeMap() attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - attr.InsertString("drop.this", "test") FilterAttributes(attr, m) @@ -305,9 +300,6 @@ func TestFilterAttributes_NilAttributes(t *testing.T) { _, ok = attr.Get("host.id") assert.True(t, ok) - - _, ok = attr.Get("drop.this") - assert.True(t, ok) } func TestFilterAttributes_NoAttributes(t *testing.T) { @@ -315,7 +307,6 @@ func TestFilterAttributes_NoAttributes(t *testing.T) { attr := pdata.NewAttributeMap() attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - attr.InsertString("drop.this", "test") FilterAttributes(attr, m) @@ -324,9 +315,6 @@ func TestFilterAttributes_NoAttributes(t *testing.T) { _, ok = attr.Get("host.id") assert.True(t, ok) - - _, ok = attr.Get("drop.this") - assert.True(t, ok) } func TestAttributesToMap(t *testing.T) { From 74641450adfe4401b8bb47394e3ce490f4306ffb Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Thu, 17 Mar 2022 10:44:54 -0600 Subject: [PATCH 5/9] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 138cee423aca..566c64371d7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- `resourcedetectionprocessor`: Add attribute allowlist (#8547) + ## v0.47.0 ### 💡 Enhancements 💡 From a6cc4fdb5c5aed9247efcbd4662f6b9da283e31b Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Thu, 17 Mar 2022 11:19:23 -0600 Subject: [PATCH 6/9] Fix lint issues --- .../resourcedetectionprocessor/internal/resourcedetection.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection.go b/processor/resourcedetectionprocessor/internal/resourcedetection.go index c1b3a825f391..1a2f9b3e1e53 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection.go @@ -63,7 +63,7 @@ func (f *ResourceProviderFactory) CreateResourceProvider( } attributesToKeep := make(map[string]struct{}) - if attributes != nil && len(attributes) > 0 { + if len(attributes) > 0 { for _, attribute := range attributes { attributesToKeep[attribute] = struct{}{} } @@ -206,7 +206,7 @@ func MergeSchemaURL(currentSchemaURL string, newSchemaURL string) string { } func FilterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) { - if attributesToKeep != nil && len(attributesToKeep) > 0 { + if len(attributesToKeep) > 0 { am.RemoveIf(func(k string, v pdata.AttributeValue) bool { _, keep := attributesToKeep[k] return !keep From 3d865ca961c3fad8bd62a1c902c45fa4e81df493 Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Fri, 18 Mar 2022 12:39:36 -0600 Subject: [PATCH 7/9] Improved logging --- .../internal/resourcedetection.go | 13 +++++++++++-- .../internal/resourcedetection_test.go | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection.go b/processor/resourcedetectionprocessor/internal/resourcedetection.go index 1a2f9b3e1e53..78b5ee866b9c 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection.go @@ -145,7 +145,10 @@ func (p *ResourceProvider) detectResource(ctx context.Context) { } } - FilterAttributes(res.Attributes(), p.attributesToKeep) + droppedAttributes := FilterAttributes(res.Attributes(), p.attributesToKeep) + if len(droppedAttributes) > 0 { + p.logger.Info("dropped resource information", zap.Strings("resource keys", droppedAttributes)) + } p.logger.Info("detected resource information", zap.Any("resource", AttributesToMap(res.Attributes()))) @@ -205,13 +208,19 @@ func MergeSchemaURL(currentSchemaURL string, newSchemaURL string) string { return currentSchemaURL } -func FilterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) { +func FilterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) []string { if len(attributesToKeep) > 0 { + droppedAttributes := make([]string, 0) am.RemoveIf(func(k string, v pdata.AttributeValue) bool { _, keep := attributesToKeep[k] + if !keep { + droppedAttributes = append(droppedAttributes, k) + } return !keep }) + return droppedAttributes } + return nil } func MergeResource(to, from pdata.Resource, overrideTo bool) { diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go index 960b294ee66f..4b0b6afee1dd 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go @@ -258,7 +258,7 @@ func TestFilterAttributes_Match(t *testing.T) { attr.InsertString("host.id", "test") attr.InsertString("drop.this", "test") - FilterAttributes(attr, m) + droppedAttributes := FilterAttributes(attr, m) _, ok := attr.Get("host.name") assert.True(t, ok) @@ -268,6 +268,8 @@ func TestFilterAttributes_Match(t *testing.T) { _, ok = attr.Get("drop.this") assert.False(t, ok) + + assert.Contains(t, droppedAttributes, "drop.this") } func TestFilterAttributes_NoMatch(t *testing.T) { @@ -278,13 +280,15 @@ func TestFilterAttributes_NoMatch(t *testing.T) { attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - FilterAttributes(attr, m) + droppedAttributes := FilterAttributes(attr, m) _, ok := attr.Get("host.name") assert.False(t, ok) _, ok = attr.Get("host.id") assert.False(t, ok) + + assert.EqualValues(t, droppedAttributes, []string{"host.name", "host.id"}) } func TestFilterAttributes_NilAttributes(t *testing.T) { @@ -293,13 +297,15 @@ func TestFilterAttributes_NilAttributes(t *testing.T) { attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - FilterAttributes(attr, m) + droppedAttributes := FilterAttributes(attr, m) _, ok := attr.Get("host.name") assert.True(t, ok) _, ok = attr.Get("host.id") assert.True(t, ok) + + assert.Equal(t, len(droppedAttributes), 0) } func TestFilterAttributes_NoAttributes(t *testing.T) { @@ -308,13 +314,15 @@ func TestFilterAttributes_NoAttributes(t *testing.T) { attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - FilterAttributes(attr, m) + droppedAttributes := FilterAttributes(attr, m) _, ok := attr.Get("host.name") assert.True(t, ok) _, ok = attr.Get("host.id") assert.True(t, ok) + + assert.Equal(t, len(droppedAttributes), 0) } func TestAttributesToMap(t *testing.T) { From 51e2522cd718bf4a76ccaa03ec5ef3139983baa4 Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Mon, 21 Mar 2022 09:15:02 -0600 Subject: [PATCH 8/9] updated function to not be exported --- .../internal/resourcedetection.go | 8 ++++---- .../internal/resourcedetection_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection.go b/processor/resourcedetectionprocessor/internal/resourcedetection.go index 78b5ee866b9c..28b972a2ec13 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection.go @@ -145,13 +145,13 @@ func (p *ResourceProvider) detectResource(ctx context.Context) { } } - droppedAttributes := FilterAttributes(res.Attributes(), p.attributesToKeep) + droppedAttributes := filterAttributes(res.Attributes(), p.attributesToKeep) + + p.logger.Info("detected resource information", zap.Any("resource", AttributesToMap(res.Attributes()))) if len(droppedAttributes) > 0 { p.logger.Info("dropped resource information", zap.Strings("resource keys", droppedAttributes)) } - p.logger.Info("detected resource information", zap.Any("resource", AttributesToMap(res.Attributes()))) - p.detectedResource.resource = res p.detectedResource.schemaURL = mergedSchemaURL } @@ -208,7 +208,7 @@ func MergeSchemaURL(currentSchemaURL string, newSchemaURL string) string { return currentSchemaURL } -func FilterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) []string { +func filterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) []string { if len(attributesToKeep) > 0 { droppedAttributes := make([]string, 0) am.RemoveIf(func(k string, v pdata.AttributeValue) bool { diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go index 4b0b6afee1dd..63a818ded9f4 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection_test.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection_test.go @@ -258,7 +258,7 @@ func TestFilterAttributes_Match(t *testing.T) { attr.InsertString("host.id", "test") attr.InsertString("drop.this", "test") - droppedAttributes := FilterAttributes(attr, m) + droppedAttributes := filterAttributes(attr, m) _, ok := attr.Get("host.name") assert.True(t, ok) @@ -280,7 +280,7 @@ func TestFilterAttributes_NoMatch(t *testing.T) { attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - droppedAttributes := FilterAttributes(attr, m) + droppedAttributes := filterAttributes(attr, m) _, ok := attr.Get("host.name") assert.False(t, ok) @@ -297,7 +297,7 @@ func TestFilterAttributes_NilAttributes(t *testing.T) { attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - droppedAttributes := FilterAttributes(attr, m) + droppedAttributes := filterAttributes(attr, m) _, ok := attr.Get("host.name") assert.True(t, ok) @@ -314,7 +314,7 @@ func TestFilterAttributes_NoAttributes(t *testing.T) { attr.InsertString("host.name", "test") attr.InsertString("host.id", "test") - droppedAttributes := FilterAttributes(attr, m) + droppedAttributes := filterAttributes(attr, m) _, ok := attr.Get("host.name") assert.True(t, ok) From 0ead18e33079bd82e0ffc8dc70d508be75551000 Mon Sep 17 00:00:00 2001 From: TylerHelmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Tue, 22 Mar 2022 08:35:48 -0600 Subject: [PATCH 9/9] Fix lint issue --- .../resourcedetectionprocessor/internal/resourcedetection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/processor/resourcedetectionprocessor/internal/resourcedetection.go b/processor/resourcedetectionprocessor/internal/resourcedetection.go index dfdc2ab79e36..356f9c4586ec 100644 --- a/processor/resourcedetectionprocessor/internal/resourcedetection.go +++ b/processor/resourcedetectionprocessor/internal/resourcedetection.go @@ -211,7 +211,7 @@ func MergeSchemaURL(currentSchemaURL string, newSchemaURL string) string { func filterAttributes(am pdata.AttributeMap, attributesToKeep map[string]struct{}) []string { if len(attributesToKeep) > 0 { droppedAttributes := make([]string, 0) - am.RemoveIf(func(k string, v pdata.AttributeValue) bool { + am.RemoveIf(func(k string, v pdata.Value) bool { _, keep := attributesToKeep[k] if !keep { droppedAttributes = append(droppedAttributes, k)