Skip to content

Commit 89882c6

Browse files
authored
Add actix-http-tracing example (open-telemetry#373)
1 parent 64bc7ba commit 89882c6

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ members = [
77
"opentelemetry-prometheus",
88
"opentelemetry-semantic-conventions",
99
"opentelemetry-zipkin",
10-
"examples/actix-udp",
1110
"examples/actix-http",
11+
"examples/actix-http-tracing",
12+
"examples/actix-udp",
1213
"examples/async",
1314
"examples/aws-xray",
1415
"examples/basic",
1516
"examples/basic-otlp",
1617
"examples/datadog",
1718
"examples/grpc",
1819
"examples/http",
20+
"examples/tracing-grpc",
1921
"examples/zipkin",
20-
"examples/tracing-grpc"
2122
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "actix-http-tracing"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
[dependencies]
8+
actix-web = "3.2"
9+
actix-web-opentelemetry = { version = "0.8", features = ["metrics"] }
10+
opentelemetry = { version = "0.10", features = ["metrics", "tokio"] }
11+
opentelemetry-jaeger = { version = "0.9", features = ["tokio"] }
12+
opentelemetry-prometheus = "0.3.0"
13+
tracing = "0.1"
14+
tracing-opentelemetry = "0.9"
15+
tracing-subscriber = "0.2"

examples/actix-http-tracing/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Actix Web with Tracing example
2+
3+
This example shows how to export data to [Jaeger] and [Prometheus] from an
4+
[actix-web] app instrumented using the [tracing] API and ecosystem.
5+
6+
[Jaeger]: https://www.jaegertracing.io
7+
[Prometheus]: https://prometheus.io
8+
[actix-web]: https://github.com/actix/actix-web
9+
[tracing]: https://github.com/tokio-rs/tracing
10+
11+
## Setup
12+
13+
```shell
14+
# Run jaeger in background
15+
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 -p14268:14268 jaegertracing/all-in-one:latest
16+
17+
# Start the actix web server
18+
$ cargo run
19+
20+
# (from another terminal window)
21+
$ curl localhost:8080/users/@ferris
22+
=> Hello @ferris
23+
24+
# View spans (see the image below)
25+
$ firefox http://localhost:16686/
26+
```
27+
28+
## Generated Telemetry
29+
30+
### Jaeger
31+
32+
After completing the steps above, the following trace information is now
33+
available:
34+
35+
![Jaeger UI](trace.png)
36+
37+
### Prometheus (Optional)
38+
39+
Optional request metrics information is now exposed via `/metrics`:
40+
41+
```shell
42+
$ curl localhost:8080/metrics
43+
44+
# HELP http_requests_duration HTTP request duration per route
45+
# TYPE http_requests_duration histogram
46+
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="0.5"} 1
47+
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="0.9"} 1
48+
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="0.99"} 1
49+
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="+Inf"} 1
50+
http_requests_duration_sum{method="GET",route="/users/{username}",status="200"} 0.001289
51+
http_requests_duration_count{method="GET",route="/users/{username}",status="200"} 1
52+
# HELP http_requests_total HTTP requests per route
53+
# TYPE http_requests_total counter
54+
http_requests_total{method="GET",route="/users/{username}",status="200"} 1
55+
```
56+
57+
### Logs
58+
59+
[tracing] has been configured to report `INFO` and above level logs to stdout
60+
via [`tracing_subscriber::fmt`] and [`tracing_subscriber::EnvFilter`] to produce
61+
the output below:
62+
63+
```shell
64+
Nov 29 13:08:04.932 INFO actix_server::builder: Starting 16 workers
65+
Nov 29 13:08:04.933 INFO actix_server::builder: Starting "actix-web-service-127.0.0.1:8080" service on 127.0.0.1:8080
66+
Nov 29 13:08:08.740 INFO greet_user{username="@ferris"}: actix_http_tracing: preparing to greet user
67+
Nov 29 13:08:08.740 INFO actix_web::middleware::logger: 127.0.0.1:63418 "GET /users/@ferris HTTP/1.1" 200 13 "-" "curl/7.64.1" 0.000758
68+
```
69+
70+
[tracing]: https://github.com/tokio-rs/tracing
71+
[`tracing_subscriber::fmt`]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/index.html
72+
[`tracing_subscriber::EnvFilter`]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use actix_web::middleware::Logger;
2+
use actix_web::{web, App, HttpServer};
3+
use actix_web_opentelemetry::RequestTracing;
4+
use opentelemetry::{global, sdk::propagation::TraceContextPropagator};
5+
use std::io;
6+
use tracing_subscriber::prelude::*;
7+
use tracing_subscriber::Registry;
8+
9+
async fn index(username: actix_web::web::Path<String>) -> String {
10+
greet_user(username.as_ref())
11+
}
12+
13+
#[tracing::instrument]
14+
fn greet_user(username: &str) -> String {
15+
tracing::info!("preparing to greet user");
16+
format!("Hello {}", username)
17+
}
18+
19+
#[actix_web::main]
20+
async fn main() -> io::Result<()> {
21+
// Start an (optional) otel prometheus metrics pipeline
22+
let metrics_exporter = opentelemetry_prometheus::exporter().init();
23+
let request_metrics = actix_web_opentelemetry::RequestMetrics::new(
24+
opentelemetry::global::meter("actix_http_tracing"),
25+
Some(|req: &actix_web::dev::ServiceRequest| {
26+
req.path() == "/metrics" && req.method() == actix_web::http::Method::GET
27+
}),
28+
Some(metrics_exporter),
29+
);
30+
31+
// Start an otel jaeger trace pipeline
32+
global::set_text_map_propagator(TraceContextPropagator::new());
33+
let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline()
34+
.with_service_name("app_name")
35+
.install()
36+
.unwrap();
37+
38+
// Initialize `tracing` using `opentelemetry-tracing` and configure logging
39+
Registry::default()
40+
.with(tracing_subscriber::EnvFilter::new("INFO"))
41+
.with(tracing_subscriber::fmt::layer())
42+
.with(tracing_opentelemetry::layer().with_tracer(tracer))
43+
.init();
44+
45+
// Start actix web with otel and tracing middlewares
46+
HttpServer::new(move || {
47+
App::new()
48+
.wrap(Logger::default())
49+
.wrap(RequestTracing::new())
50+
.wrap(request_metrics.clone())
51+
.service(web::resource("/users/{username}").to(index))
52+
})
53+
.bind("127.0.0.1:8080")?
54+
.run()
55+
.await
56+
}

examples/actix-http-tracing/trace.png

151 KB
Loading

0 commit comments

Comments
 (0)