|
| 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 | + |
| 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 |
0 commit comments