@@ -12,11 +12,27 @@ use structopt::StructOpt;
12
12
use tokio:: net:: TcpListener ;
13
13
use tokio:: signal;
14
14
15
+ #[ cfg( feature = "otel" ) ]
16
+ // To be able to set the XrayPropagator
17
+ use opentelemetry:: global;
18
+ #[ cfg( feature = "otel" ) ]
19
+ // To configure certain options such as sampling rate
20
+ use opentelemetry:: sdk:: trace as sdktrace;
21
+ #[ cfg( feature = "otel" ) ]
22
+ // For passing along the same XrayId across services
23
+ use opentelemetry_aws:: trace:: XrayPropagator ;
24
+ #[ cfg( feature = "otel" ) ]
25
+ // To be able to pass along the XrayId across services
26
+ #[ cfg( feature = "otel" ) ]
27
+ // The `Ext` traits are to allow the Registry to accept the
28
+ // OpenTelemetry-specific types (such as `OpenTelemetryLayer`)
29
+ use tracing_subscriber:: {
30
+ fmt, layer:: SubscriberExt , util:: SubscriberInitExt , util:: TryInitError , EnvFilter ,
31
+ } ;
32
+
15
33
#[ tokio:: main]
16
34
pub async fn main ( ) -> mini_redis:: Result < ( ) > {
17
- // enable logging
18
- // see https://docs.rs/tracing for more info
19
- tracing_subscriber:: fmt:: try_init ( ) ?;
35
+ set_up_logging ( ) ?;
20
36
21
37
let cli = Cli :: from_args ( ) ;
22
38
let port = cli. port . as_deref ( ) . unwrap_or ( DEFAULT_PORT ) ;
@@ -35,3 +51,46 @@ struct Cli {
35
51
#[ structopt( name = "port" , long = "--port" ) ]
36
52
port : Option < String > ,
37
53
}
54
+
55
+ #[ cfg( not( feature = "otel" ) ) ]
56
+ fn set_up_logging ( ) -> mini_redis:: Result < ( ) > {
57
+ // See https://docs.rs/tracing for more info
58
+ tracing_subscriber:: fmt:: try_init ( )
59
+ }
60
+
61
+ #[ cfg( feature = "otel" ) ]
62
+ fn set_up_logging ( ) -> Result < ( ) , TryInitError > {
63
+ // Set the global propagator to X-Ray propagator
64
+ // Note: If you need to pass the x-amzn-trace-id across services in the same trace,
65
+ // you will need this line. However, this requires additional code not pictured here.
66
+ // For a full example using hyper, see:
67
+ // https://github.com/open-telemetry/opentelemetry-rust/blob/main/examples/aws-xray/src/server.rs#L14-L26
68
+ global:: set_text_map_propagator ( XrayPropagator :: default ( ) ) ;
69
+
70
+ let tracer = opentelemetry_otlp:: new_pipeline ( )
71
+ . tracing ( )
72
+ . with_exporter ( opentelemetry_otlp:: new_exporter ( ) . tonic ( ) )
73
+ . with_trace_config (
74
+ sdktrace:: config ( )
75
+ . with_sampler ( sdktrace:: Sampler :: AlwaysOn )
76
+ // Needed in order to convert the trace IDs into an Xray-compatible format
77
+ . with_id_generator ( sdktrace:: XrayIdGenerator :: default ( ) ) ,
78
+ )
79
+ . install_simple ( )
80
+ . expect ( "Unable to initialize OtlpPipeline" ) ;
81
+
82
+ // Create a tracing layer with the configured tracer
83
+ let opentelemetry = tracing_opentelemetry:: layer ( ) . with_tracer ( tracer) ;
84
+
85
+ // Parse an `EnvFilter` configuration from the `RUST_LOG`
86
+ // environment variable.
87
+ let filter = EnvFilter :: from_default_env ( ) ;
88
+
89
+ // Use the tracing subscriber `Registry`, or any other subscriber
90
+ // that impls `LookupSpan`
91
+ tracing_subscriber:: registry ( )
92
+ . with ( opentelemetry)
93
+ . with ( filter)
94
+ . with ( fmt:: Layer :: default ( ) )
95
+ . try_init ( )
96
+ }
0 commit comments