Skip to content

Commit 671eafc

Browse files
committed
update observability section after last batch of breaking otel deps
via kube-rs/controller-rs#124 Signed-off-by: clux <[email protected]>
1 parent c2b8a6a commit 671eafc

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

docs/controllers/observability.md

+19-15
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,40 @@ cargo add opentelemetry-otlp
4848

4949
!!! warning "Telemetry Dependencies"
5050

51-
This simple use of `cargo add` above assumes the above dependencies always work well at all given versions, but this is not always true. You might see multiple versions of `opentelemetry` libs / `tonic` in `cargo tree` (which might not work), and due to different release cycles and pins, you might not be able to upgrade opentelemetry dependencies immediately. For working combinations see for instance the [pins in controller-rs](https://github.com/kube-rs/controller-rs/blob/main/Cargo.toml) + [examples in tracing-opentelemetry](https://github.com/tokio-rs/tracing-opentelemetry/tree/v0.1.x/examples).
51+
This simple use of `cargo add` above assumes the above dependencies may not always work well at latest versions. You might receive multiple versions of `opentelemetry` libs / `tonic` in `cargo tree` (which might not work), and due to different release cycles and pins, you might not be able to upgrade opentelemetry dependencies immediately. For working combinations see for instance the [pins in controller-rs](https://github.com/kube-rs/controller-rs/blob/main/Cargo.toml) + [examples in tracing-opentelemetry](https://github.com/tokio-rs/tracing-opentelemetry/tree/v0.1.x/examples).
5252

5353
Setting up the layer and configuring the `collector` follows fundamentally the same process:
5454

5555
```rust
5656
let otel = tracing_opentelemetry::OpenTelemetryLayer::new(init_tracer());
5757
```
5858

59-
Note 3 layers now:
59+
Change our registry setup to use 3 layers:
6060

61-
```rust
62-
Registry::default().with(env_filter).with(logger).with(otel).init();
61+
```diff
62+
-Registry::default().with(logger).with(env_filter).init();
63+
+Registry::default().with(env_filter).with(logger).with(otel).init();
6364
```
6465

6566
However, tracing requires us to have a configurable location of **where to send spans**, the provders needs to be globally registered, and you likely want to set some resource attributes, so creating the actual `tracer` requires a bit more work:
6667

6768
```rust
6869
fn init_tracer() -> opentelemetry_sdk::trace::Tracer {
6970
use opentelemetry::trace::TracerProvider;
70-
use opentelemetry_otlp::WithExportConfig;
71+
use opentelemetry_otlp::{SpanExporter, WithExportConfig};
7172
use opentelemetry_sdk::{runtime, trace::Config};
7273

7374
let endpoint = std::env::var("OPENTELEMETRY_ENDPOINT_URL").expect("Needs an otel collector");
74-
let exporter = opentelemetry_otlp::new_exporter().tonic().with_endpoint(endpoint);
75+
let exporter = SpanExporter::builder()
76+
.with_tonic()
77+
.with_endpoint(endpoint)
78+
.build()
79+
.unwrap();
7580

76-
let provider = opentelemetry_otlp::new_pipeline()
77-
.tracing()
78-
.with_exporter(exporter)
79-
.with_trace_config(Config::default().with_resource(resource()))
80-
.install_batch(runtime::Tokio)
81-
.expect("valid tracer");
81+
let provider = sdktrace::TracerProvider::builder()
82+
.with_batch_exporter(exporter, runtime::Tokio)
83+
.with_resource(resource())
84+
.build();
8285

8386
opentelemetry::global::set_tracer_provider(provider.clone());
8487
provider.tracer("tracing-otel-subscriber")
@@ -102,9 +105,10 @@ fn resource() -> Resource {
102105

103106
which can be extended better using the [opentelemetry_semantic_conventions](https://docs.rs/opentelemetry-semantic-conventions/0.26.0/opentelemetry_semantic_conventions/resource/index.html).
104107

105-
### Instrumenting
108+
For a full setup example for this code see [controller-rs/telemetry.rs](https://github.com/kube-rs/controller-rs/blob/main/src/telemetry.rs).
106109

107-
At this point, you can start adding `#[instrument]` attributes onto functions you want, in particular `reconcile`:
110+
### Instrumenting
111+
Once you have initialised your registry, you can start adding `#[instrument]` attributes onto functions you want. Let's do `reconcile`:
108112

109113
```rust
110114
#[instrument(skip(ctx))]
@@ -126,7 +130,7 @@ To link logs and traces we take advantage that tracing data is being outputted t
126130
async fn reconcile(foo: Arc<Foo>, ctx: Arc<Data>) -> Result<Action, Error> {
127131
let trace_id = get_trace_id();
128132
if trace_id != TraceId::INVALID {
129-
Span::current().record("trace_id", &field::display(&trace_id));
133+
Span::current().record("trace_id", field::display(&trace_id));
130134
}
131135
todo!("reconcile implementation")
132136
}

0 commit comments

Comments
 (0)