Skip to content

Commit 433c1b3

Browse files
authored
Fix tracing grpc example (#1493)
Cleaned up readme as we already removed tracing-opentelemetry and jaeger from it.
1 parent 85f678a commit 433c1b3

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

examples/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ This example uses following crates from this repo:
3232
**Tracing**
3333

3434
This example uses following crates from this repo:
35+
3536
- opentelemetry(tracing)
36-
- opentelemetry-jaeger
37+
- opentelemetry-stdout
3738

3839
The application is built using `tokio`.
3940

40-
Check this example if you want to understand *how to integrate tracing with opentelemetry*.
41+
Check this example if you want to understand *how to create spans and propagate/restore context in OpenTelemetry*.

examples/tracing-grpc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["trace
2020
prost = { workspace = true }
2121
tokio = { workspace = true, features = ["full"] }
2222
tonic = { workspace = true }
23+
serde_json = { workspace = true }
2324

2425
[build-dependencies]
2526
tonic-build = "0.9.2"

examples/tracing-grpc/README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
# GRPC example
22

3-
Example showing [Tonic] client and server interaction with OpenTelemetry context propagation. [tracing_opentelemetry](https://docs.rs/tracing-opentelemetry/0.4.0/tracing_opentelemetry/) is used to hook into the [tracing](https://github.com/tokio-rs/tracing) ecosystem, which enables drop-in replacements for [log](https://github.com/rust-lang/log) macros and an `#[instrument]` macro that will automatically add spans to your functions.
3+
Example showing [Tonic] client and server interaction with OpenTelemetry context
4+
propagation. Traces are exported to stdout.
45

56
[Tonic]: https://github.com/hyperium/tonic
67

7-
Examples
8-
--------
8+
## Running the example
99

1010
```shell
11-
# Run jaeger in background
12-
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 jaegertracing/all-in-one:latest
13-
14-
# Run the server
11+
# Run the server first
1512
$ cargo run --bin grpc-server
1613

1714
# Now run the client to make a request to the server
1815
$ cargo run --bin grpc-client
19-
20-
# View spans (see the image below)
21-
$ firefox http://localhost:16686/
2216
```
2317

24-
![Jaeger UI](trace.png)
18+
Observe that the traces are exported to stdout, and that they share the same
19+
TraceId. Also, the server span would be parented to the client span. The example
20+
demonstrates how to propagate and restore OpenTelemetry context when making
21+
out-of-process calls, so as to ensure the same trace is continued in the next
22+
process. The client here initiates the trace by creating the root client span,
23+
and it propagates its context to the server. The server, extracts the context,
24+
and creates its own server span using the extracted context, ensuring both spans
25+
are correlated.

examples/tracing-grpc/src/client.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use opentelemetry::{global, propagation::Injector};
44
use opentelemetry_sdk::{
55
propagation::TraceContextPropagator, runtime::Tokio, trace::TracerProvider,
66
};
7-
use opentelemetry_stdout::SpanExporter;
7+
use opentelemetry_stdout::SpanExporterBuilder;
88

99
use opentelemetry::{
1010
trace::{SpanKind, TraceContextExt, Tracer},
@@ -15,7 +15,15 @@ fn init_tracer() {
1515
global::set_text_map_propagator(TraceContextPropagator::new());
1616
// Install stdout exporter pipeline to be able to retrieve the collected spans.
1717
let provider = TracerProvider::builder()
18-
.with_batch_exporter(SpanExporter::default(), Tokio)
18+
.with_batch_exporter(
19+
SpanExporterBuilder::default()
20+
.with_encoder(|writer, data| {
21+
serde_json::to_writer_pretty(writer, &data).unwrap();
22+
Ok(())
23+
})
24+
.build(),
25+
Tokio,
26+
)
1927
.build();
2028

2129
global::set_tracer_provider(provider);

examples/tracing-grpc/src/server.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ use opentelemetry::{
88
use opentelemetry_sdk::{
99
propagation::TraceContextPropagator, runtime::Tokio, trace::TracerProvider,
1010
};
11-
use opentelemetry_stdout::SpanExporter;
11+
use opentelemetry_stdout::SpanExporterBuilder;
1212
use tonic::{transport::Server, Request, Response, Status};
1313

1414
fn init_tracer() {
1515
global::set_text_map_propagator(TraceContextPropagator::new());
1616
// Install stdout exporter pipeline to be able to retrieve the collected spans.
1717
let provider = TracerProvider::builder()
18-
.with_batch_exporter(SpanExporter::default(), Tokio)
18+
.with_batch_exporter(
19+
SpanExporterBuilder::default()
20+
.with_encoder(|writer, data| {
21+
serde_json::to_writer_pretty(writer, &data).unwrap();
22+
Ok(())
23+
})
24+
.build(),
25+
Tokio,
26+
)
1927
.build();
2028

2129
global::set_tracer_provider(provider);

0 commit comments

Comments
 (0)