@@ -7,13 +7,21 @@ import {
7
7
type HookContext ,
8
8
} from '@openfeature/js-sdk' ;
9
9
import { Counter , UpDownCounter , ValueType , metrics } from '@opentelemetry/api' ;
10
- import { ACTIVE_COUNT_NAME , ERROR_TOTAL_NAME , REQUESTS_TOTAL_NAME , SUCCESS_TOTAL_NAME } from '../constants' ;
10
+ import {
11
+ ACTIVE_COUNT_NAME ,
12
+ EXCEPTION_ATTR ,
13
+ ERROR_TOTAL_NAME ,
14
+ EvaluationAttributes ,
15
+ ExceptionAttributes ,
16
+ KEY_ATTR ,
17
+ PROVIDER_NAME_ATTR ,
18
+ REASON_ATTR ,
19
+ REQUESTS_TOTAL_NAME ,
20
+ SUCCESS_TOTAL_NAME ,
21
+ VARIANT_ATTR
22
+ } from '../conventions' ;
11
23
12
- type EvaluationAttributes = Pick < EvaluationDetails < FlagValue > , 'variant' | 'reason' > & {
13
- key : string ;
14
- provider : string ;
15
- } ;
16
- type ErrorEvaluationAttributes = EvaluationAttributes & { exception : string } ;
24
+ type ErrorEvaluationAttributes = EvaluationAttributes & ExceptionAttributes ;
17
25
18
26
const METER_NAME = 'js.openfeature.dev' ;
19
27
@@ -49,35 +57,35 @@ export class MetricsHook implements Hook {
49
57
}
50
58
51
59
before ( hookContext : BeforeHookContext ) {
52
- const attributes = {
53
- key : hookContext . flagKey ,
54
- provider : hookContext . providerMetadata . name ,
60
+ const attributes : EvaluationAttributes = {
61
+ [ KEY_ATTR ] : hookContext . flagKey ,
62
+ [ PROVIDER_NAME_ATTR ] : hookContext . providerMetadata . name ,
55
63
} ;
56
64
this . evaluationActiveUpDownCounter . add ( 1 , attributes ) ;
57
65
this . evaluationRequestCounter . add ( 1 , attributes ) ;
58
66
}
59
67
60
68
after ( hookContext : Readonly < HookContext < FlagValue > > , evaluationDetails : EvaluationDetails < FlagValue > ) {
61
69
this . evaluationSuccessCounter . add ( 1 , {
62
- key : hookContext . flagKey ,
63
- provider : hookContext . providerMetadata . name ,
64
- variant : evaluationDetails . variant ?? evaluationDetails . value ?. toString ( ) ,
65
- reason : evaluationDetails . reason ?? StandardResolutionReasons . UNKNOWN ,
70
+ [ KEY_ATTR ] : hookContext . flagKey ,
71
+ [ PROVIDER_NAME_ATTR ] : hookContext . providerMetadata . name ,
72
+ [ VARIANT_ATTR ] : evaluationDetails . variant ?? evaluationDetails . value ?. toString ( ) ,
73
+ [ REASON_ATTR ] : evaluationDetails . reason ?? StandardResolutionReasons . UNKNOWN ,
66
74
} ) ;
67
75
}
68
76
69
77
error ( hookContext : Readonly < HookContext < FlagValue > > , error : unknown ) {
70
78
this . evaluationErrorCounter . add ( 1 , {
71
- key : hookContext . flagKey ,
72
- provider : hookContext . providerMetadata . name ,
73
- exception : ( error as Error ) ?. message || 'Unknown error' ,
79
+ [ KEY_ATTR ] : hookContext . flagKey ,
80
+ [ PROVIDER_NAME_ATTR ] : hookContext . providerMetadata . name ,
81
+ [ EXCEPTION_ATTR ] : ( error as Error ) ?. message || 'Unknown error' ,
74
82
} ) ;
75
83
}
76
84
77
85
finally ( hookContext : Readonly < HookContext < FlagValue > > ) {
78
86
this . evaluationActiveUpDownCounter . add ( - 1 , {
79
- key : hookContext . flagKey ,
80
- provider : hookContext . providerMetadata . name ,
87
+ [ KEY_ATTR ] : hookContext . flagKey ,
88
+ [ PROVIDER_NAME_ATTR ] : hookContext . providerMetadata . name ,
81
89
} ) ;
82
90
}
83
91
}
0 commit comments