Skip to content

Commit 68fd718

Browse files
committed
feat: a more configurable tracing configuration with TracingConfig
1 parent 8838894 commit 68fd718

File tree

12 files changed

+811
-70
lines changed

12 files changed

+811
-70
lines changed

.mise.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ protoc = '32.1'
1616
"aqua:cargo-bins/cargo-binstall" = "1"
1717

1818
[tasks.format]
19+
alias = "fmt"
1920
description = "Format the code and sort dependencies"
2021
run = [
2122
"cargo fmt",

deny.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ skip = [
213213
# "[email protected]", # tonic depend on axum 0.7
214214
# "[email protected]", # axum 0.7 use tower 0.5, but hyper still use 0.4
215215
# "sync_wrapper", # axum direct and transive dependency use multiple version
216-
"regex-syntax",
217-
"regex-automata",
216+
# "regex-syntax",
217+
# "regex-automata",
218218
# "indexmap",
219219
# "hermit-abi",
220220
# "rustls-native-certs",

examples/axum-otlp/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tracing_opentelemetry_instrumentation_sdk::find_current_trace_id;
1111
#[tokio::main]
1212
async fn main() -> Result<(), BoxError> {
1313
// very opinionated init of tracing, look as is source to make your own
14-
let _guard = init_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()?;
14+
let _guard = init_tracing_opentelemetry::TracingConfig::production().init_subscriber()?;
1515

1616
let app = app();
1717
// run it

examples/grpc/src/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub mod generated {
1313
#[tokio::main]
1414
async fn main() -> Result<(), Box<dyn std::error::Error>> {
1515
// very opinionated init of tracing, look as is source to make your own
16-
let _guard = init_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()
16+
let _guard = init_tracing_opentelemetry::TracingConfig::production()
17+
.init_subscriber()
1718
.expect("init subscribers");
1819

1920
// let channel = Channel::from_static("http://[::1]:50051").connect().await?;

examples/grpc/src/server.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ impl Greeter for MyGreeter {
4848
#[tokio::main]
4949
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5050
// very opinionated init of tracing, look as is source to make your own
51-
let _guard = init_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()
51+
let _guard = init_tracing_opentelemetry::TracingConfig::production()
52+
.init_subscriber()
5253
.expect("init subscribers");
5354

5455
let addr = "0.0.0.0:50051".parse()?;

examples/load/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tracing_opentelemetry_instrumentation_sdk::otel_trace_span;
77
#[tokio::main]
88
async fn main() -> Result<(), Box<dyn std::error::Error>> {
99
// very opinionated init of tracing, look as is source to make your own
10-
let _guard = init_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()?;
10+
let _guard = init_tracing_opentelemetry::TracingConfig::production().init_subscriber()?;
1111
let mut stats = memory_stats();
1212
if stats.is_none() {
1313
eprintln!("Couldn't get the current memory usage :(");

init-tracing-opentelemetry/Cargo.toml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ opentelemetry-otlp = { workspace = true, optional = true, features = [
2020
"trace",
2121
] }
2222
# opentelemetry-resource-detectors = { workspace = true } //FIXME enable when available for opentelemetry >= 0.25
23-
opentelemetry-stdout = { workspace = true, features = ["trace"], optional = true }
23+
opentelemetry-stdout = { workspace = true, features = [
24+
"trace",
25+
], optional = true }
2426
opentelemetry-semantic-conventions = { workspace = true, optional = true }
2527
opentelemetry-zipkin = { workspace = true, features = [], optional = true }
2628
opentelemetry_sdk = { workspace = true }
@@ -39,16 +41,17 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
3941
[dev-dependencies]
4042
assert2 = { workspace = true }
4143
rstest = { workspace = true }
42-
# need tokio runtime to run smoke tests.
44+
# need tokio runtime to run smoke tests and rust doc
45+
tracing-opentelemetry-instrumentation-sdk = { path = "../tracing-opentelemetry-instrumentation-sdk" }
4346
opentelemetry_sdk = { workspace = true, features = [
4447
"trace",
4548
"rt-tokio",
4649
"testing",
4750
] }
4851
serde = { version = "1", features = ["derive"] }
4952
serde_json = "1"
53+
tokio = { version = "1", features = ["full"] }
5054
tokio-stream = { version = "0.1" }
51-
tracing-opentelemetry-instrumentation-sdk = { path = "../tracing-opentelemetry-instrumentation-sdk" }
5255
tracing-subscriber = { version = "0.3", default-features = false, features = [
5356
"env-filter",
5457
"fmt",
@@ -72,4 +75,8 @@ tls = ["opentelemetry-otlp/tls", "tonic"]
7275
tls-roots = ["opentelemetry-otlp/tls-roots"]
7376
tls-webpki-roots = ["opentelemetry-otlp/tls-webpki-roots"]
7477
logfmt = ["dep:tracing-logfmt"]
75-
metrics = ["opentelemetry-otlp/metrics", "tracing-opentelemetry/metrics", "opentelemetry-stdout/metrics"]
78+
metrics = [
79+
"opentelemetry-otlp/metrics",
80+
"tracing-opentelemetry/metrics",
81+
"opentelemetry-stdout/metrics",
82+
]

init-tracing-opentelemetry/README.md

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,65 @@
55

66
A set of helpers to initialize (and more) tracing + opentelemetry (compose your own or use opinionated preset)
77

8-
```txt
8+
```rust
9+
#[tokio::main]
10+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
11+
// Simple preset
12+
let _guard = init_tracing_opentelemetry::TracingConfig::production().init_subscriber()?;
13+
14+
//...
15+
16+
Ok(())
17+
}
18+
```
19+
20+
```rust
921
#[tokio::main]
10-
async fn main() -> Result<(), axum::BoxError> {
11-
// very opinionated init of tracing, look as is source to compose your own
12-
let _guard = init_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()?;
22+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
23+
// custom configuration
24+
let _guard = init_tracing_opentelemetry::TracingConfig::default()
25+
.with_json_format()
26+
.with_stderr()
27+
.with_log_directives("debug")
28+
.init_subscriber()?;
1329

14-
...;
30+
//...
1531

1632
Ok(())
1733
}
1834
```
1935

20-
The `init_subscribers` function returns a `OtelGuard` instance. Following the guard pattern, this struct provides no functions but, when dropped, ensures that any pending traces/metrics are sent before it exits. The syntax `let _guard` is suggested to ensure that Rust does not drop the struct until the application exits.
36+
The `init_subscriber()` function returns an `OtelGuard` instance. Following the guard pattern, this struct provides no functions but, when dropped, ensures that any pending traces/metrics are sent before it exits. The syntax `let _guard` is suggested to ensure that Rust does not drop the struct until the application exits.
37+
38+
## Configuration Options
39+
40+
### Presets
41+
42+
- `TracingConfig::development()` - Pretty format, stderr, with debug info
43+
- `TracingConfig::production()` - JSON format, stdout, minimal metadata
44+
- `TracingConfig::debug()` - Full verbosity with all span events
45+
- `TracingConfig::minimal()` - Compact format, no OpenTelemetry
46+
- `TracingConfig::testing()` - Minimal output for tests
47+
48+
### Custom Configuration
49+
50+
```rust,no_run
51+
use init_tracing_opentelemetry::TracingConfig;
52+
53+
TracingConfig::default()
54+
.with_pretty_format() // or .with_json_format(), .with_compact_format()
55+
.with_stderr() // or .with_stdout(), .with_file(path)
56+
.with_log_directives("debug") // Custom log levels
57+
.with_line_numbers(true) // Include line numbers
58+
.with_thread_names(true) // Include thread names
59+
.with_otel(true) // Enable OpenTelemetry
60+
.init_subscriber()
61+
.expect("valid tracing configuration");
62+
```
63+
64+
### Legacy API (deprecated)
2165

22-
To configure opentelemetry tracer & tracing (& metrics), you can use the functions from `init_tracing_opentelemetry::tracing_subscriber_ext`, but they are very opinionated (and WIP to make them more customizable and friendly), so we recommend making your composition, but look at the code (to avoid some issue) and share your feedback.
66+
For backward compatibility, the old API is still available:
2367

2468
```txt
2569
pub fn build_loglevel_filter_layer() -> tracing_subscriber::filter::EnvFilter {
@@ -150,12 +194,12 @@ spec:
150194

151195
- check you only have a single version of opentelemtry (could be part of your CI/build), use `cargo-deny` or `cargo tree`
152196

153-
```sh
154-
# Check only one version of opentelemetry should be used
155-
# else issue with setup of global (static variable)
156-
# check_single_version_opentelemtry:
157-
cargo tree -i opentelemetry
158-
```
197+
```sh
198+
# Check only one version of opentelemetry should be used
199+
# else issue with setup of global (static variable)
200+
# check_single_version_opentelemtry:
201+
cargo tree -i opentelemetry
202+
```
159203

160204
- check the code of your exporter and the integration with `tracing` (as subscriber's layer)
161205
- check the environment variables of opentelemetry `OTEL_EXPORTER...` and `OTEL_TRACES_SAMPLER` (values are logged on target `otel::setup` )
@@ -173,7 +217,7 @@ Configure the following set of environment variables to configure the metrics ex
173217
- `OTEL_EXPORTER_OTLP_METRICS_PROTOCOL` override to `OTEL_EXPORTER_OTLP_PROTOCOL`, fallback to auto-detection based on ENDPOINT port
174218
- `OTEL_EXPORTER_OTLP_METRICS_TIMEOUT` to set the timeout for the connection to the exporter
175219
- `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE` to set the temporality preference for the exporter
176-
- `OTEL_METRIC_EXPORT_INTERVAL` to set frequence of metrics export in __*milliseconds*__, defaults to 60s
220+
- `OTEL_METRIC_EXPORT_INTERVAL` to set frequence of metrics export in **_milliseconds_**, defaults to 60s
177221

178222
## Changelog - History
179223

0 commit comments

Comments
 (0)