Skip to content

Commit

Permalink
feat(views): add FilteringAttributesProcessor (#2733)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Jan 26, 2022
1 parent 8daaddc commit e1c32b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'
});
});
});

0 comments on commit e1c32b2

Please sign in to comment.