1- use std:: { any:: Any , rc :: Rc , sync:: Arc , time:: Duration } ;
1+ use std:: { any:: Any , sync:: Arc , time:: Duration } ;
22
33use magnus:: {
44 class, function,
55 gc:: register_mark_object,
66 method,
77 prelude:: * ,
88 r_hash:: ForEach ,
9- value:: { BoxValue , IntoId , Lazy , Qfalse , Qtrue } ,
9+ value:: { IntoId , Lazy , Qfalse , Qtrue } ,
1010 DataTypeFunctions , Error , Float , Integer , RClass , RHash , RModule , RString , Ruby , StaticSymbol ,
1111 Symbol , TryConvert , TypedData , Value ,
1212} ;
1313use temporal_sdk_core_api:: telemetry:: metrics:: {
1414 self , BufferInstrumentRef , CustomMetricAttributes , MetricEvent ,
1515} ;
1616
17- use crate :: { error, id, runtime:: Runtime , ROOT_MOD } ;
17+ use crate :: { error, id, runtime:: Runtime , util :: SendSyncBoxValue , ROOT_MOD } ;
1818
1919pub fn init ( ruby : & Ruby ) -> Result < ( ) , Error > {
2020 let root_mod = ruby. get_inner ( & ROOT_MOD ) ;
@@ -275,26 +275,16 @@ fn metric_key_value(k: Value, v: Value) -> Result<metrics::MetricKeyValue, Error
275275
276276#[ derive( Clone , Debug ) ]
277277pub struct BufferedMetricRef {
278- value : Rc < BoxValue < Value > > ,
278+ value : Arc < SendSyncBoxValue < Value > > ,
279279}
280280
281281impl BufferInstrumentRef for BufferedMetricRef { }
282282
283- // We can't use Ruby Opaque because it doesn't protect the object from being
284- // GC'd, but we trust ourselves not to access this value outside of Ruby
285- // context (which has global GVL to ensure thread safety).
286- unsafe impl Send for BufferedMetricRef { }
287- unsafe impl Sync for BufferedMetricRef { }
288-
289283#[ derive( Debug ) ]
290284struct BufferedMetricAttributes {
291- value : BoxValue < RHash > ,
285+ value : SendSyncBoxValue < RHash > ,
292286}
293287
294- // See Send/Sync for BufferedMetricRef for details on why we do this
295- unsafe impl Send for BufferedMetricAttributes { }
296- unsafe impl Sync for BufferedMetricAttributes { }
297-
298288impl CustomMetricAttributes for BufferedMetricAttributes {
299289 fn as_any ( self : Arc < Self > ) -> Arc < dyn Any + Send + Sync > {
300290 self as Arc < dyn Any + Send + Sync >
@@ -402,7 +392,7 @@ fn convert_metric_event(
402392 // Put on lazy ref
403393 populate_into
404394 . set ( Arc :: new ( BufferedMetricRef {
405- value : Rc :: new ( BoxValue :: new ( val) ) ,
395+ value : Arc :: new ( SendSyncBoxValue :: new ( val) ) ,
406396 } ) )
407397 . map_err ( |_| error ! ( "Failed setting metric ref" ) ) ?;
408398 Ok ( None )
@@ -423,8 +413,10 @@ fn convert_metric_event(
423413 . downcast :: < BufferedMetricAttributes > ( )
424414 . map_err ( |_| {
425415 error ! ( "Unable to downcast to expected buffered metric attributes" )
426- } ) ?;
427- attrs. value . as_ref ( ) . funcall ( "dup" , ( ) ) ?
416+ } ) ?
417+ . value
418+ . value ( ruby) ;
419+ attrs. funcall ( "dup" , ( ) ) ?
428420 }
429421 None => ruby. hash_new_capa ( attributes. len ( ) ) ,
430422 } ;
@@ -441,7 +433,7 @@ fn convert_metric_event(
441433 // Put on lazy ref
442434 populate_into
443435 . set ( Arc :: new ( BufferedMetricAttributes {
444- value : BoxValue :: new ( hash) ,
436+ value : SendSyncBoxValue :: new ( hash) ,
445437 } ) )
446438 . map_err ( |_| error ! ( "Failed setting metric attrs" ) ) ?;
447439 Ok ( None )
@@ -458,7 +450,7 @@ fn convert_metric_event(
458450 "new" ,
459451 (
460452 // Metric
461- * * instrument. get ( ) . clone ( ) . value . clone ( ) ,
453+ instrument. get ( ) . clone ( ) . value . clone ( ) . value ( ruby ) ,
462454 // Value
463455 match update {
464456 metrics:: MetricUpdateVal :: Duration ( v) if durations_as_seconds => {
@@ -482,15 +474,16 @@ fn convert_metric_event(
482474 metrics:: MetricUpdateVal :: ValueF64 ( v) => ruby. into_value ( v) ,
483475 } ,
484476 // Attributes
485- * attributes
477+ attributes
486478 . get ( )
487479 . clone ( )
488480 . as_any ( )
489481 . downcast :: < BufferedMetricAttributes > ( )
490482 . map_err ( |_| {
491483 error ! ( "Unable to downcast to expected buffered metric attributes" )
492484 } ) ?
493- . value ,
485+ . value
486+ . value ( ruby) ,
494487 ) ,
495488 ) ?,
496489 ) )
0 commit comments