Skip to content

Commit 1fc86da

Browse files
authored
0.28 migration guide and OTLP Example fixes (#2622)
1 parent 61e539f commit 1fc86da

File tree

6 files changed

+270
-56
lines changed

6 files changed

+270
-56
lines changed

docs/migration_0.28.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Migration guide from 0.27 to 0.28
2+
3+
OpenTelemetry Rust 0.28 introduces a large number of breaking changes that
4+
impact all signals (logs/metrics/traces). This guide is intended to help with a
5+
smooth migration for the common use cases of using `opentelemetry`,
6+
`opentelemetry_sdk` `opentelemetry-otlp`, `opentelemetry-appender-tracing`
7+
crates. The detailed changelog for each crate that you use can be consulted for
8+
the full set of changes. This doc covers only the common scenario.
9+
10+
## Tracing Shutdown changes
11+
12+
`opentelemetry::global::shutdown_tracer_provider()` is removed. Now, you should
13+
explicitly call shutdown() on the created tracer provider.
14+
15+
Before (0.27):
16+
17+
```rust
18+
opentelemetry::global::shutdown_tracer_provider();
19+
```
20+
21+
After (0.28):
22+
23+
```rust
24+
let tracer_provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
25+
.build();
26+
27+
// Clone and set the tracer provider globally. Retain the original to invoke shutdown later.
28+
opentelemetry::global::set_tracer_provider(tracer_provider.clone());
29+
30+
// Shutdown the provider when application is exiting.
31+
tracer_provider.shutdown();
32+
```
33+
34+
This now makes shutdown consistent across signals.
35+
36+
## Rename SDK Structs
37+
38+
`LoggerProvider`, `TracerProvider` are renamed to `SdkLoggerProvider` and
39+
`SdkTracerProvider` respectively. `MeterProvider` was already named
40+
`SdkMeterProvider` and this now ensures consistency across signals.
41+
42+
### Async Runtime Requirements removed
43+
44+
When using OTLP Exporter for Logs, Traces a "batching" exporter is recommended.
45+
Also, metrics always required a component named `PeriodicReader`. These
46+
components previously needed user to pass in an async runtime and enable
47+
appropriate feature flag depending on the runtime.
48+
49+
These components have been re-written to no longer require an async runtime.
50+
Instead they operate by spawning dedicated background thread, and making
51+
blocking calls from the same.
52+
53+
PeriodicReader, BatchSpanProcessor, BatchLogProcessor are the components
54+
affected.
55+
56+
For Logs,Traces replace `.with_batch_exporter(exporter, runtime::Tokio)` with
57+
`.with_batch_exporter(exporter)`.
58+
59+
For Metrics, replace `let reader =
60+
PeriodicReader::builder(exporter, runtime::Tokio).build();` with `let reader =
61+
PeriodicReader::builder(exporter).build();` or more conveniently,
62+
`.with_periodic_exporter(exporter)`.
63+
64+
Please note the following:
65+
66+
* With the new approach, only the following grpc/http clients are supported in
67+
`opentelemetry-otlp`.
68+
69+
`grpc-tonic` (OTLP
70+
Exporter must be created from within a Tokio runtime)
71+
72+
`reqwest-blocking-client`
73+
74+
In other words,
75+
`reqwest` and `hyper` are not supported.
76+
If using exporters other than `opentelemetry-otlp`, consult the docs
77+
for the same to know if there are any restrictions/requirements on async
78+
runtime.
79+
80+
* Timeout enforcement is now moved to Exporters. i.e
81+
BatchProcessor,PeriodicReader does not enforce timeouts. For logs and traces,
82+
`max_export_timeout` (on Processors) or `OTEL_BLRP_EXPORT_TIMEOUT` or
83+
`OTEL_BSP_EXPORT_TIMEOUT` is no longer supported. For metrics, `with_timeout` on
84+
PeriodicReader is no longer supported.
85+
86+
`OTEL_EXPORTER_OTLP_TIMEOUT` can be used to setup timeout for OTLP Exporters
87+
via environment variables, or `.with_tonic().with_timeout()` or
88+
`.with_http().with_timeout()` programmatically.
89+
90+
* If you need the old behavior (your application cannot spawn a new thread, or
91+
need to use another networking client etc.) use appropriate feature flag(s) from
92+
below.
93+
“experimental_metrics_periodicreader_with_async_runtime”
94+
"experimental_logs_batch_log_processor_with_async_runtime"
95+
"experimental_trace_batch_span_processor_with_async_runtime"
96+
97+
**and** adjust the namespace:
98+
99+
Example, when using Tokio runtime.
100+
101+
```rust
102+
let reader = opentelemetry_sdk::metrics::periodic_reader_with_async_runtime::PeriodicReader::builder(exporter, runtime::Tokio).build();
103+
let tracer_provider = SdkTracerProvider::builder()
104+
.with_span_processor(span_processor_with_async_runtime::BatchSpanProcessor::builder(exporter, runtime::Tokio).build())
105+
.build();
106+
let logger_provider = SdkLoggerProvider::builder()
107+
.with_log_processor(log_processor_with_async_runtime::BatchLogProcessor::builder(exporter, runtime::Tokio).build())
108+
.build();
109+
```
110+
111+
## OTLP Default change
112+
113+
"grpc-tonic" feature flag is no longer enabled by default in
114+
`opentelemetry-otlp`. "http-proto" and "reqwest-blocking-client" features are
115+
added as default, to align with the OTel specification.
116+
117+
## Resource Changes
118+
119+
`Resource` creation is moved to a builder pattern, and `Resource::{new, empty,
120+
from_detectors, new_with_defaults, from_schema_url, merge, default}` are
121+
replaced with `Resource::builder()`.
122+
123+
Before:
124+
125+
```rust
126+
Resource::default().with_attributes([
127+
KeyValue::new("service.name", "test_service"),
128+
KeyValue::new("key", "value"),
129+
]);
130+
```
131+
132+
After:
133+
134+
```rust
135+
Resource::builder()
136+
.with_service_name("test_service")
137+
.with_attribute(KeyValue::new("key", "value"))
138+
.build();
139+
```
140+
141+
## Improved internal logging
142+
143+
OpenTelemetry internally used `tracing` to emit its internal logs. This is under
144+
feature-flag "internal-logs" that is enabled by default in all crates. When
145+
using OTel Logging, care must be taken to avoid OTel's own internal log being
146+
fed back to OTel, creating an circular dependency. This can be achieved via proper
147+
filtering. The OTLP Examples in the repo shows how to achieve this. It also
148+
shows how to send OTel's internal logs to stdout using `tracing::Fmt`.
149+
150+
## Full example
151+
152+
A fully runnable example application using OTLP Exporter is provided in this
153+
repo. Comparing the 0.27 vs 0.28 of the example would give a good overview of
154+
the changes required to be made.
155+
156+
[Basic OTLP Example
157+
(0.27)](https://github.com/open-telemetry/opentelemetry-rust/tree/opentelemetry-otlp-0.27.0/opentelemetry-otlp/examples)
158+
[Basic OTLP Example
159+
(0.28)](https://github.com/open-telemetry/opentelemetry-rust/tree/opentelemetry-otlp-0.27.0/opentelemetry-otlp/examples)
160+
// TODO: Update this link after github tag is created.
161+
162+
This guide covers only the most common breaking changes. If you’re using custom
163+
exporters or processors (or authoring one), please consult the changelog for
164+
additional migration details.
165+
166+
## Notes on Breaking Changes and the Path to 1.0
167+
168+
We understand that breaking changes can be challenging, but they are essential
169+
for the growth and stability of the project. With the release of 0.28, the
170+
Metric API (`opentelemetry` crate, "metrics" feature flag) and LogBridge API
171+
(`opentelemetry` crate, "logs" feature flag) are now stable, and we do not
172+
anticipate further breaking changes for these components.
173+
174+
Moreover, the `opentelemetry_sdk` crate for "logs" and "metrics" will have a
175+
very high bar for any future breaking changes. Any changes are expected to
176+
primarily impact those developing custom components, such as custom exporters.
177+
In the upcoming releases, we aim to bring the "traces" feature to the same level
178+
of stability as "logs" and "metrics". Additionally, "opentelemetry-otlp", the
179+
official exporter, will also receive stability guarantees.
180+
181+
We are excited to announce that a 1.0 release, encompassing logs, metrics, and
182+
traces, is planned for June 2025. We appreciate your patience and support as we
183+
work towards this milestone. The 1.0 release will cover the API
184+
(`opentelemetry`), SDK (`opentelemetry_sdk`), OTLP Exporter
185+
(`opentelemetry-otlp`), and Tracing-Bridge (`opentelemetry-appender-tracing`).
186+
187+
We encourage you to share your feedback via GitHub issues or the OTel-Rust Slack
188+
channel [here](https://cloud-native.slack.com/archives/C03GDP0H023).

opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ reqwest-blocking = ["opentelemetry-otlp/reqwest-blocking-client"]
1313
once_cell = { workspace = true }
1414
opentelemetry = { path = "../../../opentelemetry" }
1515
opentelemetry_sdk = { path = "../../../opentelemetry-sdk" }
16-
opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "logs", "internal-logs"], default-features = false}
17-
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}
16+
opentelemetry-otlp = { path = "../.."}
17+
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing"}
1818

1919
tokio = { workspace = true, features = ["full"] }
2020
tracing = { workspace = true, features = ["std"]}

opentelemetry-otlp/examples/basic-otlp-http/src/main.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
/// To use hyper as the HTTP client - cargo run --features="hyper" --no-default-features
21
use once_cell::sync::Lazy;
32
use opentelemetry::{
43
global,
5-
trace::{TraceContextExt, TraceError, Tracer},
4+
trace::{TraceContextExt, Tracer},
65
InstrumentationScope, KeyValue,
76
};
87
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
98
use opentelemetry_otlp::WithExportConfig;
109
use opentelemetry_otlp::{LogExporter, MetricExporter, Protocol, SpanExporter};
10+
use opentelemetry_sdk::Resource;
1111
use opentelemetry_sdk::{
12-
logs::SdkLoggerProvider,
13-
metrics::{MetricError, SdkMeterProvider},
14-
trace::{self as sdktrace, SdkTracerProvider},
15-
};
16-
use opentelemetry_sdk::{
17-
logs::{self as sdklogs},
18-
Resource,
12+
logs::SdkLoggerProvider, metrics::SdkMeterProvider, trace::SdkTracerProvider,
1913
};
2014
use std::error::Error;
2115
use tracing::info;
@@ -24,52 +18,52 @@ use tracing_subscriber::EnvFilter;
2418

2519
static RESOURCE: Lazy<Resource> = Lazy::new(|| {
2620
Resource::builder()
27-
.with_service_name("basic-otlp-example")
21+
.with_service_name("basic-otlp-example-http")
2822
.build()
2923
});
3024

31-
fn init_logs() -> Result<sdklogs::SdkLoggerProvider, opentelemetry_sdk::logs::LogError> {
25+
fn init_logs() -> SdkLoggerProvider {
3226
let exporter = LogExporter::builder()
3327
.with_http()
34-
.with_endpoint("http://localhost:4318/v1/logs")
3528
.with_protocol(Protocol::HttpBinary)
36-
.build()?;
29+
.build()
30+
.expect("Failed to create log exporter");
3731

38-
Ok(SdkLoggerProvider::builder()
32+
SdkLoggerProvider::builder()
3933
.with_batch_exporter(exporter)
4034
.with_resource(RESOURCE.clone())
41-
.build())
35+
.build()
4236
}
4337

44-
fn init_traces() -> Result<sdktrace::SdkTracerProvider, TraceError> {
38+
fn init_traces() -> SdkTracerProvider {
4539
let exporter = SpanExporter::builder()
4640
.with_http()
4741
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
48-
.with_endpoint("http://localhost:4318/v1/traces")
49-
.build()?;
42+
.build()
43+
.expect("Failed to create trace exporter");
5044

51-
Ok(SdkTracerProvider::builder()
45+
SdkTracerProvider::builder()
5246
.with_batch_exporter(exporter)
5347
.with_resource(RESOURCE.clone())
54-
.build())
48+
.build()
5549
}
5650

57-
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricError> {
51+
fn init_metrics() -> SdkMeterProvider {
5852
let exporter = MetricExporter::builder()
5953
.with_http()
6054
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
61-
.with_endpoint("http://localhost:4318/v1/metrics")
62-
.build()?;
55+
.build()
56+
.expect("Failed to create metric exporter");
6357

64-
Ok(SdkMeterProvider::builder()
58+
SdkMeterProvider::builder()
6559
.with_periodic_exporter(exporter)
6660
.with_resource(RESOURCE.clone())
67-
.build())
61+
.build()
6862
}
6963

7064
#[tokio::main]
7165
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
72-
let logger_provider = init_logs()?;
66+
let logger_provider = init_logs();
7367

7468
// Create a new OpenTelemetryTracingBridge using the above LoggerProvider.
7569
let otel_layer = OpenTelemetryTracingBridge::new(&logger_provider);
@@ -107,10 +101,25 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
107101
.with(fmt_layer)
108102
.init();
109103

110-
let tracer_provider = init_traces()?;
104+
// At this point Logs (OTel Logs and Fmt Logs) are initialized, which will
105+
// allow internal-logs from Tracing/Metrics initializer to be captured.
106+
107+
let tracer_provider = init_traces();
108+
// Set the global tracer provider using a clone of the tracer_provider.
109+
// Setting global tracer provider is required if other parts of the application
110+
// uses global::tracer() or global::tracer_with_version() to get a tracer.
111+
// Cloning simply creates a new reference to the same tracer provider. It is
112+
// important to hold on to the tracer_provider here, so as to invoke
113+
// shutdown on it when application ends.
111114
global::set_tracer_provider(tracer_provider.clone());
112115

113-
let meter_provider = init_metrics()?;
116+
let meter_provider = init_metrics();
117+
// Set the global meter provider using a clone of the meter_provider.
118+
// Setting global meter provider is required if other parts of the application
119+
// uses global::meter() or global::meter_with_version() to get a meter.
120+
// Cloning simply creates a new reference to the same meter provider. It is
121+
// important to hold on to the meter_provider here, so as to invoke
122+
// shutdown on it when application ends.
114123
global::set_meter_provider(meter_provider.clone());
115124

116125
let common_scope_attributes = vec![KeyValue::new("scope-key", "scope-value")];
@@ -152,8 +161,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
152161
info!(target: "my-target", "hello from {}. My price is {}", "apple", 1.99);
153162

154163
tracer_provider.shutdown()?;
155-
logger_provider.shutdown()?;
156164
meter_provider.shutdown()?;
165+
logger_provider.shutdown()?;
157166

158167
Ok(())
159168
}

opentelemetry-otlp/examples/basic-otlp/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ opentelemetry = { path = "../../../opentelemetry" }
1111
opentelemetry_sdk = { path = "../../../opentelemetry-sdk" }
1212
opentelemetry-otlp = { path = "../../../opentelemetry-otlp", features = ["grpc-tonic"] }
1313
tokio = { version = "1.0", features = ["full"] }
14-
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}
14+
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing"}
1515
tracing = { workspace = true, features = ["std"]}
1616
tracing-subscriber = { workspace = true, features = ["env-filter","registry", "std", "fmt"] }

0 commit comments

Comments
 (0)