Skip to main content

trillium_opentelemetry/
lib.rs

1//! This crate provides opentelemetry metrics conforming to
2//! [semantic conventions for http](https://opentelemetry.io/docs/reference/specification/metrics/semantic_conventions/http-metrics/).
3#![forbid(unsafe_code)]
4#![deny(
5    missing_copy_implementations,
6    rustdoc::missing_crate_level_docs,
7    missing_debug_implementations,
8    nonstandard_style,
9    unused_qualifications,
10    missing_docs
11)]
12pub use opentelemetry;
13
14#[cfg(all(feature = "trace", feature = "metrics"))]
15mod instrument;
16#[cfg(feature = "metrics")]
17mod metrics;
18#[cfg(feature = "trace")]
19mod trace;
20
21#[cfg(feature = "trace")]
22mod instrument_handler;
23
24#[cfg(all(feature = "trace", feature = "metrics"))]
25pub use instrument::{Instrument, instrument};
26#[cfg(feature = "trace")]
27pub use instrument_handler::{InstrumentHandler, instrument_handler};
28#[cfg(feature = "metrics")]
29pub use metrics::{Metrics, metrics};
30#[cfg(any(feature = "trace", feature = "metrics"))]
31use opentelemetry::InstrumentationScope;
32#[cfg(any(feature = "trace", feature = "metrics"))]
33use opentelemetry_semantic_conventions::SCHEMA_URL;
34#[cfg(feature = "trace")]
35pub use trace::{Trace, trace};
36
37/// instrumentation using [`opentelemetry::global`]
38pub mod global {
39    #[cfg(any(feature = "trace", feature = "metrics"))]
40    use super::instrumentation_scope;
41
42    #[cfg(all(feature = "trace", feature = "metrics"))]
43    pub use super::instrument::instrument_global as instrument;
44
45    #[cfg(feature = "trace")]
46    pub use super::instrument_handler::instrument_handler_global as instrument_handler;
47
48    #[cfg(feature = "trace")]
49    ///configure a [`Trace`](crate::trace::Trace) against the global tracer provider
50    pub fn trace() -> super::Trace<opentelemetry::global::BoxedTracer> {
51        super::Trace::new(opentelemetry::global::tracer_with_scope(
52            instrumentation_scope(),
53        ))
54    }
55
56    #[cfg(feature = "metrics")]
57    /// configure a [`Metrics`](crate::metrics::Metrics) against the global meter provider
58    pub fn metrics() -> super::Metrics {
59        opentelemetry::global::meter_provider()
60            .meter_with_scope(instrumentation_scope())
61            .into()
62    }
63}
64
65#[cfg(any(feature = "trace", feature = "metrics"))]
66fn instrumentation_scope() -> InstrumentationScope {
67    InstrumentationScope::builder("trillium-opentelemetry")
68        .with_version(env!("CARGO_PKG_VERSION"))
69        .with_schema_url(SCHEMA_URL)
70        .build()
71}