@@ -7,20 +7,24 @@ import {
77	IdentifySeriesResult , 
88}  from  './types/Hooks' 
99import  {  trace  }  from  '@opentelemetry/api' 
10- import  type   {   HighlightPublicInterface   }  from  '../../client/types/types ' 
10+ import  {   type   HighlightPublicInterface ,   MetricCategory   }  from  '../../client' 
1111import  type  {  ErrorMessage ,  Source  }  from  '../../client/types/shared-types' 
12- import  {  IntegrationClient  }  from  '../index' 
13- import  {  LDClientMin  }  from  './types/LDClient' 
12+ import  type  {  IntegrationClient  }  from  '../index' 
13+ import  type  {  LDClientMin  }  from  './types/LDClient' 
14+ import  type  {  RecordMetric  }  from  '../../client/types/types' 
1415
1516const  FEATURE_FLAG_SCOPE  =  'feature_flag' 
17+ // TODO(vkorolik) reporting environment as `${FEATURE_FLAG_SCOPE}.set.id` 
1618const  FEATURE_FLAG_KEY_ATTR  =  `${ FEATURE_FLAG_SCOPE }  .key` 
17- const  FEATURE_FLAG_PROVIDER_ATTR  =  `${ FEATURE_FLAG_SCOPE }  .provider_name ` 
19+ const  FEATURE_FLAG_PROVIDER_ATTR  =  `${ FEATURE_FLAG_SCOPE }  .provider.name ` 
1820const  FEATURE_FLAG_CONTEXT_KEY_ATTR  =  `${ FEATURE_FLAG_SCOPE }  .context.key` 
19- const  FEATURE_FLAG_VARIANT_ATTR  =  `${ FEATURE_FLAG_SCOPE }  .variant` 
21+ const  FEATURE_FLAG_VARIANT_ATTR  =  `${ FEATURE_FLAG_SCOPE }  .result. variant` 
2022const  FEATURE_FLAG_SPAN_NAME  =  'evaluation' 
2123
24+ const  LD_INITIALIZE_EVENT  =  '$ld:telemetry:initialize' 
2225const  LD_ERROR_EVENT  =  '$ld:telemetry:error' 
2326const  LD_TRACK_EVENT  =  '$ld:telemetry:track' 
27+ const  LD_METRIC_EVENT  =  '$ld:telemetry:metric' 
2428
2529function  encodeKey ( key : string ) : string  { 
2630	if  ( key . includes ( '%' )  ||  key . includes ( ':' ) )  { 
@@ -64,7 +68,7 @@ export function setupLaunchDarklyIntegration(
6468		)  =>  { 
6569			hClient . identify ( 
6670				getCanonicalKey ( hookContext . context ) , 
67- 				undefined , 
71+ 				hookContext . context , 
6872				'LaunchDarkly' , 
6973			) 
7074			return  data 
@@ -107,6 +111,31 @@ export class LaunchDarklyIntegration implements IntegrationClient {
107111		this . client  =  client 
108112	} 
109113
114+ 	init ( sessionSecureID : string )  { 
115+ 		this . client . track ( LD_INITIALIZE_EVENT ,  { 
116+ 			sessionSecureID, 
117+ 		} ) 
118+ 	} 
119+ 
120+ 	recordMetric ( sessionSecureID : string ,  metric : RecordMetric )  { 
121+ 		// only record web vitals 
122+ 		if  ( metric . category  !==  MetricCategory . WebVital )  { 
123+ 			return 
124+ 		} 
125+ 		// ignore Jank metric, sent on interaction 
126+ 		if  ( metric . name  ===  'Jank' )  { 
127+ 			return 
128+ 		} 
129+ 		this . client . track ( 
130+ 			`${ LD_METRIC_EVENT }  :${ metric . name . toLowerCase ( ) }  ` , 
131+ 			{ 
132+ 				...metric , 
133+ 				sessionSecureID, 
134+ 			} , 
135+ 			metric . value , 
136+ 		) 
137+ 	} 
138+ 
110139	identify ( 
111140		_sessionSecureID : string , 
112141		_user_identifier : string , 
@@ -124,9 +153,17 @@ export class LaunchDarklyIntegration implements IntegrationClient {
124153	} 
125154
126155	track ( sessionSecureID : string ,  metadata : object )  { 
127- 		this . client . track ( LD_TRACK_EVENT ,  { 
128- 			...metadata , 
129- 			sessionSecureID, 
130- 		} ) 
156+ 		const  event  =  ( metadata  as  unknown  as  {  event ?: string  } ) . event 
157+ 		// skip integration hClient.track() calls 
158+ 		if  ( event  ===  FEATURE_FLAG_SPAN_NAME )  { 
159+ 			return 
160+ 		} 
161+ 		this . client . track ( 
162+ 			event  ? `${ LD_TRACK_EVENT }  :${ event }  `  : LD_TRACK_EVENT , 
163+ 			{ 
164+ 				...metadata , 
165+ 				sessionSecureID, 
166+ 			} , 
167+ 		) 
131168	} 
132169} 
0 commit comments