Skip to content

Commit

Permalink
Fix tracing grpc example (#1493)
Browse files Browse the repository at this point in the history
Cleaned up readme as we already removed tracing-opentelemetry and jaeger
from it.
  • Loading branch information
cijothomas committed Jan 26, 2024
1 parent 85f678a commit 433c1b3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
5 changes: 3 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ This example uses following crates from this repo:
**Tracing**

This example uses following crates from this repo:

- opentelemetry(tracing)
- opentelemetry-jaeger
- opentelemetry-stdout

The application is built using `tokio`.

Check this example if you want to understand *how to integrate tracing with opentelemetry*.
Check this example if you want to understand *how to create spans and propagate/restore context in OpenTelemetry*.
1 change: 1 addition & 0 deletions examples/tracing-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["trace
prost = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tonic = { workspace = true }
serde_json = { workspace = true }

[build-dependencies]
tonic-build = "0.9.2"
23 changes: 12 additions & 11 deletions examples/tracing-grpc/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# GRPC example

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.
Example showing [Tonic] client and server interaction with OpenTelemetry context
propagation. Traces are exported to stdout.

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

Examples
--------
## Running the example

```shell
# Run jaeger in background
$ docker run -d -p6831:6831/udp -p6832:6832/udp -p16686:16686 jaegertracing/all-in-one:latest

# Run the server
# Run the server first
$ cargo run --bin grpc-server

# Now run the client to make a request to the server
$ cargo run --bin grpc-client

# View spans (see the image below)
$ firefox http://localhost:16686/
```

![Jaeger UI](trace.png)
Observe that the traces are exported to stdout, and that they share the same
TraceId. Also, the server span would be parented to the client span. The example
demonstrates how to propagate and restore OpenTelemetry context when making
out-of-process calls, so as to ensure the same trace is continued in the next
process. The client here initiates the trace by creating the root client span,
and it propagates its context to the server. The server, extracts the context,
and creates its own server span using the extracted context, ensuring both spans
are correlated.
12 changes: 10 additions & 2 deletions examples/tracing-grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use opentelemetry::{global, propagation::Injector};
use opentelemetry_sdk::{
propagation::TraceContextPropagator, runtime::Tokio, trace::TracerProvider,
};
use opentelemetry_stdout::SpanExporter;
use opentelemetry_stdout::SpanExporterBuilder;

use opentelemetry::{
trace::{SpanKind, TraceContextExt, Tracer},
Expand All @@ -15,7 +15,15 @@ fn init_tracer() {
global::set_text_map_propagator(TraceContextPropagator::new());
// Install stdout exporter pipeline to be able to retrieve the collected spans.
let provider = TracerProvider::builder()
.with_batch_exporter(SpanExporter::default(), Tokio)
.with_batch_exporter(
SpanExporterBuilder::default()
.with_encoder(|writer, data| {
serde_json::to_writer_pretty(writer, &data).unwrap();
Ok(())
})
.build(),
Tokio,
)
.build();

global::set_tracer_provider(provider);
Expand Down
12 changes: 10 additions & 2 deletions examples/tracing-grpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ use opentelemetry::{
use opentelemetry_sdk::{
propagation::TraceContextPropagator, runtime::Tokio, trace::TracerProvider,
};
use opentelemetry_stdout::SpanExporter;
use opentelemetry_stdout::SpanExporterBuilder;
use tonic::{transport::Server, Request, Response, Status};

fn init_tracer() {
global::set_text_map_propagator(TraceContextPropagator::new());
// Install stdout exporter pipeline to be able to retrieve the collected spans.
let provider = TracerProvider::builder()
.with_batch_exporter(SpanExporter::default(), Tokio)
.with_batch_exporter(
SpanExporterBuilder::default()
.with_encoder(|writer, data| {
serde_json::to_writer_pretty(writer, &data).unwrap();
Ok(())
})
.build(),
Tokio,
)
.build();

global::set_tracer_provider(provider);
Expand Down

0 comments on commit 433c1b3

Please sign in to comment.