@@ -6,9 +6,14 @@ use opentelemetry::{
66 KeyValue ,
77} ;
88use opentelemetry_appender_tracing:: layer:: OpenTelemetryTracingBridge ;
9- use opentelemetry_otlp:: Protocol ;
10- use opentelemetry_otlp:: { HttpExporterBuilder , WithExportConfig } ;
11- use opentelemetry_sdk:: trace:: { self as sdktrace, Config } ;
9+ use opentelemetry_otlp:: WithExportConfig ;
10+ use opentelemetry_otlp:: { LogExporter , MetricsExporter , Protocol , SpanExporter } ;
11+ use opentelemetry_sdk:: {
12+ logs:: LoggerProvider ,
13+ metrics:: { PeriodicReader , SdkMeterProvider } ,
14+ runtime,
15+ trace:: { self as sdktrace, Config , TracerProvider } ,
16+ } ;
1217use opentelemetry_sdk:: {
1318 logs:: { self as sdklogs} ,
1419 Resource ,
@@ -18,6 +23,9 @@ use tracing::info;
1823use tracing_subscriber:: prelude:: * ;
1924use tracing_subscriber:: EnvFilter ;
2025
26+ #[ cfg( feature = "hyper" ) ]
27+ use opentelemetry_otlp:: WithHttpConfig ;
28+
2129#[ cfg( feature = "hyper" ) ]
2230mod hyper;
2331
@@ -28,47 +36,46 @@ static RESOURCE: Lazy<Resource> = Lazy::new(|| {
2836 ) ] )
2937} ) ;
3038
31- fn http_exporter ( ) -> HttpExporterBuilder {
32- let exporter = opentelemetry_otlp:: new_exporter ( ) . http ( ) ;
39+ fn init_logs ( ) -> Result < sdklogs:: LoggerProvider , opentelemetry:: logs:: LogError > {
40+ let exporter_builder = LogExporter :: builder ( )
41+ . with_http ( )
42+ . with_endpoint ( "http://localhost:4318/v1/logs" )
43+ . with_protocol ( Protocol :: HttpBinary ) ;
44+
3345 #[ cfg( feature = "hyper" ) ]
34- let exporter = exporter. with_http_client ( hyper:: HyperClient :: default ( ) ) ;
35- exporter
36- }
46+ let exporter_builder = exporter_builder. with_http_client ( hyper:: HyperClient :: default ( ) ) ;
3747
38- fn init_logs ( ) -> Result < sdklogs:: LoggerProvider , opentelemetry:: logs:: LogError > {
39- opentelemetry_otlp:: new_pipeline ( )
40- . logging ( )
48+ let exporter = exporter_builder. build ( ) ?;
49+
50+ Ok ( LoggerProvider :: builder ( )
51+ . with_batch_exporter ( exporter, runtime:: Tokio )
4152 . with_resource ( RESOURCE . clone ( ) )
42- . with_exporter (
43- http_exporter ( )
44- . with_protocol ( Protocol :: HttpBinary ) //can be changed to `Protocol::HttpJson` to export in JSON format
45- . with_endpoint ( "http://localhost:4318/v1/logs" ) ,
46- )
47- . install_batch ( opentelemetry_sdk:: runtime:: Tokio )
53+ . build ( ) )
4854}
4955
5056fn init_tracer_provider ( ) -> Result < sdktrace:: TracerProvider , TraceError > {
51- opentelemetry_otlp :: new_pipeline ( )
52- . tracing ( )
53- . with_exporter (
54- http_exporter ( )
55- . with_protocol ( Protocol :: HttpBinary ) //can be changed to `Protocol::HttpJson` to export in JSON format
56- . with_endpoint ( "http://localhost:4318/v1/traces" ) ,
57- )
58- . with_trace_config ( Config :: default ( ) . with_resource ( RESOURCE . clone ( ) ) )
59- . install_batch ( opentelemetry_sdk :: runtime :: Tokio )
57+ let exporter = SpanExporter :: builder ( )
58+ . with_http ( )
59+ . with_protocol ( Protocol :: HttpBinary ) //can be changed to `Protocol::HttpJson` to export in JSON format
60+ . with_endpoint ( "http://localhost:4318/v1/traces" )
61+ . build ( ) ? ;
62+ Ok ( TracerProvider :: builder ( )
63+ . with_batch_exporter ( exporter , runtime :: Tokio )
64+ . with_config ( Config :: default ( ) . with_resource ( RESOURCE . clone ( ) ) )
65+ . build ( ) )
6066}
6167
6268fn init_metrics ( ) -> Result < opentelemetry_sdk:: metrics:: SdkMeterProvider , MetricsError > {
63- opentelemetry_otlp:: new_pipeline ( )
64- . metrics ( opentelemetry_sdk:: runtime:: Tokio )
65- . with_exporter (
66- http_exporter ( )
67- . with_protocol ( Protocol :: HttpBinary ) //can be changed to `Protocol::HttpJson` to export in JSON format
68- . with_endpoint ( "http://localhost:4318/v1/metrics" ) ,
69- )
69+ let exporter = MetricsExporter :: builder ( )
70+ . with_http ( )
71+ . with_protocol ( Protocol :: HttpBinary ) //can be changed to `Protocol::HttpJson` to export in JSON format
72+ . with_endpoint ( "http://localhost:4318/v1/metrics" )
73+ . build ( ) ?;
74+
75+ Ok ( SdkMeterProvider :: builder ( )
76+ . with_reader ( PeriodicReader :: builder ( exporter, runtime:: Tokio ) . build ( ) )
7077 . with_resource ( RESOURCE . clone ( ) )
71- . build ( )
78+ . build ( ) )
7279}
7380
7481#[ tokio:: main]
@@ -136,7 +143,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
136143 . u64_counter ( "test_counter" )
137144 . with_description ( "a simple counter for demo purposes." )
138145 . with_unit ( "my_unit" )
139- . init ( ) ;
146+ . build ( ) ;
140147 for _ in 0 ..10 {
141148 counter. add ( 1 , & [ KeyValue :: new ( "test_key" , "test_value" ) ] ) ;
142149 }
0 commit comments