1
+ use opentelemetry:: sdk:: Resource ;
1
2
#[ cfg( any( feature = "jaeger" , feature = "otlp" ) ) ]
2
3
use opentelemetry:: {
3
- global, sdk:: propagation:: TraceContextPropagator , sdk:: trace as sdktrace, sdk:: Resource ,
4
- trace:: TraceError ,
4
+ global, sdk:: propagation:: TraceContextPropagator , sdk:: trace as sdktrace, trace:: TraceError ,
5
5
} ;
6
6
7
7
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
@@ -14,14 +14,10 @@ pub enum CollectorKind {
14
14
}
15
15
16
16
#[ cfg( any( feature = "jaeger" , feature = "otlp" ) ) ]
17
- pub fn init_tracer ( kind : CollectorKind ) -> Result < sdktrace:: Tracer , TraceError > {
18
- // use opentelemetry_otlp::WithExportConfig;
19
- use opentelemetry_semantic_conventions as semcov;
20
- let resource = Resource :: new ( vec ! [
21
- semcov:: resource:: SERVICE_NAME . string( env!( "CARGO_PKG_NAME" ) ) ,
22
- semcov:: resource:: SERVICE_VERSION . string( env!( "CARGO_PKG_VERSION" ) ) ,
23
- ] ) ;
24
-
17
+ pub fn init_tracer (
18
+ kind : CollectorKind ,
19
+ resource : Resource ,
20
+ ) -> Result < sdktrace:: Tracer , TraceError > {
25
21
match kind {
26
22
#[ cfg( feature = "otlp" ) ]
27
23
CollectorKind :: Otlp => {
@@ -41,18 +37,39 @@ pub fn init_tracer(kind: CollectorKind) -> Result<sdktrace::Tracer, TraceError>
41
37
}
42
38
}
43
39
40
+ /// call with service name and version
41
+ ///
42
+ /// ```rust
43
+ /// make_resource(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
44
+ /// ```
45
+ pub fn make_resource < S > ( service_name : S , service_version : S ) -> Resource
46
+ where
47
+ S : Into < String > ,
48
+ {
49
+ use opentelemetry_semantic_conventions as semcov;
50
+ Resource :: new ( vec ! [
51
+ semcov:: resource:: SERVICE_NAME . string( service_name. into( ) ) ,
52
+ semcov:: resource:: SERVICE_VERSION . string( service_version. into( ) ) ,
53
+ ] )
54
+ }
55
+
44
56
#[ cfg( feature = "otlp" ) ]
45
57
pub fn init_tracer_otlp ( resource : Resource ) -> Result < sdktrace:: Tracer , TraceError > {
46
- global:: set_text_map_propagator ( TraceContextPropagator :: new ( ) ) ;
47
-
48
- // resource = resource.merge(&read_dt_metadata());
58
+ use opentelemetry_otlp:: WithExportConfig ;
49
59
60
+ global:: set_text_map_propagator ( TraceContextPropagator :: new ( ) ) ;
61
+ // FIXME choice the right/official env variable `OTEL_COLLECTOR_URL` or `OTEL_EXPORTER_OTLP_ENDPOINT`
62
+ // TODO try to autodetect if http or grpc should be used (eg based on env variable, port ???)
63
+ //endpoint (default = 0.0.0.0:4317 for grpc protocol, 0.0.0.0:4318 http protocol):
64
+ //.http().with_endpoint(collector_url),
65
+ let endpoint_grpc = std:: env:: var ( "OTEL_EXPORTER_OTLP_ENDPOINT" )
66
+ . unwrap_or_else ( |_| "http://0.0.0.0:4317" . to_string ( ) ) ;
67
+ let exporter = opentelemetry_otlp:: new_exporter ( )
68
+ . tonic ( )
69
+ . with_endpoint ( endpoint_grpc) ;
50
70
opentelemetry_otlp:: new_pipeline ( )
51
71
. tracing ( )
52
- //endpoint (default = 0.0.0.0:4317 for grpc protocol, 0.0.0.0:4318 http protocol):
53
- . with_exporter (
54
- opentelemetry_otlp:: new_exporter ( ) . tonic ( ) , //.http().with_endpoint(collector_url),
55
- )
72
+ . with_exporter ( exporter)
56
73
. with_trace_config (
57
74
sdktrace:: config ( )
58
75
. with_resource ( resource)
@@ -69,7 +86,6 @@ pub fn init_tracer_jaeger(resource: Resource) -> Result<sdktrace::Tracer, TraceE
69
86
) ;
70
87
71
88
opentelemetry_jaeger:: new_pipeline ( )
72
- . with_service_name ( env ! ( "CARGO_PKG_NAME" ) )
73
89
. with_trace_config (
74
90
sdktrace:: config ( )
75
91
. with_resource ( resource)
0 commit comments