1- // Based off of https://github.com/tokio-rs/tracing-opentelemetry/blob/v0.1.x/examples/opentelemetry-otlp.rs
2- // Based off of https://github.com/tokio-rs/tracing-opentelemetry/blob/v0.1.x/examples/opentelemetry-otlp.rs
3-
41use opentelemetry:: KeyValue ;
52use opentelemetry:: trace:: { SamplingResult , SpanKind } ;
63use opentelemetry_otlp:: WithExportConfig ;
@@ -11,7 +8,7 @@ use opentelemetry_sdk::{
118use opentelemetry_semantic_conventions:: { SCHEMA_URL , attribute:: SERVICE_VERSION } ;
129use std:: sync:: { Arc , OnceLock , RwLock } ;
1310
14- /// Dynamic sampler that can be updated at runtime
11+ /// Dynamic sampler that can be updated at runtime.
1512#[ derive( Clone , Debug ) ]
1613struct DynamicSampler {
1714 ratio : Arc < RwLock < f64 > > ,
@@ -43,7 +40,6 @@ impl opentelemetry_sdk::trace::ShouldSample for DynamicSampler {
4340 ) -> SamplingResult {
4441 let ratio = self . ratio . read ( ) . ok ( ) . map ( |r| * r) . unwrap_or ( 0.001 ) ;
4542
46- // Use TraceIdRatioBased sampling logic
4743 let sampler = Sampler :: TraceIdRatioBased ( ratio) ;
4844 sampler. should_sample (
4945 parent_context,
@@ -58,7 +54,7 @@ impl opentelemetry_sdk::trace::ShouldSample for DynamicSampler {
5854
5955static SAMPLER : OnceLock < DynamicSampler > = OnceLock :: new ( ) ;
6056
61- /// Update the sampler ratio at runtime
57+ /// Update the sampler ratio at runtime.
6258pub fn set_sampler_ratio ( ratio : f64 ) -> anyhow:: Result < ( ) > {
6359 let sampler = SAMPLER
6460 . get ( )
@@ -94,21 +90,17 @@ fn init_tracer_provider() -> SdkTracerProvider {
9490 . build ( )
9591 . unwrap ( ) ;
9692
97- // Create dynamic sampler with initial ratio from env
9893 let initial_ratio = std:: env:: var ( "RIVET_OTEL_SAMPLER_RATIO" )
9994 . ok ( )
10095 . and_then ( |s| s. parse :: < f64 > ( ) . ok ( ) )
10196 . unwrap_or ( 0.001 ) ;
10297
10398 let dynamic_sampler = DynamicSampler :: new ( initial_ratio) ;
10499
105- // Store sampler globally for later updates
106100 let _ = SAMPLER . set ( dynamic_sampler. clone ( ) ) ;
107101
108102 SdkTracerProvider :: builder ( )
109- // Customize sampling strategy with parent-based sampling using our dynamic sampler
110103 . with_sampler ( Sampler :: ParentBased ( Box :: new ( dynamic_sampler) ) )
111- // If export trace to AWS X-Ray, you can use XrayIdGenerator
112104 . with_id_generator ( RandomIdGenerator :: default ( ) )
113105 . with_resource ( resource ( ) )
114106 . with_batch_exporter ( exporter)
@@ -117,7 +109,6 @@ fn init_tracer_provider() -> SdkTracerProvider {
117109
118110/// Initialize OtelProviderGuard for opentelemetry-related termination processing.
119111pub fn init_otel_providers ( ) -> Option < OtelProviderGuard > {
120- // Check if otel is enabled
121112 let enable_otel = std:: env:: var ( "RIVET_OTEL_ENABLED" ) . map_or ( false , |x| x == "1" ) ;
122113
123114 if enable_otel {
@@ -137,7 +128,7 @@ pub struct OtelProviderGuard {
137128impl Drop for OtelProviderGuard {
138129 fn drop ( & mut self ) {
139130 if let Err ( err) = self . tracer_provider . shutdown ( ) {
140- eprintln ! ( "{err:?} ") ;
131+ tracing :: error! ( ?err , "failed to shut down otel tracer provider ") ;
141132 }
142133 }
143134}
0 commit comments