Skip to content

Commit 5211835

Browse files
Ureq (#121)
1 parent acc2f5f commit 5211835

File tree

12 files changed

+182
-50
lines changed

12 files changed

+182
-50
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ serialize = ["serde", "bincode"]
4343
members = [
4444
"opentelemetry-jaeger",
4545
"opentelemetry-zipkin",
46-
"examples/actix",
46+
"examples/actix-udp",
47+
"examples/actix-http",
4748
"examples/async",
4849
"examples/basic",
4950
"examples/grpc",

examples/actix-http/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "actix-http-example"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
opentelemetry = { path = "../../" }
8+
opentelemetry-jaeger = { path = "../../opentelemetry-jaeger", features = ["collector_client"] }
9+
thrift = "0.13.0"
10+
futures = "0.3"
11+
actix-web = "2"
12+
actix-service = "1"
13+
actix-rt = "1"
14+
env_logger = "0.7.1"
15+
tokio = { version = "0.2.21", features = ["full"] }

examples/actix-http/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Actix-web - Jaeger example with HTTP collector and batch exporter
2+
3+
This example shows how to export spans from an actix-web application and ship them
4+
to the collector directly via HTTP.
5+
It uses the batch exporter to avoid excessive network roundtrips to Jaeger.
6+
7+
## Usage
8+
9+
Launch the application:
10+
```shell
11+
# Run jaeger in background
12+
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 -p14268:14268 jaegertracing/all-in-one:latest
13+
14+
# Start the actix-web server
15+
$ cargo run
16+
17+
# View spans
18+
$ firefox http://localhost:16686/
19+
```
20+
21+
Fire a request:
22+
```bash
23+
curl http://localhost:8088
24+
```

examples/actix-http/src/main.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use actix_service::Service;
2+
use actix_web::middleware::Logger;
3+
use actix_web::{web, App, HttpServer};
4+
use opentelemetry::api::{Key, TraceContextExt, Tracer};
5+
use opentelemetry::sdk::BatchSpanProcessor;
6+
use opentelemetry::{global, sdk};
7+
use opentelemetry::api::trace::futures::FutureExt;
8+
9+
fn init_tracer() -> thrift::Result<()> {
10+
let exporter = opentelemetry_jaeger::Exporter::builder()
11+
.with_collector_endpoint("http://127.0.0.1:14268/api/traces")
12+
.with_process(opentelemetry_jaeger::Process {
13+
service_name: "trace-http-demo".to_string(),
14+
tags: vec![
15+
Key::new("exporter").string("jaeger"),
16+
Key::new("float").f64(312.23),
17+
],
18+
})
19+
.init()?;
20+
21+
let batch_exporter = BatchSpanProcessor::builder(exporter, tokio::spawn, tokio::time::interval)
22+
.build();
23+
24+
let provider = sdk::Provider::builder()
25+
.with_batch_exporter(batch_exporter)
26+
.with_config(sdk::Config {
27+
default_sampler: Box::new(sdk::Sampler::Always),
28+
..Default::default()
29+
})
30+
.build();
31+
global::set_provider(provider);
32+
33+
Ok(())
34+
}
35+
36+
async fn index() -> &'static str {
37+
let tracer = global::tracer("request");
38+
tracer.in_span("index", |ctx| {
39+
ctx.span().set_attribute(Key::new("parameter").i64(10));
40+
"Index"
41+
})
42+
}
43+
44+
#[actix_rt::main]
45+
async fn main() -> std::io::Result<()> {
46+
std::env::set_var("RUST_LOG", "debug");
47+
env_logger::init();
48+
init_tracer().expect("Failed to initialise tracer.");
49+
50+
HttpServer::new(|| {
51+
App::new()
52+
.wrap(Logger::default())
53+
.wrap_fn(|req, srv| {
54+
let tracer = global::tracer("request");
55+
tracer.in_span("middleware", move |cx| {
56+
cx.span().set_attribute(Key::new("path").string(req.path()));
57+
srv.call(req).with_context(cx)
58+
})
59+
})
60+
.route("/", web::get().to(index))
61+
})
62+
.bind("127.0.0.1:8088")
63+
.unwrap()
64+
.run()
65+
.await
66+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[package]
2-
name = "actix-example"
2+
name = "actix-udp-example"
33
version = "0.1.0"
44
edition = "2018"
55

66
[dependencies]
77
opentelemetry = { path = "../../" }
88
opentelemetry-jaeger = { path = "../../opentelemetry-jaeger" }
99
thrift = "0.13.0"
10-
futures = "0.1.25"
11-
actix-web = "1.0.9"
12-
actix-service = "0.4.1"
10+
futures = "0.3"
11+
actix-web = "2"
12+
actix-service = "1"
13+
actix-rt = "1"
14+
env_logger = "0.7.1"

examples/actix-udp/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Actix-web - Jaeger example with UDP agent
2+
3+
This example shows how to export spans from an actix-web application and ship them
4+
to the Jaeger agent over UDP.
5+
6+
## Usage
7+
8+
Launch the application:
9+
```shell
10+
# Run jaeger in background
11+
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 -p14268:14268 jaegertracing/all-in-one:latest
12+
13+
# Start the actix-web server
14+
$ cargo run
15+
16+
# View spans
17+
$ firefox http://localhost:16686/
18+
```
19+
20+
Fire a request:
21+
```bash
22+
curl http://localhost:8088
23+
```

examples/actix/src/main.rs renamed to examples/actix-udp/src/main.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use actix_service::Service;
2+
use actix_web::middleware::Logger;
23
use actix_web::{web, App, HttpServer};
3-
use futures::future::Future;
4+
use opentelemetry::api::trace::futures::FutureExt;
45
use opentelemetry::api::{Key, TraceContextExt, Tracer};
56
use opentelemetry::{global, sdk};
67

@@ -27,31 +28,34 @@ fn init_tracer() -> thrift::Result<()> {
2728
Ok(())
2829
}
2930

30-
fn index() -> &'static str {
31+
async fn index() -> &'static str {
3132
let tracer = global::tracer("request");
3233
tracer.in_span("index", |ctx| {
3334
ctx.span().set_attribute(Key::new("parameter").i64(10));
3435
"Index"
3536
})
3637
}
3738

38-
fn main() -> thrift::Result<()> {
39-
init_tracer()?;
39+
#[actix_rt::main]
40+
async fn main() -> std::io::Result<()> {
41+
std::env::set_var("RUST_LOG", "debug");
42+
env_logger::init();
43+
init_tracer().expect("Failed to initialise tracer.");
4044

4145
HttpServer::new(|| {
4246
App::new()
47+
.wrap(Logger::default())
4348
.wrap_fn(|req, srv| {
4449
let tracer = global::tracer("request");
4550
tracer.in_span("middleware", move |cx| {
4651
cx.span().set_attribute(Key::new("path").string(req.path()));
47-
srv.call(req).map(|res| res)
52+
srv.call(req).with_context(cx)
4853
})
4954
})
5055
.route("/", web::get().to(index))
5156
})
5257
.bind("127.0.0.1:8088")
5358
.unwrap()
54-
.run()?;
55-
56-
Ok(())
59+
.run()
60+
.await
5761
}

examples/basic/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This example shows basic span and metric usage, and exports to Jaeger.
66

77
```shell
88
# Run jaeger in background
9-
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 jaegertracing/all-in-one:latest
9+
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 -p14268:14268 jaegertracing/all-in-one:latest
1010

1111
# Report spans
1212
$ cargo run

opentelemetry-jaeger/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ edition = "2018"
1717

1818
[dependencies]
1919
opentelemetry = { version = "0.5.0", default-features = false, features = ["trace"], path = ".." }
20-
reqwest = { version = "0.10.1", features = ["blocking"], optional = true }
20+
ureq = { version = "1.0.0", optional = true }
2121
thrift = "0.13.0"
2222

2323
[features]
2424
default = []
25-
collector_client = ["reqwest"]
25+
collector_client = ["ureq"]

opentelemetry-jaeger/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,10 @@ impl<T: net::ToSocketAddrs> Builder<T> {
277277
self.collector_username,
278278
self.collector_password,
279279
)?;
280-
Ok((self.process, uploader::BatchUploader::Collector(collector)))
280+
Ok((
281+
self.process,
282+
uploader::BatchUploader::Collector(Box::new(collector)),
283+
))
281284
} else {
282285
Err(::thrift::Error::from(
283286
"Collector endpoint or agent endpoint must be set",

0 commit comments

Comments
 (0)