@@ -54,21 +54,24 @@ struct ComposerPackages {
5454    packages :  Vec < data:: Dependency > , 
5555} 
5656
57- #[ derive( Clone ) ]  
57+ pub  struct  TelemetryCachedEntry  { 
58+     last_used :  Instant , 
59+     pub  client :  Arc < Mutex < TelemetryCachedClient > > , 
60+ } 
61+ 
5862pub  struct  TelemetryCachedClient  { 
59-     pub  client :  TelemetryWorkerHandle , 
60-     pub  shm_writer :  Arc < OneWayShmWriter < NamedShmHandle > > , 
61-     pub  last_used :  Instant , 
63+     pub  worker :  TelemetryWorkerHandle , 
64+     pub  shm_writer :  OneWayShmWriter < NamedShmHandle > , 
6265    pub  config_sent :  bool , 
6366    pub  buffered_integrations :  HashSet < Integration > , 
6467    pub  buffered_composer_paths :  HashSet < PathBuf > , 
6568    pub  last_endpoints_push :  SystemTime , 
66-     pub  telemetry_metrics :  Arc < Mutex < HashMap < String ,  ContextKey > > > , 
67-     pub  handle :  Arc < Mutex < Option < JoinHandle < ( ) > > > > , 
69+     pub  telemetry_metrics :  HashMap < String ,  ContextKey > , 
70+     pub  handle :  Option < JoinHandle < ( ) > > , 
6871} 
6972
7073impl  TelemetryCachedClient  { 
71-     pub   fn  new ( 
74+     fn  new ( 
7275        service :  & str , 
7376        env :  & str , 
7477        instance_id :  & InstanceId , 
@@ -91,18 +94,17 @@ impl TelemetryCachedClient {
9194
9295        info ! ( "spawning telemetry worker {config:?}" ) ; 
9396        Self  { 
94-             client :  handle. clone ( ) , 
95-             shm_writer :  Arc :: new ( 
97+             worker :  handle. clone ( ) , 
98+             shm_writer :  { 
9699                #[ allow( clippy:: unwrap_used) ]  
97-                 OneWayShmWriter :: < NamedShmHandle > :: new ( path_for_telemetry ( service,  env) ) . unwrap ( ) , 
98-             ) , 
99-             last_used :  Instant :: now ( ) , 
100+                 OneWayShmWriter :: < NamedShmHandle > :: new ( path_for_telemetry ( service,  env) ) . unwrap ( ) 
101+             } , 
100102            config_sent :  false , 
101103            buffered_integrations :  HashSet :: new ( ) , 
102104            buffered_composer_paths :  HashSet :: new ( ) , 
103105            last_endpoints_push :  SystemTime :: UNIX_EPOCH , 
104106            telemetry_metrics :  Default :: default ( ) , 
105-             handle :  Arc :: new ( Mutex :: new ( None ) ) , 
107+             handle :  None , 
106108        } 
107109    } 
108110
@@ -119,12 +121,11 @@ impl TelemetryCachedClient {
119121        } 
120122    } 
121123
122-     pub  fn  register_metric ( & self ,  metric :  MetricContext )  { 
123-         let  mut  metrics = self . telemetry_metrics . lock_or_panic ( ) ; 
124-         if  !metrics. contains_key ( & metric. name )  { 
125-             metrics. insert ( 
124+     pub  fn  register_metric ( & mut  self ,  metric :  MetricContext )  { 
125+         if  !self . telemetry_metrics . contains_key ( & metric. name )  { 
126+             self . telemetry_metrics . insert ( 
126127                metric. name . clone ( ) , 
127-                 self . client . register_metric_context ( 
128+                 self . worker . register_metric_context ( 
128129                    metric. name , 
129130                    metric. tags , 
130131                    metric. metric_type , 
@@ -140,14 +141,13 @@ impl TelemetryCachedClient {
140141        ( name,  val,  tags) :  ( String ,  f64 ,  Vec < Tag > ) , 
141142    )  -> TelemetryActions  { 
142143        #[ allow( clippy:: unwrap_used) ]  
143-         TelemetryActions :: AddPoint ( ( 
144-             val, 
145-             * self . telemetry_metrics . lock_or_panic ( ) . get ( & name) . unwrap ( ) , 
146-             tags, 
147-         ) ) 
144+         TelemetryActions :: AddPoint ( ( val,  * self . telemetry_metrics . get ( & name) . unwrap ( ) ,  tags) ) 
148145    } 
149146
150-     pub  fn  process_actions ( & self ,  sidecar_actions :  Vec < SidecarAction > )  -> Vec < TelemetryActions >  { 
147+     pub  fn  process_actions ( 
148+         & mut  self , 
149+         sidecar_actions :  Vec < SidecarAction > , 
150+     )  -> Vec < TelemetryActions >  { 
151151        let  mut  actions = vec ! [ ] ; 
152152        for  action in  sidecar_actions { 
153153            match  action { 
@@ -236,13 +236,13 @@ type EnvString = String;
236236type  TelemetryCachedClientKey  = ( ServiceString ,  EnvString ) ; 
237237
238238pub  struct  TelemetryCachedClientSet  { 
239-     pub  inner :  Arc < Mutex < HashMap < TelemetryCachedClientKey ,  TelemetryCachedClient > > > , 
239+     pub  inner :  Arc < Mutex < HashMap < TelemetryCachedClientKey ,  TelemetryCachedEntry > > > , 
240240    cleanup_handle :  Option < tokio:: task:: JoinHandle < ( ) > > , 
241241} 
242242
243243impl  Default  for  TelemetryCachedClientSet  { 
244244    fn  default ( )  -> Self  { 
245-         let  inner:  Arc < Mutex < HashMap < TelemetryCachedClientKey ,  TelemetryCachedClient > > >  =
245+         let  inner:  Arc < Mutex < HashMap < TelemetryCachedClientKey ,  TelemetryCachedEntry > > >  =
246246            Arc :: new ( Default :: default ( ) ) ; 
247247        let  clients = inner. clone ( ) ; 
248248
@@ -286,7 +286,7 @@ impl TelemetryCachedClientSet {
286286        instance_id :  & InstanceId , 
287287        runtime_meta :  & RuntimeMetadata , 
288288        get_config :  F , 
289-     )  -> TelemetryCachedClient 
289+     )  -> Arc < Mutex < TelemetryCachedClient > > 
290290    where 
291291        F :  FnOnce ( )  -> ddtelemetry:: config:: Config , 
292292    { 
@@ -296,32 +296,37 @@ impl TelemetryCachedClientSet {
296296
297297        if  let  Some ( existing)  = map. get_mut ( & key)  { 
298298            existing. last_used  = Instant :: now ( ) ; 
299-             let  client = existing. clone ( ) ; 
300299            tokio:: spawn ( { 
301-                 let  telemetry  = client. clone ( ) ; 
300+                 let  worker  = existing . client . lock_or_panic ( ) . worker . clone ( ) ; 
302301                async  move  { 
303-                     telemetry
304-                         . client 
302+                     worker
305303                        . send_msg ( TelemetryActions :: Lifecycle ( LifecycleAction :: Start ) ) 
306304                        . await 
307305                        . ok ( ) ; 
308306                } 
309307            } ) ; 
310308
311309            info ! ( "Reusing existing telemetry client for {key:?}" ) ; 
312-             return  client; 
310+             return  existing . client . clone ( ) ; 
313311        } 
314312
315-         let  client =
316-             TelemetryCachedClient :: new ( service,  env,  instance_id,  runtime_meta,  get_config) ; 
313+         let  entry = TelemetryCachedEntry  { 
314+             last_used :  Instant :: now ( ) , 
315+             client :  Arc :: new ( Mutex :: new ( TelemetryCachedClient :: new ( 
316+                 service, 
317+                 env, 
318+                 instance_id, 
319+                 runtime_meta, 
320+                 get_config, 
321+             ) ) ) , 
322+         } ; 
317323
318-         map. insert ( key. clone ( ) ,  client . clone ( ) ) ; 
324+         let  entry =  map. entry ( key. clone ( ) ) . or_insert ( entry ) ; 
319325
320326        tokio:: spawn ( { 
321-             let  telemetry  = client. clone ( ) ; 
327+             let  worker  = entry . client . lock_or_panic ( ) . worker . clone ( ) ; 
322328            async  move  { 
323-                 telemetry
324-                     . client 
329+                 worker
325330                    . send_msg ( TelemetryActions :: Lifecycle ( LifecycleAction :: Start ) ) 
326331                    . await 
327332                    . ok ( ) ; 
@@ -330,7 +335,7 @@ impl TelemetryCachedClientSet {
330335
331336        info ! ( "Created new telemetry client for {key:?}" ) ; 
332337
333-         client
338+         entry . client . clone ( ) 
334339    } 
335340
336341    pub  fn  remove_telemetry_client ( & self ,  service :  & str ,  env :  & str )  { 
@@ -388,10 +393,11 @@ pub(crate) async fn telemetry_action_receiver_task(sidecar: SidecarServer) {
388393            & actions. service_name , 
389394            & actions. env_name , 
390395        ) ; 
396+         let  client = telemetry_client. lock_or_panic ( ) . worker . clone ( ) ; 
391397
392398        for  action in  actions. actions  { 
393399            let  action_str = format ! ( "{action:?}" ) ; 
394-             match  telemetry_client . client . send_msg ( action) . await  { 
400+             match  client. send_msg ( action) . await  { 
395401                Ok ( _)  => { 
396402                    debug ! ( "Sent telemetry action to TelemetryWorker: {action_str}" ) ; 
397403                } 
@@ -409,7 +415,7 @@ fn get_telemetry_client(
409415    instance_id :  & InstanceId , 
410416    service_name :  & str , 
411417    env_name :  & str , 
412- )  -> TelemetryCachedClient  { 
418+ )  -> Arc < Mutex < TelemetryCachedClient > >  { 
413419    let  session = sidecar. get_session ( & instance_id. session_id ) ; 
414420    let  trace_config = session. get_trace_config ( ) ; 
415421    let  runtime_meta = RuntimeMetadata :: new ( 
0 commit comments