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(any(feature = "trace", feature = "metrics"))]
15mod conventions;
16#[cfg(all(feature = "trace", feature = "metrics"))]
17mod instrument;
18#[cfg(feature = "metrics")]
19mod metrics;
20#[cfg(feature = "trace")]
21mod trace;
22
23#[cfg(feature = "trace")]
24mod instrument_handler;
25
26#[cfg(all(feature = "trace", feature = "metrics"))]
27pub use instrument::{Instrument, instrument};
28#[cfg(feature = "trace")]
29pub use instrument_handler::{InstrumentHandler, instrument_handler};
30#[cfg(feature = "metrics")]
31pub use metrics::{Metrics, metrics};
32#[cfg(any(feature = "trace", feature = "metrics"))]
33use opentelemetry::InstrumentationScope;
34#[cfg(any(feature = "trace", feature = "metrics"))]
35use opentelemetry_semantic_conventions::SCHEMA_URL;
36#[cfg(feature = "trace")]
37pub use trace::{Trace, trace};
38
39/// instrumentation using [`opentelemetry::global`]
40pub mod global {
41    #[cfg(any(feature = "trace", feature = "metrics"))]
42    use super::instrumentation_scope;
43
44    #[cfg(all(feature = "trace", feature = "metrics"))]
45    pub use super::instrument::instrument_global as instrument;
46
47    #[cfg(feature = "trace")]
48    pub use super::instrument_handler::instrument_handler_global as instrument_handler;
49
50    #[cfg(feature = "trace")]
51    ///configure a [`Trace`](crate::trace::Trace) against the global tracer provider
52    pub fn trace() -> super::Trace<opentelemetry::global::BoxedTracer> {
53        super::Trace::new(opentelemetry::global::tracer_with_scope(
54            instrumentation_scope(),
55        ))
56    }
57
58    #[cfg(feature = "metrics")]
59    /// configure a [`Metrics`](crate::metrics::Metrics) against the global meter provider
60    pub fn metrics() -> super::Metrics {
61        opentelemetry::global::meter_provider()
62            .meter_with_scope(instrumentation_scope())
63            .into()
64    }
65}
66
67#[cfg(any(feature = "trace", feature = "metrics"))]
68fn instrumentation_scope() -> InstrumentationScope {
69    InstrumentationScope::builder("trillium-opentelemetry")
70        .with_version(env!("CARGO_PKG_VERSION"))
71        .with_schema_url(SCHEMA_URL)
72        .build()
73}