pdk_metrics_lib/lib.rs
1// Copyright (c) 2026, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5//! PDK Metrics Library
6//!
7//! USE AT OWN RISK: The metrics library is experimental and subject to change.
8//!
9//! Exposes interfaces to handle counters and gauges for custom policies.
10//!
11//! ## Highlights
12//!
13//! - Counters and gauges via [`MetricsBuilder`]
14//! - Default tags applied automatically: `source=custom-metrics` and `category=<filter_name>`
15//! - Name/tag sanitization to ensure valid metric identifiers
16//! - Convenience readiness gauge in [`readiness`]
17//!
18
19mod builder;
20pub mod readiness;
21
22pub use builder::{MetricsBuilder, MetricsInstanceBuilder};
23use pdk_core::classy::MetricsHost;
24use std::rc::Rc;
25
26/// Trait to interface with the underlying metrics system provided by the Flex engine.
27pub trait Metric {
28 /// Increase the current metric by the provided value
29 fn increase(&self, offset: i64);
30
31 /// Set the current metric to the provided value
32 fn set(&self, value: u64);
33
34 /// Return the current value of the metric
35 fn get(&self) -> u64;
36}
37
38/// Implementation of the [`Metric`] trait.
39pub struct MetricsInstance {
40 metrics: Rc<dyn MetricsHost>,
41 id: u32,
42}
43
44impl Metric for MetricsInstance {
45 fn increase(&self, offset: i64) {
46 self.metrics.increment_metric(self.id, offset);
47 }
48
49 fn set(&self, value: u64) {
50 self.metrics.record_metric(self.id, value);
51 }
52
53 fn get(&self) -> u64 {
54 self.metrics.get_metric(self.id)
55 }
56}