Skip to content
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

feat(views): add FilteringAttributesProcessor #2733

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
});
});
});