5 releases

new 0.1.0 Feb 10, 2025
0.0.3 Dec 8, 2024
0.0.2 Dec 2, 2024
0.0.1 Dec 1, 2024
0.0.0 Nov 28, 2024

#7 in #pprof

Download history 371/week @ 2024-11-27 175/week @ 2024-12-04 98/week @ 2024-12-11 75/week @ 2024-12-18 45/week @ 2024-12-25 153/week @ 2025-01-01 40/week @ 2025-01-08 9/week @ 2025-01-15 4/week @ 2025-01-22 99/week @ 2025-02-05

115 downloads per month
Used in scuffle-image-processor

MIT/Apache

190KB
4K SLoC

scuffle-bootstrap-telemetry

[!WARNING]
This crate is under active development and may not be stable.

crates.io docs.rs


A crate used to add telemetry to applications built with the scuffle-bootstrap.

Status

This crate is currently under development and is not yet stable, unit tests are not yet fully implemented.

Unit tests are not yet fully implemented. Use at your own risk.

License

This project is licensed under the MIT or Apache-2.0 license. You can choose between one of them if you use this work.

SPDX-License-Identifier: MIT OR Apache-2.0


lib.rs:

A crate used to add telemetry to applications built with the scuffle-bootstrap crate.

Emit metrics using the scuffle-metrics crate.

Feature Flags

  • prometheus: Enable Prometheus support.
  • pprof: Enable pprof support.
  • opentelemetry-metrics: Enable OpenTelemetry metrics support.
  • opentelemetry-traces: Enable OpenTelemetry traces support.
  • opentelemetry-logs: Enable OpenTelemetry logs support.

All features are enabled by default.

See TelemetrySvc for more details.

Example

use std::net::SocketAddr;
use std::sync::Arc;

use scuffle_bootstrap::global::GlobalWithoutConfig;
use scuffle_bootstrap_telemetry::{prometheus_client, opentelemetry, opentelemetry_sdk, TelemetryConfig, TelemetrySvc};

struct Global {
    prometheus: prometheus_client::registry::Registry,
    open_telemetry: opentelemetry::OpenTelemetry,
}

impl GlobalWithoutConfig for Global {
    async fn init() -> anyhow::Result<Arc<Self>> {
        // Initialize the Prometheus metrics registry.
        let mut prometheus = prometheus_client::registry::Registry::default();
        // The exporter converts opentelemetry metrics into the Prometheus format.
        let exporter = scuffle_metrics::prometheus::exporter().build();
        // Register the exporter as a data source for the Prometheus registry.
        prometheus.register_collector(exporter.collector());

        // Initialize the OpenTelemetry metrics provider and add the Prometheus exporter as a reader.
        let metrics = opentelemetry_sdk::metrics::SdkMeterProvider::builder().with_reader(exporter).build();
        opentelemetry::global::set_meter_provider(metrics.clone());

        // Initialize the OpenTelemetry configuration instance.
        let open_telemetry = opentelemetry::OpenTelemetry::new().with_metrics(metrics);

        Ok(Arc::new(Self {
            prometheus,
            open_telemetry,
        }))
    }
}

impl TelemetryConfig for Global {
    fn bind_address(&self) -> Option<SocketAddr> {
        // Tells the http server to bind to port 8080 on localhost.
        Some(SocketAddr::from(([127, 0, 0, 1], 8080)))
    }

    fn prometheus_metrics_registry(&self) -> Option<&prometheus_client::registry::Registry> {
        Some(&self.prometheus)
    }

    fn opentelemetry(&self) -> Option<&opentelemetry::OpenTelemetry> {
        Some(&self.open_telemetry)
    }
}

#[scuffle_metrics::metrics]
mod example {
    use scuffle_metrics::{CounterU64, MetricEnum};

    #[derive(MetricEnum)]
    pub enum Kind {
        Http,
        Grpc,
    }

    #[metrics(unit = "requests")]
    pub fn request(kind: Kind) -> CounterU64;
}

// Now emit metrics from anywhere in your code using the `example` module.
example::request(example::Kind::Http).incr();

scuffle_bootstrap::main! {
    Global {
        TelemetrySvc,
    }
};

Status

This crate is currently under development and is not yet stable, unit tests are not yet fully implemented.

Unit tests are not yet fully implemented. Use at your own risk.

License

This project is licensed under the MIT or Apache-2.0 license. You can choose between one of them if you use this work.

SPDX-License-Identifier: MIT OR Apache-2.0

Dependencies

~6–18MB
~252K SLoC