opentelemetry_sdk/
lib.rs

1//! Implements the [`SDK`] component of [OpenTelemetry].
2//!
3//! *[Supported Rust Versions](#supported-rust-versions)*
4//!
5//! [`SDK`]: https://opentelemetry.io/docs/specs/otel/overview/#sdk
6//! [OpenTelemetry]: https://opentelemetry.io/docs/what-is-opentelemetry/
7//! [msrv]: #supported-rust-versions
8//!
9//! # Getting Started
10//!
11//! ```no_run
12//! # #[cfg(feature = "trace")]
13//! # {
14//! use opentelemetry::{global, trace::{Tracer, TracerProvider}};
15//! use opentelemetry_sdk::trace::SdkTracerProvider;
16//!
17//! fn main() {
18//!     // Choose an exporter like `opentelemetry_stdout::SpanExporter`
19//!     # fn example<T: opentelemetry_sdk::trace::SpanExporter + 'static>(new_exporter: impl Fn() -> T) {
20//!     let exporter = new_exporter();
21//!
22//!     // Create a new trace pipeline that prints to stdout
23//!     let provider = SdkTracerProvider::builder()
24//!         .with_simple_exporter(exporter)
25//!         .build();
26//!     let tracer = provider.tracer("readme_example");
27//!
28//!     tracer.in_span("doing_work", |cx| {
29//!         // Traced app logic here...
30//!     });
31//!
32//!     // Shutdown trace pipeline
33//!     provider.shutdown().expect("TracerProvider should shutdown successfully")
34//!     # }
35//! }
36//! # }
37//! ```
38//!
39//! See the [examples] directory for different integration patterns.
40//!
41//! See the API [`trace`] module docs for more information on creating and managing
42//! spans.
43//!
44//! [examples]: https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples
45//! [`trace`]: https://docs.rs/opentelemetry/latest/opentelemetry/trace/index.html
46//!
47//! # Metrics
48//!
49//! ### Creating instruments and recording measurements
50//!
51//! ```
52//! # #[cfg(feature = "metrics")]
53//! # {
54//! use opentelemetry::{global, KeyValue};
55//!
56//! // get a meter from a provider
57//! let meter = global::meter("my_service");
58//!
59//! // create an instrument
60//! let counter = meter.u64_counter("my_counter").build();
61//!
62//! // record a measurement
63//! counter.add(1, &[KeyValue::new("http.client_ip", "83.164.160.102")]);
64//! # }
65//! ```
66//!
67//! See the [examples] directory for different integration patterns.
68//!
69//! See the API [`metrics`] module docs for more information on creating and
70//! managing instruments.
71//!
72//! [examples]: https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples
73//! [`metrics`]: https://docs.rs/opentelemetry/latest/opentelemetry/metrics/index.html
74//!
75//! ## Crate Feature Flags
76//!
77//! The following feature flags can used to control the telemetry signals to use:
78//!
79//! * `trace`: Includes the trace SDK (enabled by default).
80//! * `metrics`: Includes the metrics SDK.
81//! * `logs`: Includes the logs SDK.
82//!
83//! For `trace` the following feature flags are available:
84//!
85//! * `jaeger_remote_sampler`: Enables the [Jaeger remote sampler](https://www.jaegertracing.io/docs/1.53/sampling/).
86//!
87//! For `logs` the following feature flags are available:
88//!
89//! * `spec_unstable_logs_enabled`: control the log level
90//!
91//! Support for recording and exporting telemetry asynchronously and perform
92//! metrics aggregation can be added via the following flags:
93//!
94//! * `experimental_async_runtime`: Enables the experimental `Runtime` trait and related functionality.
95//! * `rt-tokio`: Spawn telemetry tasks using [tokio]'s multi-thread runtime.
96//! * `rt-tokio-current-thread`: Spawn telemetry tasks on a separate runtime so that the main runtime won't be blocked.
97//!
98//! [tokio]: https://crates.io/crates/tokio
99#![warn(
100    future_incompatible,
101    missing_debug_implementations,
102    missing_docs,
103    nonstandard_style,
104    rust_2018_idioms,
105    unreachable_pub,
106    unused
107)]
108#![allow(clippy::needless_doctest_main)]
109#![cfg_attr(
110    docsrs,
111    feature(doc_cfg, doc_auto_cfg),
112    deny(rustdoc::broken_intra_doc_links)
113)]
114#![doc(
115    html_logo_url = "https://raw.githubusercontent.com/open-telemetry/opentelemetry-rust/main/assets/logo.svg"
116)]
117#![cfg_attr(test, deny(warnings))]
118
119pub(crate) mod growable_array;
120
121#[cfg(feature = "logs")]
122#[cfg_attr(docsrs, doc(cfg(feature = "logs")))]
123pub mod logs;
124#[cfg(feature = "metrics")]
125#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
126pub mod metrics;
127#[cfg(feature = "trace")]
128#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
129pub mod propagation;
130pub mod resource;
131#[cfg(feature = "experimental_async_runtime")]
132pub mod runtime;
133#[cfg(any(feature = "testing", test))]
134#[cfg_attr(docsrs, doc(cfg(any(feature = "testing", test))))]
135pub mod testing;
136
137#[cfg(feature = "trace")]
138#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
139pub mod trace;
140
141#[doc(hidden)]
142pub mod util;
143
144#[doc(inline)]
145pub use resource::Resource;
146
147pub mod error;
148pub use error::ExportError;
149
150#[cfg(any(feature = "testing", test))]
151#[derive(thiserror::Error, Debug)]
152/// Errors that can occur during when returning telemetry from InMemoryLogExporter
153pub enum InMemoryExporterError {
154    /// Operation failed due to an internal error.
155    ///
156    /// The error message is intended for logging purposes only and should not
157    /// be used to make programmatic decisions. It is implementation-specific
158    /// and subject to change without notice. Consumers of this error should not
159    /// rely on its content beyond logging.
160    #[error("Unable to obtain telemetry. Reason: {0}")]
161    InternalFailure(String),
162}
163
164#[cfg(any(feature = "testing", test))]
165impl<T> From<std::sync::PoisonError<T>> for InMemoryExporterError {
166    fn from(err: std::sync::PoisonError<T>) -> Self {
167        InMemoryExporterError::InternalFailure(format!("Mutex poison error: {}", err))
168    }
169}