14
14
* limitations under the License.
15
15
*/
16
16
17
- import { Context , MetricAttributes } from '@opentelemetry/api' ;
17
+ import { Context , Attributes } from '@opentelemetry/api' ;
18
18
19
19
/**
20
20
* The {@link AttributesProcessor} is responsible for customizing which
21
21
* attribute(s) are to be reported as metrics dimension(s) and adding
22
22
* additional dimension(s) from the {@link Context}.
23
23
*/
24
- export abstract class AttributesProcessor {
24
+ export interface IAttributesProcessor {
25
25
/**
26
26
* Process the metric instrument attributes.
27
27
*
28
28
* @param incoming The metric instrument attributes.
29
29
* @param context The active context when the instrument is synchronous.
30
30
* `undefined` otherwise.
31
31
*/
32
- abstract process (
33
- incoming : MetricAttributes ,
34
- context ?: Context
35
- ) : MetricAttributes ;
36
-
37
- static Noop ( ) {
38
- return NOOP ;
39
- }
32
+ process : ( incoming : Attributes , context ?: Context ) => Attributes ;
40
33
}
41
34
42
- export class NoopAttributesProcessor extends AttributesProcessor {
43
- process ( incoming : MetricAttributes , _context ?: Context ) {
35
+ class NoopAttributesProcessor implements IAttributesProcessor {
36
+ process ( incoming : Attributes , _context ?: Context ) {
44
37
return incoming ;
45
38
}
46
39
}
47
40
48
- /**
49
- * {@link AttributesProcessor } that filters by allowed attribute names and drops any names that are not in the
50
- * allow list.
51
- */
52
- export class FilteringAttributesProcessor extends AttributesProcessor {
53
- constructor ( private _allowedAttributeNames : string [ ] ) {
54
- super ( ) ;
41
+ class MultiAttributesProcessor implements IAttributesProcessor {
42
+ constructor ( private readonly _processors : IAttributesProcessor [ ] ) { }
43
+ process ( incoming : Attributes , context ?: Context ) : Attributes {
44
+ let filteredAttributes = incoming ;
45
+ for ( const processor of this . _processors ) {
46
+ filteredAttributes = processor . process ( filteredAttributes , context ) ;
47
+ }
48
+ return filteredAttributes ;
55
49
}
50
+ }
51
+
52
+ class AllowListProcessor implements IAttributesProcessor {
53
+ constructor ( private _allowedAttributeNames : string [ ] ) { }
56
54
57
- process ( incoming : MetricAttributes , _context : Context ) : MetricAttributes {
58
- const filteredAttributes : MetricAttributes = { } ;
55
+ process ( incoming : Attributes , _context ? : Context ) : Attributes {
56
+ const filteredAttributes : Attributes = { } ;
59
57
Object . keys ( incoming )
60
58
. filter ( attributeName =>
61
59
this . _allowedAttributeNames . includes ( attributeName )
@@ -68,4 +66,62 @@ export class FilteringAttributesProcessor extends AttributesProcessor {
68
66
}
69
67
}
70
68
69
+ class DenyListProcessor implements IAttributesProcessor {
70
+ constructor ( private _deniedAttributeNames : string [ ] ) { }
71
+
72
+ process ( incoming : Attributes , _context ?: Context ) : Attributes {
73
+ const filteredAttributes : Attributes = { } ;
74
+ Object . keys ( incoming )
75
+ . filter (
76
+ attributeName => ! this . _deniedAttributeNames . includes ( attributeName )
77
+ )
78
+ . forEach (
79
+ attributeName =>
80
+ ( filteredAttributes [ attributeName ] = incoming [ attributeName ] )
81
+ ) ;
82
+ return filteredAttributes ;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * @internal
88
+ *
89
+ * Create an {@link IAttributesProcessor} that acts as a simple pass-through for attributes.
90
+ */
91
+ export function createNoopAttributesProcessor ( ) : IAttributesProcessor {
92
+ return NOOP ;
93
+ }
94
+
95
+ /**
96
+ * @internal
97
+ *
98
+ * Create an {@link IAttributesProcessor} that applies all processors from the provided list in order.
99
+ *
100
+ * @param processors Processors to apply in order.
101
+ */
102
+ export function createMultiAttributesProcessor (
103
+ processors : IAttributesProcessor [ ]
104
+ ) : IAttributesProcessor {
105
+ return new MultiAttributesProcessor ( processors ) ;
106
+ }
107
+
108
+ /**
109
+ * Create an {@link IAttributesProcessor} that filters by allowed attribute names and drops any names that are not in the
110
+ * allow list.
111
+ */
112
+ export function createAllowListAttributesProcessor (
113
+ attributeAllowList : string [ ]
114
+ ) : IAttributesProcessor {
115
+ return new AllowListProcessor ( attributeAllowList ) ;
116
+ }
117
+
118
+ /**
119
+ * Create an {@link IAttributesProcessor} that drops attributes based on the names provided in the deny list
120
+ */
121
+ export function createDenyListAttributesProcessor (
122
+ attributeDenyList : string [ ]
123
+ ) : IAttributesProcessor {
124
+ return new DenyListProcessor ( attributeDenyList ) ;
125
+ }
126
+
71
127
const NOOP = new NoopAttributesProcessor ( ) ;
0 commit comments