1+ use std:: time:: Duration ;
2+
13use opentelemetry:: { global, propagation:: TextMapCompositePropagator , trace:: TracerProvider } ;
24use opentelemetry_datadog:: { ApiVersion , DatadogPipelineBuilder , DatadogPropagator } ;
3- use opentelemetry_otlp:: WithExportConfig ;
5+ use opentelemetry_otlp:: { WithExportConfig , WithHttpConfig } ;
46use opentelemetry_sdk:: {
57 Resource ,
68 propagation:: TraceContextPropagator ,
@@ -131,6 +133,7 @@ fn get_tracer_datadog(
131133 service_version : String ,
132134 service_env : String ,
133135) -> anyhow:: Result < SdkTracerProvider > {
136+ let http_client = get_http_client ( ) ?;
134137 let mut config = trace:: Config :: default ( ) ;
135138 config. sampler = Box :: new ( get_sampler ( ) ) ;
136139 config. id_generator = Box :: new ( RandomIdGenerator :: default ( ) ) ;
@@ -142,6 +145,7 @@ fn get_tracer_datadog(
142145 . with_api_version ( ApiVersion :: Version05 )
143146 . with_agent_endpoint ( datadog_endpoint)
144147 . with_trace_config ( config)
148+ . with_http_client ( http_client)
145149 . install_batch ( )
146150 . map_err ( |e| e. into ( ) )
147151}
@@ -154,8 +158,10 @@ fn get_tracer_otlp(
154158 service_version : String ,
155159 service_env : String ,
156160) -> anyhow:: Result < SdkTracerProvider > {
161+ let http_client = get_http_client ( ) ?;
157162 let otlp_exporter = opentelemetry_otlp:: SpanExporter :: builder ( )
158163 . with_http ( )
164+ . with_http_client ( http_client)
159165 . with_protocol ( opentelemetry_otlp:: Protocol :: HttpBinary )
160166 . with_endpoint ( otlp_endpoint)
161167 . build ( ) ?;
@@ -198,3 +204,15 @@ fn get_sampler() -> Sampler {
198204 // (2) if no parent, then the trace id ratio
199205 Sampler :: ParentBased ( Box :: new ( Sampler :: TraceIdRatioBased ( ratio_to_sample) ) )
200206}
207+
208+ /// Get the HTTP client to be used
209+ /// This is used to send traces to the tracing endpoint
210+ ///
211+ /// The exporter runs on a different thread than the main thread, so we need to use a blocking client.
212+ fn get_http_client ( ) -> anyhow:: Result < reqwest:: blocking:: Client > {
213+ reqwest:: blocking:: Client :: builder ( )
214+ . timeout ( Duration :: from_secs ( 10 ) )
215+ . connect_timeout ( Duration :: from_secs ( 5 ) )
216+ . build ( )
217+ . map_err ( |e| anyhow:: anyhow!( "Failed to create HTTP client for telemetry: {}" , e) )
218+ }
0 commit comments