Skip to content

Commit f60125e

Browse files
authored
docs: Improve metrics-basic and tracing-grpc getting started READMEs (#3477)
1 parent 34837c8 commit f60125e

2 files changed

Lines changed: 121 additions & 19 deletions

File tree

examples/metrics-basic/README.md

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
1-
# Metric Instrument Usage Example
1+
# Getting started with OpenTelemetry Rust Metrics
22

3-
This example shows how to use the various metric instruments to report metrics. The
4-
example setups a MeterProvider with stdout exporter, so metrics can be seen on
5-
the stdout.
3+
This example demonstrates the basics of recording metrics with OpenTelemetry in
4+
Rust. If you're new to OpenTelemetry metrics, this is a great place to start!
5+
6+
## Understanding OpenTelemetry Metrics
7+
8+
OpenTelemetry provides an end-user facing Metrics API. Application and library
9+
authors use this API directly (via the
10+
[`opentelemetry`](https://docs.rs/opentelemetry/latest/opentelemetry/) crate)
11+
to create instruments and record measurements. The
12+
[`opentelemetry-sdk`](https://docs.rs/opentelemetry_sdk/latest/opentelemetry_sdk/)
13+
crate provides the implementation that aggregates those measurements and
14+
forwards them to one or more exporters.
15+
16+
The Metrics API offers several instrument types, each suited to a particular
17+
kind of measurement:
18+
19+
- **Counter** - monotonically increasing values (e.g. number of requests
20+
served).
21+
- **UpDownCounter** - values that can increase or decrease (e.g. number of
22+
active connections).
23+
- **Histogram** - distribution of values (e.g. request latency).
24+
- **Gauge** - the current value of something at the time of measurement (e.g.
25+
current CPU temperature).
26+
- **Observable Counter / UpDownCounter / Gauge** - asynchronous variants where a
27+
user-supplied callback is invoked at collection time to report the current
28+
value.
29+
30+
## What This Example Does
31+
32+
This example:
33+
34+
1. Sets up an OpenTelemetry `SdkMeterProvider` with resource attributes (like
35+
service name)
36+
2. Configures a **stdout exporter** to output metrics to the console (for
37+
simplicity)
38+
3. Creates one of every instrument type (`Counter`, `UpDownCounter`,
39+
`Histogram`, `Gauge`, and their observable counterparts) and records sample
40+
measurements
41+
4. Properly shuts down the metrics pipeline so all buffered measurements are
42+
flushed
43+
44+
**Note on Exporters**: This example uses the stdout exporter for demonstration
45+
purposes. In production scenarios, you would typically use other exporters such
46+
as:
47+
48+
- **OTLP exporter** (`opentelemetry-otlp`) to send metrics to an OpenTelemetry
49+
Collector or compatible backend. See the [OTLP
50+
example](../../opentelemetry-otlp/examples/basic-otlp/README.md) for details.
51+
- **Prometheus exporter** (`opentelemetry-prometheus`) to expose metrics in a
52+
Prometheus-scrapeable format.
53+
- Other vendor-specific exporters for your observability platform.
654

755
## Usage
856

9-
Run the following, and the Metrics will be written out to stdout.
57+
Run the example to see metrics being recorded through the OpenTelemetry Metrics
58+
API and output via the stdout exporter:
1059

1160
```shell
12-
$ cargo run
61+
cargo run
1362
```
63+
64+
You'll see the metric output in your console, demonstrating how OpenTelemetry
65+
collects, aggregates, and exports measurements from each instrument type.

examples/tracing-grpc/README.md

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
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

Comments
 (0)