-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
65 lines (56 loc) · 1.87 KB
/
main.rs
File metadata and controls
65 lines (56 loc) · 1.87 KB
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
64
65
use std::net::SocketAddr;
use std::time::Duration;
use axum::{routing::get, Router};
use ddtrace::axum::OtelAxumLayer;
use ddtrace::error::Error;
use ddtrace::formatter::DatadogFormatter;
use ddtrace::set_global_propagator;
use ddtrace::tracer::build_layer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
#[tokio::main]
async fn main() -> Result<(), Error> {
let service_name = std::env::var("DD_SERVICE").unwrap_or("my-service".to_string());
let (tracing_layer, _guard) = build_layer(service_name)?;
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into()),
))
.with(
tracing_subscriber::fmt::layer()
.json()
.event_format(DatadogFormatter),
)
.with(tracing_layer)
.init();
set_global_propagator();
let app = Router::new()
.route("/", get(root))
.layer(OtelAxumLayer::default())
.route("/health", get(health));
let addr = SocketAddr::from(([0, 0, 0, 0], 3025));
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
tracing::info!("listening on {}", addr);
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"
}