1
- import { EvaluationDetails , FlagValue , Hook , HookContext , Logger } from '@openfeature/web-sdk' ;
2
- import { ExporterMetadataValue , FeatureEvent , GoFeatureFlagWebProviderOptions } from './model' ;
3
- import { copy } from 'copy-anything' ;
4
- import { CollectorError } from './errors/collector-error' ;
5
- import { GoffApiController } from './controller/goff-api' ;
1
+ import { EvaluationDetails , FlagValue , Hook , HookContext } from '@openfeature/web-sdk' ;
2
+ import { CollectorManager } from './collector-manager' ;
6
3
7
4
const defaultTargetingKey = 'undefined-targetingKey' ;
8
5
type Timer = ReturnType < typeof setInterval > ;
9
6
10
7
export class GoFeatureFlagDataCollectorHook implements Hook {
11
- // bgSchedulerId contains the id of the setInterval that is running.
12
- private bgScheduler ?: Timer ;
13
- // dataCollectorBuffer contains all the FeatureEvents that we need to send to the relay-proxy for data collection.
14
- private dataCollectorBuffer ?: FeatureEvent < any > [ ] ;
15
- // dataFlushInterval interval time (in millisecond) we use to call the relay proxy to collect data.
16
- private readonly dataFlushInterval : number ;
17
- // dataCollectorMetadata are the metadata used when calling the data collector endpoint
18
- private readonly dataCollectorMetadata : Record < string , ExporterMetadataValue > ;
19
- private readonly goffApiController : GoffApiController ;
20
- // logger is the Open Feature logger to use
21
- private logger ?: Logger ;
8
+ private collectorManagger ?: CollectorManager ;
22
9
23
- constructor ( options : GoFeatureFlagWebProviderOptions , logger ?: Logger ) {
24
- this . dataFlushInterval = options . dataFlushInterval || 1000 * 60 ;
25
- this . logger = logger ;
26
- this . goffApiController = new GoffApiController ( options ) ;
27
- this . dataCollectorMetadata = {
28
- provider : 'web' ,
29
- openfeature : true ,
30
- ...options . exporterMetadata ,
31
- } ;
32
- }
33
-
34
- init ( ) {
35
- this . bgScheduler = setInterval ( async ( ) => await this . callGoffDataCollection ( ) , this . dataFlushInterval ) ;
36
- this . dataCollectorBuffer = [ ] ;
37
- }
38
-
39
- async close ( ) {
40
- clearInterval ( this . bgScheduler ) ;
41
- // We call the data collector with what is still in the buffer.
42
- await this . callGoffDataCollection ( ) ;
43
- }
44
-
45
- /**
46
- * callGoffDataCollection is a function called periodically to send the usage of the flag to the
47
- * central service in charge of collecting the data.
48
- */
49
- async callGoffDataCollection ( ) {
50
- const dataToSend = copy ( this . dataCollectorBuffer ) || [ ] ;
51
- this . dataCollectorBuffer = [ ] ;
52
- try {
53
- await this . goffApiController . collectData ( dataToSend , this . dataCollectorMetadata ) ;
54
- } catch ( e ) {
55
- if ( ! ( e instanceof CollectorError ) ) {
56
- throw e ;
57
- }
58
- this . logger ?. error ( e ) ;
59
- // if we have an issue calling the collector, we put the data back in the buffer
60
- this . dataCollectorBuffer = [ ...this . dataCollectorBuffer , ...dataToSend ] ;
61
- return ;
62
- }
10
+ constructor ( collectorManager : CollectorManager ) {
11
+ this . collectorManagger = collectorManager ;
63
12
}
64
13
65
14
after ( hookContext : HookContext , evaluationDetails : EvaluationDetails < FlagValue > ) {
@@ -74,7 +23,7 @@ export class GoFeatureFlagDataCollectorHook implements Hook {
74
23
userKey : hookContext . context . targetingKey || defaultTargetingKey ,
75
24
source : 'PROVIDER_CACHE' ,
76
25
} ;
77
- this . dataCollectorBuffer ?. push ( event ) ;
26
+ this . collectorManagger ?. add ( event ) ;
78
27
}
79
28
80
29
error ( hookContext : HookContext ) {
@@ -89,6 +38,6 @@ export class GoFeatureFlagDataCollectorHook implements Hook {
89
38
userKey : hookContext . context . targetingKey || defaultTargetingKey ,
90
39
source : 'PROVIDER_CACHE' ,
91
40
} ;
92
- this . dataCollectorBuffer ?. push ( event ) ;
41
+ this . collectorManagger ?. add ( event ) ;
93
42
}
94
43
}
0 commit comments