|
| 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)`. For Metrics, replace `let reader = |
| 58 | +PeriodicReader::builder(exporter, runtime::Tokio).build();` with `let reader = |
| 59 | +PeriodicReader::builder(exporter).build();` or more conveniently, |
| 60 | +`.with_periodic_exporter(exporter)`. |
| 61 | + |
| 62 | +Please note the following: |
| 63 | + |
| 64 | +1. With the new approach, only the following grpc/http clients are supported in |
| 65 | + `opentelemetry-otlp`. grpc-tonic, reqwest-blocking-client In other words, |
| 66 | + `reqwest` and `hyper` are not supported. If using `grpc-tonic`, the OTLP |
| 67 | + Exporter must be created from within a Tokio runtime. |
| 68 | +2. Timeout enforcement is now moved to Exporters. i.e |
| 69 | +BatchProcessor,PeriodicReader does not enforce timeouts. For logs and traces, |
| 70 | +`max_export_timeout` (on Processors) or `OTEL_BLRP_EXPORT_TIMEOUT` or |
| 71 | +`OTEL_BSP_EXPORT_TIMEOUT` is no longer supported. For metrics, `with_timeout` on |
| 72 | +PeriodicReader is no longer supported. |
| 73 | + |
| 74 | +`OTEL_EXPORTER_OTLP_TIMEOUT` can be used to setup timeout for OTLP Exporters via |
| 75 | +environment variables, or `.with_tonic().with_timeout()` or |
| 76 | +`.with_http().with_timeout()` programmatically. |
| 77 | + |
| 78 | +If you need the old behavior (your application cannot spawn a new thread, or |
| 79 | +need to use another networking client etc.) use appropriate feature flags from |
| 80 | + below “experimental_metrics_periodicreader_with_async_runtime” |
| 81 | + "experimental_logs_batch_log_processor_with_async_runtime" |
| 82 | + "experimental_trace_batch_span_processor_with_async_runtime" |
| 83 | + |
| 84 | + **and** adjust the namespace: |
| 85 | + |
| 86 | + Example, when using Tokio runtime. |
| 87 | + |
| 88 | + ```rust |
| 89 | +let reader = opentelemetry_sdk::metrics::periodic_reader_with_async_runtime::PeriodicReader::builder(exporter, runtime::Tokio).build(); |
| 90 | +let tracer_provider = SdkTracerProvider::builder() |
| 91 | + .with_span_processor(span_processor_with_async_runtime::BatchSpanProcessor::builder(exporter, runtime::Tokio).build()) |
| 92 | + .build(); |
| 93 | +let logger_provider = SdkLoggerProvider::builder() |
| 94 | +.with_log_processor(log_processor_with_async_runtime::BatchLogProcessor::builder(exporter, runtime::Tokio).build()) |
| 95 | +.build(); |
| 96 | +``` |
| 97 | + |
| 98 | +## OTLP Default change |
| 99 | + |
| 100 | +"grpc-tonic" feature flag is no longer enabled by default. "http-proto" and |
| 101 | + "reqwest-blocking-client" features are added as default, to align with the |
| 102 | + OTel specification. |
| 103 | + |
| 104 | +## Resource Changes |
| 105 | + |
| 106 | +`Resource` creation is moved to a builder pattern, and `Resource::{new, empty, |
| 107 | +from_detectors, new_with_defaults, from_schema_url, merge, default}` are |
| 108 | +replaced with `Resource::builder()`. |
| 109 | + |
| 110 | +Before: |
| 111 | + |
| 112 | +```rust |
| 113 | +Resource::default().with_attributes([ |
| 114 | + KeyValue::new("service.name", "test_service"), |
| 115 | + KeyValue::new("key", "value"), |
| 116 | +]); |
| 117 | +``` |
| 118 | + |
| 119 | +After: |
| 120 | + |
| 121 | +```rust |
| 122 | +Resource::builder() |
| 123 | + .with_service_name("test_service") |
| 124 | + .with_attribute(KeyValue::new("key", "value")) |
| 125 | + .build(); |
| 126 | +``` |
| 127 | + |
| 128 | +## Improved internal logging |
| 129 | + |
| 130 | +OpenTelemetry internally used `tracing` to emit its internal logs. This is under |
| 131 | +feature-flag "internal-logs" that is enabled by default in all crates. When |
| 132 | +using OTel Logging, care must be taken to avoid OTel's own internal log being |
| 133 | +fed back to OTel, creating an infinite loop. This can be achieved via proper |
| 134 | +filtering. The OTLP Examples in the repo shows how to achieve this. It also |
| 135 | +shows how to send OTel's internal logs to stdtout using `tracing::Fmt`. |
| 136 | + |
| 137 | + |
| 138 | +## Full example |
| 139 | + |
| 140 | +A fully runnable example application using OTLP Exporter is provided in this |
| 141 | +repo. Comparing the 0.27 vs 0.28 of the example would give a good overview of |
| 142 | +the changes required to be made. |
| 143 | + |
| 144 | +[Basic OTLP Example |
| 145 | +(0.27)](https://github.com/open-telemetry/opentelemetry-rust/tree/opentelemetry-otlp-0.27.0/opentelemetry-otlp/examples) |
| 146 | +[Basic OTLP Example |
| 147 | +(0.28)](https://github.com/open-telemetry/opentelemetry-rust/tree/opentelemetry-otlp-0.28.0/opentelemetry-otlp/examples) |
| 148 | + |
| 149 | +This guide covers only the most common breaking changes. If you’re using custom |
| 150 | +exporters or processors (or authoring one), please consult the changelog for |
| 151 | +additional migration details. |
| 152 | + |
0 commit comments