1- # GRPC example
1+ # Getting started with OpenTelemetry Rust Tracing - gRPC Example
22
3- Example showing [ Tonic] client and server interaction with OpenTelemetry context
4- propagation. Traces are exported to stdout.
3+ This example demonstrates the basics of distributed tracing with OpenTelemetry
4+ in Rust, using a [ Tonic] ( https://github.com/hyperium/tonic ) gRPC client and
5+ server. While not the absolute simplest tracing example, it shows a realistic,
6+ end-to-end scenario: a single trace that spans two processes.
57
6- [ Tonic ] : https://github.com/hyperium/tonic
8+ ## Understanding OpenTelemetry Tracing
79
8- ## Running the example
10+ OpenTelemetry provides an end-user facing Tracing API. Application and library
11+ authors use this API directly (via the
12+ [ ` opentelemetry ` ] ( https://docs.rs/opentelemetry/latest/opentelemetry/ ) crate)
13+ to create ** spans** that represent units of work. The
14+ [ ` opentelemetry-sdk ` ] ( https://docs.rs/opentelemetry_sdk/latest/opentelemetry_sdk/ )
15+ crate provides the implementation that processes those spans and forwards them
16+ to one or more exporters.
17+
18+ A few key concepts:
19+
20+ - ** Span** - a single, named, timed operation (e.g. "handle HTTP request",
21+ "query database"). Spans can carry attributes and events.
22+ - ** Trace** - a tree of related spans, sharing a single ` TraceId ` , that
23+ together represent the lifecycle of one logical request or operation.
24+ - ** Context Propagation** - the mechanism that carries trace identifiers
25+ across process boundaries (e.g. via HTTP headers or gRPC metadata) so that
26+ spans created in different processes are stitched together into one trace.
27+
28+ ## What This Example Does
29+
30+ This example consists of two binaries - a gRPC client and a gRPC server -
31+ sharing a simple "Greeter" service definition:
32+
33+ 1 . Both client and server set up an OpenTelemetry ` SdkTracerProvider ` with a
34+ ** stdout exporter** (for simplicity) and register the W3C
35+ ` TraceContextPropagator ` as the global propagator. This propagator defines
36+ * how* trace context is serialized into and deserialized from carrier
37+ formats (HTTP headers, gRPC metadata, etc.) using the
38+ [ W3C Trace Context] ( https://www.w3.org/TR/trace-context/ ) standard - it is
39+ the piece that makes steps 2 and 3 below possible.
40+ 2 . The ** client** creates a root span (` Greeter/client ` ) for the outgoing
41+ call, then uses the globally-registered propagator to ** inject** the
42+ current trace context into the gRPC request metadata before sending it.
43+ 3 . The ** server** uses the same globally-registered propagator to ** extract**
44+ the trace context from the incoming gRPC metadata and creates its own
45+ span (` Greeter/server ` ) as a child of the client's span.
46+ 4 . Both spans are exported to stdout. Because they share the same ` TraceId `
47+ and the server span references the client span as its parent, the two
48+ pieces are correlated into a single distributed trace.
49+
50+ Without the propagator setup in step 1, the inject/extract calls would have
51+ nothing to do and the server would create an unrelated root span instead of a
52+ child of the client - giving you two disconnected traces rather than one.
53+
54+ ** Note on Exporters** : This example uses the stdout exporter for demonstration
55+ purposes. In production scenarios, you would typically use other exporters such
56+ as:
57+
58+ - ** OTLP exporter** (` opentelemetry-otlp ` ) to send traces to an OpenTelemetry
59+ Collector or compatible backend. See the [ OTLP
60+ example] ( ../../opentelemetry-otlp/examples/basic-otlp/README.md ) for details.
61+ - Other vendor-specific exporters for your observability platform.
62+
63+ ## Usage
964
1065``` shell
1166# Run the server first
@@ -15,11 +70,6 @@ $ cargo run --bin grpc-server
1570$ cargo run --bin grpc-client
1671```
1772
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.
73+ Observe in the stdout output that both spans share the same ` TraceId ` , and
74+ that the server span is parented to the client span - confirming that the
75+ trace was successfully propagated across the process boundary.
0 commit comments