opentelemetry_spanprocessor_any/
lib.rs

1//! OpenTelemetry provides a single set of APIs, libraries, agents, and collector
2//! services to capture distributed traces and metrics from your application. You
3//! can analyze them using [Prometheus], [Jaeger], and other observability tools.
4//!
5//! *Compiler support: [requires `rustc` 1.46+][msrv]*
6//!
7//! [Prometheus]: https://prometheus.io
8//! [Jaeger]: https://www.jaegertracing.io
9//! [msrv]: #supported-rust-versions
10//!
11//! # Getting Started
12//!
13//! ```no_run
14//! # #[cfg(feature = "trace")]
15//! # {
16//! use opentelemetry::{global, sdk::export::trace::stdout, trace::Tracer};
17//!
18//! fn main() {
19//!     // Create a new trace pipeline that prints to stdout
20//!     let tracer = stdout::new_pipeline().install_simple();
21//!
22//!     tracer.in_span("doing_work", |cx| {
23//!         // Traced app logic here...
24//!     });
25//!
26//!     // Shutdown trace pipeline
27//!     global::shutdown_tracer_provider();
28//! }
29//! # }
30//! ```
31//!
32//! See the [examples] directory for different integration patterns.
33//!
34//! [examples]: https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples
35//!
36//! # Traces
37//!
38//! The [`trace`] module includes types for tracking the progression of a single
39//! request while it is handled by services that make up an application. A trace
40//! is a tree of [`Span`]s which are objects that represent the work being done
41//! by individual services or components involved in a request as it flows
42//! through a system.
43//!
44//! ### Creating and exporting spans
45//!
46//! ```
47//! # #[cfg(feature = "trace")]
48//! # {
49//! use opentelemetry::{global, trace::{Span, Tracer}, KeyValue};
50//!
51//! // get a tracer from a provider
52//! let tracer = global::tracer("my_service");
53//!
54//! // start a new span
55//! let mut span = tracer.start("my_span");
56//!
57//! // set some attributes
58//! span.set_attribute(KeyValue::new("http.client_ip", "83.164.160.102"));
59//!
60//! // perform some more work...
61//!
62//! // end or drop the span to export
63//! span.end();
64//! # }
65//! ```
66//!
67//! See the [`trace`] module docs for more information on creating and managing
68//! spans.
69//!
70//! [`Span`]: crate::trace::Span
71//!
72//! # Metrics
73//!
74//! Note: the metrics specification is **still in progress** and **subject to major
75//! changes**.
76//!
77//! The [`metrics`] module includes types for recording measurements about a
78//! service at runtime.
79//!
80//! ### Creating instruments and recording measurements
81//!
82//! ```
83//! # #[cfg(feature = "metrics")]
84//! # {
85//! use opentelemetry::{global, KeyValue};
86//!
87//! // get a meter from a provider
88//! let meter = global::meter("my_service");
89//!
90//! // create an instrument
91//! let counter = meter.u64_counter("my_counter").init();
92//!
93//! // record a measurement
94//! counter.add(1, &[KeyValue::new("http.client_ip", "83.164.160.102")]);
95//! # }
96//! ```
97//!
98//! See the [`metrics`] module docs for more information on creating and
99//! managing instruments.
100//!
101//! ## Crate Feature Flags
102//!
103//! The following core crate feature flags are available:
104//!
105//! * `trace`: Includes the trace API and SDK (enabled by default).
106//! * `metrics`: Includes the unstable metrics API and SDK.
107//! * `serialize`: Adds [serde] serializers for common types.
108//!
109//! Support for recording and exporting telemetry asynchronously can be added
110//! via the following flags:
111//!
112//! * `rt-tokio`: Spawn telemetry tasks using [tokio]'s multi-thread runtime.
113//! * `rt-tokio-current-thread`: Spawn telemetry tasks on a separate runtime so that the main runtime won't be blocked.
114//! * `rt-async-std`: Spawn telemetry tasks using [async-std]'s runtime.
115//!
116//! [tokio]: https://crates.io/crates/tokio
117//! [async-std]: https://crates.io/crates/async-std
118//! [serde]: https://crates.io/crates/serde
119//!
120//! ## Related Crates
121//!
122//! In addition to `opentelemetry`, the [`open-telemetry/opentelemetry-rust`]
123//! repository contains several additional crates designed to be used with the
124//! `opentelemetry` ecosystem. This includes a collection of trace
125//! `SpanExporter` and metrics pull and push controller implementations, as well
126//! as utility and adapter crates to assist in propagating state and
127//! instrumenting applications.
128//!
129//! In particular, the following crates are likely to be of interest:
130//!
131//! - [`opentelemetry-http`] provides an interface for injecting and extracting
132//!   trace information from [`http`] headers.
133//! - [`opentelemetry-jaeger`] provides a pipeline and exporter for sending
134//!   trace information to [`Jaeger`].
135//! - [`opentelemetry-otlp`] exporter for sending trace and metric data in the
136//!   OTLP format to the OpenTelemetry collector.
137//! - [`opentelemetry-prometheus`] provides a pipeline and exporter for sending
138//!   metrics information to [`Prometheus`].
139//! - [`opentelemetry-zipkin`] provides a pipeline and exporter for sending
140//!   trace information to [`Zipkin`].
141//! - [`opentelemetry-datadog`] provides additional exporters to [`Datadog`].
142//! - [`opentelemetry-aws`] provides unofficial propagators for AWS X-ray.
143//! - [`opentelemetry-contrib`] provides additional exporters and propagators that are
144//!   experimental.
145//! - [`opentelemetry-semantic-conventions`] provides standard names and
146//!   semantic otel conventions.
147//! - [`opentelemetry-stackdriver`] provides an exporter for Google's [Cloud Trace]
148//!   (which used to be called StackDriver).
149//!
150//! Additionally, there are also several third-party crates which are not
151//! maintained by the `opentelemetry` project. These include:
152//!
153//! - [`tracing-opentelemetry`] provides integration for applications
154//!   instrumented using the [`tracing`] API and ecosystem.
155//! - [`actix-web-opentelemetry`] provides integration for the [`actix-web`] web
156//!   server and ecosystem.
157//! - [`opentelemetry-application-insights`] provides an unofficial [Azure
158//!   Application Insights] exporter.
159//! - [`opentelemetry-tide`] provides integration for the [`Tide`] web server
160//!   and ecosystem.
161//!
162//! If you're the maintainer of an `opentelemetry` ecosystem crate not listed
163//! above, please let us know! We'd love to add your project to the list!
164//!
165//! [`open-telemetry/opentelemetry-rust`]: https://github.com/open-telemetry/opentelemetry-rust
166//! [`opentelemetry-jaeger`]: https://crates.io/crates/opentelemetry-jaeger
167//! [`Jaeger`]: https://www.jaegertracing.io
168//! [`opentelemetry-otlp`]: https://crates.io/crates/opentelemetry-otlp
169//! [`opentelemetry-http`]: https://crates.io/crates/opentelemetry-http
170//! [`opentelemetry-prometheus`]: https://crates.io/crates/opentelemetry-prometheus
171//! [`opentelemetry-aws`]: https://crates.io/crates/opentelemetry-aws
172//! [`Prometheus`]: https://prometheus.io
173//! [`opentelemetry-zipkin`]: https://crates.io/crates/opentelemetry-zipkin
174//! [`http`]: https://crates.io/crates/http
175//! [`Zipkin`]: https://zipkin.io
176//! [`opentelemetry-contrib`]: https://crates.io/crates/opentelemetry-contrib
177//! [`opentelemetry-datadog`]: https://crates.io/crates/opentelemetry-datadog
178//! [`Datadog`]: https://www.datadoghq.com
179//! [`opentelemetry-semantic-conventions`]: https://crates.io/crates/opentelemetry-semantic-conventions
180//!
181//! [`tracing-opentelemetry`]: https://crates.io/crates/tracing-opentelemetry
182//! [`tracing`]: https://crates.io/crates/tracing
183//! [`actix-web-opentelemetry`]: https://crates.io/crates/actix-web-opentelemetry
184//! [`actix-web`]: https://crates.io/crates/actix-web
185//! [`opentelemetry-application-insights`]: https://crates.io/crates/opentelemetry-application-insights
186//! [Azure Application Insights]: https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview
187//! [`opentelemetry-tide`]: https://crates.io/crates/opentelemetry-tide
188//! [`Tide`]: https://crates.io/crates/tide
189//! [`opentelemetry-stackdriver`]: https://crates.io/crates/opentelemetry-stackdriver
190//! [Cloud Trace]: https://cloud.google.com/trace/
191//!
192//! ## Supported Rust Versions
193//!
194//! OpenTelemetry is built against the latest stable release. The minimum
195//! supported version is 1.46. The current OpenTelemetry version is not
196//! guaranteed to build on Rust versions earlier than the minimum supported
197//! version.
198//!
199//! The current stable Rust compiler and the three most recent minor versions
200//! before it will always be supported. For example, if the current stable
201//! compiler version is 1.49, the minimum supported version will not be
202//! increased past 1.46, three minor versions prior. Increasing the minimum
203//! supported compiler version is not considered a semver breaking change as
204//! long as doing so complies with this policy.
205#![warn(
206    future_incompatible,
207    missing_debug_implementations,
208    missing_docs,
209    nonstandard_style,
210    rust_2018_idioms,
211    unreachable_pub,
212    unused
213)]
214#![allow(clippy::needless_doctest_main)]
215#![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::broken_intra_doc_links))]
216#![doc(
217    html_logo_url = "https://raw.githubusercontent.com/open-telemetry/opentelemetry-rust/main/assets/logo.svg"
218)]
219#![cfg_attr(test, deny(warnings))]
220
221pub mod global;
222pub mod sdk;
223
224#[cfg(feature = "testing")]
225#[doc(hidden)]
226pub mod testing;
227
228pub mod baggage;
229
230mod context;
231
232pub use context::{Context, ContextGuard};
233
234mod common;
235
236pub use common::{Array, Key, KeyValue, Value};
237
238pub mod runtime;
239
240#[doc(hidden)]
241pub mod util;
242
243#[cfg(feature = "metrics")]
244#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
245pub mod attributes;
246
247#[cfg(feature = "metrics")]
248#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
249pub mod metrics;
250
251pub mod propagation;
252
253#[cfg(feature = "trace")]
254#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
255pub mod trace;
256
257#[cfg(any(feature = "metrics", feature = "trace"))]
258pub(crate) mod time {
259    use std::time::SystemTime;
260
261    #[cfg(not(target_arch = "wasm32"))]
262    pub(crate) fn now() -> SystemTime {
263        SystemTime::now()
264    }
265
266    #[cfg(target_arch = "wasm32")]
267    pub(crate) fn now() -> SystemTime {
268        SystemTime::UNIX_EPOCH + std::time::Duration::from_millis(js_sys::Date::now() as u64)
269    }
270}