opentelemetry_instrumentation_process/
lib.rs

1#![doc = include_str!("../README.md")]
2#![deny(clippy::all, clippy::pedantic)]
3
4use std::sync::LazyLock;
5
6use opentelemetry::InstrumentationScope;
7use opentelemetry::metrics::Meter;
8
9#[allow(dead_code, reason = "Might be unused on unsupported platforms")]
10static METER: LazyLock<Meter> = LazyLock::new(|| {
11    let scope = InstrumentationScope::builder(env!("CARGO_PKG_NAME"))
12        .with_version(env!("CARGO_PKG_VERSION"))
13        .with_schema_url(opentelemetry_semantic_conventions::SCHEMA_URL)
14        .build();
15
16    opentelemetry::global::meter_with_scope(scope)
17});
18
19#[cfg(target_os = "linux")]
20mod linux;
21
22/// The error returned when process metrics initialisation fails.
23#[derive(Debug)]
24pub struct InitError {
25    source: Box<dyn std::error::Error + Send + Sync + 'static>,
26}
27
28impl std::fmt::Display for InitError {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        write!(f, "Could not initialise process metrics")
31    }
32}
33
34impl std::error::Error for InitError {
35    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36        Some(self.source.as_ref())
37    }
38}
39
40/// Initialise process metrics collection.
41///
42/// # Errors
43///
44/// Returns an error if initialisation fails.
45pub fn init() -> Result<(), InitError> {
46    #[cfg(target_os = "linux")]
47    self::linux::init().map_err(|source| InitError { source })?;
48
49    Ok(())
50}