-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.rs
63 lines (52 loc) · 1.8 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::net::SocketAddr;
use std::time::Duration;
use datadog_tracing::axum::{OtelAxumLayer, OtelInResponseLayer};
use axum::{routing::get, Router};
use tower_http::timeout::TimeoutLayer;
use tokio::net::TcpListener;
use tracing::info;
use datadog_tracing::axum::shutdown_signal;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (_guard, tracer_shutdown) = datadog_tracing::init()?;
let app = Router::new()
.route("/", get(root))
// include trace context as header into the response
.layer(OtelInResponseLayer)
//start OpenTelemetry trace on incoming request
.layer((
OtelAxumLayer::default(),
// Graceful shutdown will wait for outstanding requests to complete. Add a timeout so
// requests don't hang forever.
TimeoutLayer::new(Duration::from_secs(90)),
))
.route("/health", get(health));
let addr = SocketAddr::from(([0, 0, 0, 0], 3025));
let listener = TcpListener::bind(addr).await?;
info!("listening on {}", addr);
axum::serve(listener, app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await?;
tracer_shutdown.shutdown();
Ok(())
}
async fn root() -> &'static str {
do_something().await;
"Hello, World!"
}
#[tracing::instrument]
async fn do_something() {
tokio::time::sleep(Duration::from_millis(120)).await;
do_something_else().await;
tracing::info!("in the middle of doing something");
tokio::time::sleep(Duration::from_millis(10)).await;
do_something_else().await;
tokio::time::sleep(Duration::from_millis(20)).await;
}
#[tracing::instrument]
async fn do_something_else() {
tokio::time::sleep(Duration::from_millis(40)).await;
}
async fn health() -> &'static str {
"healthy"
}