From e1c32b2daab842dc96b79a149f496d649491043c Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Wed, 26 Jan 2022 20:21:15 +0100 Subject: [PATCH] feat(views): add FilteringAttributesProcessor (#2733) --- .../src/view/AttributesProcessor.ts | 18 +++++++++++++++ .../test/view/AttributesProcessor.test.ts | 23 +++++++++++++++++++ 2 files changed, 41 insertions(+) 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' + }); + }); +});