diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts index 5ed8d2c0ab..0a6aa4706e 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts @@ -43,4 +43,22 @@ export class NoopAttributesProcessor extends AttributesProcessor { } } +/** + * {@link AttributesProcessor} that filters by allowed attribute names and drops any names that are not in the + * allow list. + */ +export class FilteringAttributesProcessor extends AttributesProcessor { + constructor(private _allowedAttributeNames: string[]) { + super(); + } + + process(incoming: Attributes, _context: Context): Attributes { + const filteredAttributes: Attributes = {}; + Object.keys(incoming) + .filter(attributeName => this._allowedAttributeNames.includes(attributeName)) + .forEach(attributeName => filteredAttributes[attributeName] = incoming[attributeName]); + return filteredAttributes; + } +} + const NOOP = new NoopAttributesProcessor; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts index fd7ea105fb..1a0db429e5 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts @@ -17,6 +17,7 @@ import * as assert from 'assert'; import { context } from '@opentelemetry/api'; import { NoopAttributesProcessor } from '../../src/view/AttributesProcessor'; +import { FilteringAttributesProcessor } from '../../src/view/AttributesProcessor'; describe('NoopAttributesProcessor', () => { const processor = new NoopAttributesProcessor(); @@ -30,3 +31,25 @@ describe('NoopAttributesProcessor', () => { ); }); }); + +describe('FilteringAttributesProcessor', () => { + it('should not add keys when attributes do not exist', () => { + const processor = new FilteringAttributesProcessor(['foo', 'bar']); + assert.deepStrictEqual( + processor.process({}, context.active()), {}); + }); + + it('should only keep allowed attributes', () => { + const processor = new FilteringAttributesProcessor(['foo', 'bar']); + assert.deepStrictEqual( + processor.process({ + foo: 'fooValue', + bar: 'barValue', + baz: 'bazValue' + }, context.active()), + { + foo: 'fooValue', + bar: 'barValue' + }); + }); +});