pub struct PrometheusLayer { /* private fields */ }Expand description
PrometheusLayer records OpenDAL operation and HTTP fetch metrics with
prometheus.
§Prometheus Metrics
This layer registers operation metrics and HTTP fetch metrics. Please see the documentation of observe module for metric details.
For a more detailed explanation of these metrics and how they are used, please refer to the Prometheus documentation.
§Examples
§Basic Usage
let registry = prometheus::default_registry();
let op = Operator::new(services::Memory::default())?
.layer(
PrometheusLayer::builder()
.register(registry)
.expect("register metrics successfully"),
);
// Write data into object test.
op.write("test", "Hello, World!").await?;
// Read data from the object.
let bs = op.read("test").await?;
info!("content: {}", String::from_utf8_lossy(&bs.to_bytes()));
// Get object metadata.
let meta = op.stat("test").await?;
info!("meta: {:?}", meta);
// Export prometheus metrics.
let mut buffer = Vec::<u8>::new();
let encoder = prometheus::TextEncoder::new();
encoder.encode(&prometheus::gather(), &mut buffer).unwrap();
println!("## Prometheus Metrics");
println!("{}", String::from_utf8(buffer.clone()).unwrap());§Global Instance
PrometheusLayer needs to be registered before instantiation.
If there are multiple operators in an application that need the PrometheusLayer, we could
instantiate it once and pass it to multiple operators. But we cannot directly call
.layer(PrometheusLayer::builder().register(®istry)?) for different services, because
registering the same metrics multiple times to the same registry will cause register errors.
Therefore, we can provide a global instance for the PrometheusLayer.
fn global_prometheus_layer() -> &'static PrometheusLayer {
static GLOBAL: OnceLock<PrometheusLayer> = OnceLock::new();
GLOBAL.get_or_init(|| {
PrometheusLayer::builder()
.register_default()
.expect("Failed to register with the global registry")
})
}
let op = Operator::new(services::Memory::default())?
.layer(global_prometheus_layer().clone());
// Write data into object test.
op.write("test", "Hello, World!").await?;
// Read data from the object.
let bs = op.read("test").await?;
info!("content: {}", String::from_utf8_lossy(&bs.to_bytes()));
// Get object metadata.
let meta = op.stat("test").await?;
info!("meta: {:?}", meta);
// Export prometheus metrics.
let mut buffer = Vec::<u8>::new();
let encoder = prometheus::TextEncoder::new();
encoder.encode(&prometheus::gather(), &mut buffer).unwrap();
println!("## Prometheus Metrics");
println!("{}", String::from_utf8(buffer.clone()).unwrap());Implementations§
Source§impl PrometheusLayer
impl PrometheusLayer
Sourcepub fn builder() -> PrometheusLayerBuilder
pub fn builder() -> PrometheusLayerBuilder
Create a PrometheusLayerBuilder to set the configuration of metrics.
§Example
// Pick a builder and configure it.
let builder = services::Memory::default();
let registry = prometheus::default_registry();
let duration_seconds_buckets = prometheus::exponential_buckets(0.01, 2.0, 16).unwrap();
let bytes_buckets = prometheus::exponential_buckets(1.0, 2.0, 16).unwrap();
let _ = Operator::new(builder)?
.layer(
PrometheusLayer::builder()
.duration_seconds_buckets(duration_seconds_buckets)
.bytes_buckets(bytes_buckets)
.register(registry)
.expect("register metrics successfully"),
);Trait Implementations§
Source§impl Clone for PrometheusLayer
impl Clone for PrometheusLayer
Source§fn clone(&self) -> PrometheusLayer
fn clone(&self) -> PrometheusLayer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more