• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! # Tracing OpenTelemetry
2 //!
3 //! [`tracing`] is a framework for instrumenting Rust programs to collect
4 //! structured, event-based diagnostic information. This crate provides a layer
5 //! that connects spans from multiple systems into a trace and emits them to
6 //! [OpenTelemetry]-compatible distributed tracing systems for processing and
7 //! visualization.
8 //!
9 //! [OpenTelemetry]: https://opentelemetry.io
10 //! [`tracing`]: https://github.com/tokio-rs/tracing
11 //!
12 //! *Compiler support: [requires `rustc` 1.56+][msrv]*
13 //!
14 //! [msrv]: #supported-rust-versions
15 //!
16 //! ### Special Fields
17 //!
18 //! Fields with an `otel.` prefix are reserved for this crate and have specific
19 //! meaning. They are treated as ordinary fields by other layers. The current
20 //! special fields are:
21 //!
22 //! * `otel.name`: Override the span name sent to OpenTelemetry exporters.
23 //! Setting this field is useful if you want to display non-static information
24 //! in your span name.
25 //! * `otel.kind`: Set the span kind to one of the supported OpenTelemetry [span kinds].
26 //! * `otel.status_code`: Set the span status code to one of the supported OpenTelemetry [span status codes].
27 //! * `otel.status_message`: Set the span status message.
28 //!
29 //! [span kinds]: opentelemetry::trace::SpanKind
30 //! [span status codes]: opentelemetry::trace::StatusCode
31 //!
32 //! ### Semantic Conventions
33 //!
34 //! OpenTelemetry defines conventional names for attributes of common
35 //! operations. These names can be assigned directly as fields, e.g.
36 //! `trace_span!("request", "otel.kind" = %SpanKind::Client, "http.url" = ..)`, and they
37 //! will be passed through to your configured OpenTelemetry exporter. You can
38 //! find the full list of the operations and their expected field names in the
39 //! [semantic conventions] spec.
40 //!
41 //! [semantic conventions]: https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions
42 //!
43 //! ### Stability Status
44 //!
45 //! The OpenTelemetry specification is currently in beta so some breaking
46 //! changes may still occur on the path to 1.0. You can follow the changes via
47 //! the [spec repository] to track progress toward stabilization.
48 //!
49 //! [spec repository]: https://github.com/open-telemetry/opentelemetry-specification
50 //!
51 //! ## Examples
52 //!
53 //! ```
54 //! use opentelemetry::sdk::export::trace::stdout;
55 //! use tracing::{error, span};
56 //! use tracing_subscriber::layer::SubscriberExt;
57 //! use tracing_subscriber::Registry;
58 //!
59 //! // Create a new OpenTelemetry pipeline
60 //! let tracer = stdout::new_pipeline().install_simple();
61 //!
62 //! // Create a tracing layer with the configured tracer
63 //! let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
64 //!
65 //! // Use the tracing subscriber `Registry`, or any other subscriber
66 //! // that impls `LookupSpan`
67 //! let subscriber = Registry::default().with(telemetry);
68 //!
69 //! // Trace executed code
70 //! tracing::subscriber::with_default(subscriber, || {
71 //!     // Spans will be sent to the configured OpenTelemetry exporter
72 //!     let root = span!(tracing::Level::TRACE, "app_start", work_units = 2);
73 //!     let _enter = root.enter();
74 //!
75 //!     error!("This event will be logged in the root span.");
76 //! });
77 //! ```
78 //!
79 //! ## Feature Flags
80 //!
81 //! - `metrics`: Enables the [`MetricsSubscriber`] type, a [subscriber] that
82 //!   exports OpenTelemetry metrics from specifically-named events. This enables
83 //!   the `metrics` feature flag on the `opentelemetry` crate.  *Enabled by
84 //!   default*.
85 //!
86 //! ## Supported Rust Versions
87 //!
88 //! Tracing is built against the latest stable release. The minimum supported
89 //! version is 1.56. The current Tracing version is not guaranteed to build on
90 //! Rust versions earlier than the minimum supported version.
91 //!
92 //! Tracing follows the same compiler support policies as the rest of the Tokio
93 //! project. The current stable Rust compiler and the three most recent minor
94 //! versions before it will always be supported. For example, if the current
95 //! stable compiler version is 1.45, the minimum supported version will not be
96 //! increased past 1.42, three minor versions prior. Increasing the minimum
97 //! supported compiler version is not considered a semver breaking change as
98 //! long as doing so complies with this policy.
99 //!
100 //! [subscriber]: tracing_subscriber::subscribe
101 #![deny(unreachable_pub)]
102 #![cfg_attr(test, deny(warnings))]
103 #![doc(html_root_url = "https://docs.rs/tracing-opentelemetry/0.18.0")]
104 #![doc(
105     html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png",
106     issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/"
107 )]
108 #![cfg_attr(
109     docsrs,
110     // Allows displaying cfgs/feature flags in the documentation.
111     feature(doc_cfg, doc_auto_cfg),
112     // Allows adding traits to RustDoc's list of "notable traits"
113     feature(doc_notable_trait),
114     // Fail the docs build if any intra-docs links are broken
115     deny(rustdoc::broken_intra_doc_links),
116 )]
117 
118 /// Implementation of the trace::Subscriber trait; publishes OpenTelemetry metrics.
119 #[cfg(feature = "metrics")]
120 mod metrics;
121 
122 /// Implementation of the trace::Layer as a source of OpenTelemetry data.
123 mod layer;
124 /// Span extension which enables OpenTelemetry context management.
125 mod span_ext;
126 /// Protocols for OpenTelemetry Tracers that are compatible with Tracing
127 mod tracer;
128 
129 pub use layer::{layer, OpenTelemetryLayer};
130 
131 #[cfg(feature = "metrics")]
132 pub use metrics::MetricsLayer;
133 pub use span_ext::OpenTelemetrySpanExt;
134 pub use tracer::PreSampledTracer;
135 
136 /// Per-span OpenTelemetry data tracked by this crate.
137 ///
138 /// Useful for implementing [PreSampledTracer] in alternate otel SDKs.
139 #[derive(Debug, Clone)]
140 pub struct OtelData {
141     /// The parent otel `Context` for the current tracing span.
142     pub parent_cx: opentelemetry::Context,
143 
144     /// The otel span data recorded during the current tracing span.
145     pub builder: opentelemetry::trace::SpanBuilder,
146 }
147