-
Notifications
You must be signed in to change notification settings - Fork 549
docs: Add tracing tutorial with Jaeger #3071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cijothomas
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
cijothomas:cijothomas/tutorial1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b495cb3
git commit -am "docs: Add tracing tutorial with Jaeger"
cijothomas 3439ef6
fmt
cijothomas 98311f4
spelling
cijothomas eb6082e
Merge branch 'main' into cijothomas/tutorial1
cijothomas 0106789
fix cargo
cijothomas d227dc8
Merge branch 'main' into cijothomas/tutorial1
cijothomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "all-signals" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
rust-version = "1.75.0" | ||
publish = false | ||
|
||
[[bin]] | ||
name = "all-signals" | ||
path = "src/main.rs" | ||
bench = false | ||
|
||
[dependencies] | ||
opentelemetry = { path = "../../opentelemetry" } | ||
opentelemetry_sdk = { path = "../../opentelemetry-sdk" } | ||
opentelemetry-otlp = { workspace = true, features = ["grpc-tonic"] } | ||
tokio = { workspace = true, features = ["full"] } | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Getting Started with OpenTelemetry Tracing in Rust | ||
|
||
This tutorial demonstrates how to instrument a Rust application with | ||
OpenTelemetry tracing and visualize the traces using | ||
[Jaeger](https://www.jaegertracing.io/). | ||
|
||
## Running the example | ||
|
||
### Prerequisites | ||
|
||
- **Docker**: Install from [docker.com](https://docs.docker.com/get-docker/) for | ||
running Jaeger locally | ||
|
||
### Step 1: Start Jaeger | ||
|
||
Start Jaeger using Docker: | ||
|
||
```shell | ||
docker run --rm -d --name jaeger \ | ||
-p 16686:16686 \ | ||
-p 4317:4317 \ | ||
cr.jaegertracing.io/jaegertracing/jaeger:2.8.0 | ||
``` | ||
|
||
This exposes: | ||
|
||
- Port `16686`: Jaeger web UI | ||
- Port `4317`: OTLP endpoint for receiving traces | ||
|
||
Verify it's running at <http://localhost:16686/> | ||
|
||
 | ||
|
||
### Step 2: Run the application | ||
|
||
```shell | ||
cargo run | ||
``` | ||
|
||
This will: | ||
|
||
1. Initialize OpenTelemetry with OTLP exporter pointing to `localhost:4317` | ||
2. Create a parent span (`Main operation`) and child span (`Sub operation`) | ||
3. Send the trace data to Jaeger | ||
|
||
### Step 3: View traces in Jaeger | ||
|
||
1. Open [http://localhost:16686](http://localhost:16686) and refresh | ||
2. Select `DemoApp` from the **Service** dropdown | ||
3. Click **Find Traces** | ||
|
||
 | ||
|
||
Click on a trace to open Trace Details view, which shows the span hierarchy and timing: | ||
|
||
 | ||
|
||
You'll see the `Main operation` span containing the `Sub operation` span, along | ||
with their timing information and any attributes or events. | ||
|
||
## Data flow | ||
|
||
```mermaid | ||
graph LR | ||
A[Rust App] -->|OTLP| B[Jaeger] | ||
B --> C[Web UI] | ||
``` | ||
|
||
### Cleanup | ||
|
||
```shell | ||
docker stop jaeger | ||
``` | ||
|
||
## Next steps (TODO) | ||
|
||
- Add metrics and logs instrumentation | ||
- Add sampling and context propagation | ||
- Add distributed tracing across multiple services |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use opentelemetry::global; | ||
use opentelemetry::trace::{TraceContextExt, Tracer}; | ||
use opentelemetry::KeyValue; | ||
use opentelemetry_otlp::SpanExporter; | ||
use opentelemetry_sdk::trace::SdkTracerProvider; | ||
use opentelemetry_sdk::Resource; | ||
use std::sync::OnceLock; | ||
use std::time::Duration; | ||
|
||
fn get_resource() -> Resource { | ||
static RESOURCE: OnceLock<Resource> = OnceLock::new(); | ||
RESOURCE | ||
.get_or_init(|| Resource::builder().with_service_name("DemoApp").build()) | ||
.clone() | ||
} | ||
|
||
fn init_traces() -> SdkTracerProvider { | ||
let exporter = SpanExporter::builder() | ||
.with_tonic() | ||
.build() | ||
.expect("Failed to create span exporter"); | ||
SdkTracerProvider::builder() | ||
.with_resource(get_resource()) | ||
.with_batch_exporter(exporter) | ||
.build() | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let tracer_provider = init_traces(); | ||
global::set_tracer_provider(tracer_provider.clone()); | ||
let tracer = global::tracer("my-application"); | ||
|
||
tracer.in_span("Main operation", |cx| { | ||
let span = cx.span(); | ||
span.set_attribute(KeyValue::new("operation.name", "demo")); | ||
|
||
// Simulate some work | ||
std::thread::sleep(Duration::from_millis(200)); | ||
|
||
tracer.in_span("Sub operation", |cx| { | ||
let span = cx.span(); | ||
span.set_attribute(KeyValue::new("operation.type", "processing")); | ||
|
||
// Simulate sub-operation work | ||
std::thread::sleep(Duration::from_millis(50)); | ||
|
||
span.add_event("Processing completed", vec![]); | ||
}); | ||
}); | ||
|
||
tracer_provider | ||
.shutdown() | ||
.expect("Failed to shutdown tracer provider"); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.