Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
926 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "actix-http-tracing" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
actix-web = "3.2" | ||
actix-web-opentelemetry = { version = "0.8", features = ["metrics"] } | ||
opentelemetry = { version = "0.10", features = ["metrics", "tokio"] } | ||
opentelemetry-jaeger = { version = "0.9", features = ["tokio"] } | ||
opentelemetry-prometheus = "0.3.0" | ||
tracing = "0.1" | ||
tracing-opentelemetry = "0.9" | ||
tracing-subscriber = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Actix Web with Tracing example | ||
|
||
This example shows how to export data to [Jaeger] and [Prometheus] from an | ||
[actix-web] app instrumented using the [tracing] API and ecosystem. | ||
|
||
[Jaeger]: https://www.jaegertracing.io | ||
[Prometheus]: https://prometheus.io | ||
[actix-web]: https://github.com/actix/actix-web | ||
[tracing]: https://github.com/tokio-rs/tracing | ||
|
||
## Setup | ||
|
||
```shell | ||
# Run jaeger in background | ||
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 -p14268:14268 jaegertracing/all-in-one:latest | ||
|
||
# Start the actix web server | ||
$ cargo run | ||
|
||
# (from another terminal window) | ||
$ curl localhost:8080/users/@ferris | ||
=> Hello @ferris | ||
|
||
# View spans (see the image below) | ||
$ firefox http://localhost:16686/ | ||
``` | ||
|
||
## Generated Telemetry | ||
|
||
### Jaeger | ||
|
||
After completing the steps above, the following trace information is now | ||
available: | ||
|
||
![Jaeger UI](trace.png) | ||
|
||
### Prometheus (Optional) | ||
|
||
Optional request metrics information is now exposed via `/metrics`: | ||
|
||
```shell | ||
$ curl localhost:8080/metrics | ||
|
||
# HELP http_requests_duration HTTP request duration per route | ||
# TYPE http_requests_duration histogram | ||
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="0.5"} 1 | ||
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="0.9"} 1 | ||
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="0.99"} 1 | ||
http_requests_duration_bucket{method="GET",route="/users/{username}",status="200",le="+Inf"} 1 | ||
http_requests_duration_sum{method="GET",route="/users/{username}",status="200"} 0.001289 | ||
http_requests_duration_count{method="GET",route="/users/{username}",status="200"} 1 | ||
# HELP http_requests_total HTTP requests per route | ||
# TYPE http_requests_total counter | ||
http_requests_total{method="GET",route="/users/{username}",status="200"} 1 | ||
``` | ||
|
||
### Logs | ||
|
||
[tracing] has been configured to report `INFO` and above level logs to stdout | ||
via [`tracing_subscriber::fmt`] and [`tracing_subscriber::EnvFilter`] to produce | ||
the output below: | ||
|
||
```shell | ||
Nov 29 13:08:04.932 INFO actix_server::builder: Starting 16 workers | ||
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 | ||
Nov 29 13:08:08.740 INFO greet_user{username="@ferris"}: actix_http_tracing: preparing to greet user | ||
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 | ||
``` | ||
|
||
[tracing]: https://github.com/tokio-rs/tracing | ||
[`tracing_subscriber::fmt`]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/index.html | ||
[`tracing_subscriber::EnvFilter`]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use actix_web::middleware::Logger; | ||
use actix_web::{web, App, HttpServer}; | ||
use actix_web_opentelemetry::RequestTracing; | ||
use opentelemetry::{global, sdk::propagation::TraceContextPropagator}; | ||
use std::io; | ||
use tracing_subscriber::prelude::*; | ||
use tracing_subscriber::Registry; | ||
|
||
async fn index(username: actix_web::web::Path<String>) -> String { | ||
greet_user(username.as_ref()) | ||
} | ||
|
||
#[tracing::instrument] | ||
fn greet_user(username: &str) -> String { | ||
tracing::info!("preparing to greet user"); | ||
format!("Hello {}", username) | ||
} | ||
|
||
#[actix_web::main] | ||
async fn main() -> io::Result<()> { | ||
// Start an (optional) otel prometheus metrics pipeline | ||
let metrics_exporter = opentelemetry_prometheus::exporter().init(); | ||
let request_metrics = actix_web_opentelemetry::RequestMetrics::new( | ||
opentelemetry::global::meter("actix_http_tracing"), | ||
Some(|req: &actix_web::dev::ServiceRequest| { | ||
req.path() == "/metrics" && req.method() == actix_web::http::Method::GET | ||
}), | ||
Some(metrics_exporter), | ||
); | ||
|
||
// Start an otel jaeger trace pipeline | ||
global::set_text_map_propagator(TraceContextPropagator::new()); | ||
let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline() | ||
.with_service_name("app_name") | ||
.install() | ||
.unwrap(); | ||
|
||
// Initialize `tracing` using `opentelemetry-tracing` and configure logging | ||
Registry::default() | ||
.with(tracing_subscriber::EnvFilter::new("INFO")) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.with(tracing_opentelemetry::layer().with_tracer(tracer)) | ||
.init(); | ||
|
||
// Start actix web with otel and tracing middlewares | ||
HttpServer::new(move || { | ||
App::new() | ||
.wrap(Logger::default()) | ||
.wrap(RequestTracing::new()) | ||
.wrap(request_metrics.clone()) | ||
.service(web::resource("/users/{username}").to(index)) | ||
}) | ||
.bind("127.0.0.1:8080")? | ||
.run() | ||
.await | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.