-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementation of collector http client with pure hyper (#853)
- Loading branch information
Showing
15 changed files
with
259 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "actix-hyper-example" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio"] } | ||
opentelemetry-jaeger = { path = "../../opentelemetry-jaeger", features = ["hyper_collector_client", "rt-tokio-current-thread"] } | ||
actix-web = "4.1.0" | ||
actix-service = "2.0.0" | ||
env_logger = "0.9.0" | ||
tokio = { version = "1", features = ["full"] } |
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,24 @@ | ||
# Actix-web - Jaeger example with HTTP collector and batch exporter | ||
|
||
This example shows how to export spans from an actix-web application and ship them | ||
to the collector directly via HTTP. | ||
It uses the batch exporter to avoid excessive network roundtrips to Jaeger. | ||
|
||
## Usage | ||
|
||
Launch the application: | ||
```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 | ||
|
||
# View spans | ||
$ firefox http://localhost:16686/ | ||
``` | ||
|
||
Fire a request: | ||
```bash | ||
curl http://localhost:8088 | ||
``` |
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_service::Service; | ||
use actix_web::middleware::Logger; | ||
use actix_web::{web, App, HttpServer}; | ||
use opentelemetry::global::shutdown_tracer_provider; | ||
use opentelemetry::trace::TraceError; | ||
use opentelemetry::{global, sdk::trace as sdktrace}; | ||
use opentelemetry::{ | ||
trace::{FutureExt, TraceContextExt, Tracer}, | ||
Key, | ||
}; | ||
|
||
fn init_tracer() -> Result<sdktrace::Tracer, TraceError> { | ||
opentelemetry_jaeger::new_collector_pipeline() | ||
.with_endpoint("http://127.0.0.1:14268/api/traces") | ||
.with_service_name("trace-http-demo") | ||
.with_hyper() | ||
.install_batch(opentelemetry::runtime::TokioCurrentThread) | ||
} | ||
|
||
async fn index() -> &'static str { | ||
let tracer = global::tracer("request"); | ||
tracer.in_span("index", |ctx| { | ||
ctx.span().set_attribute(Key::new("parameter").i64(10)); | ||
"Index" | ||
}) | ||
} | ||
|
||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
std::env::set_var("RUST_LOG", "debug"); | ||
env_logger::init(); | ||
let tracer = init_tracer().expect("Failed to initialise tracer."); | ||
|
||
HttpServer::new(move || { | ||
let tracer = tracer.clone(); | ||
App::new() | ||
.wrap(Logger::default()) | ||
.wrap_fn(move |req, srv| { | ||
tracer.in_span("middleware", move |cx| { | ||
cx.span() | ||
.set_attribute(Key::new("path").string(req.path().to_string())); | ||
srv.call(req).with_context(cx) | ||
}) | ||
}) | ||
.route("/", web::get().to(index)) | ||
}) | ||
.bind("127.0.0.1:8088") | ||
.unwrap() | ||
.run() | ||
.await?; | ||
|
||
// wait until all pending spans get exported. | ||
shutdown_tracer_provider(); | ||
|
||
Ok(()) | ||
} |
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.