use std::{collections::BTreeMap, convert::Infallible, fmt, sync::Mutex};
use serde::{Deserialize, Deserializer, Serialize, Serializer, ser::SerializeStruct};
use thiserror::Error;
use crate::result::{ClassifyError, ErrorKind};
pub const MAX_METRIC_NAME_LENGTH: usize = 128;
pub const MAX_METRIC_LABELS: usize = 16;
pub const MAX_METRIC_LABEL_KEY_LENGTH: usize = 64;
pub const MAX_METRIC_LABEL_VALUE_LENGTH: usize = 256;
pub const DEFAULT_MAX_METRIC_SERIES: usize = 10_000;
pub const DEFAULT_MAX_METRIC_SERIES_PER_METRIC: usize = 1_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum MetricKind {
Counter,
Gauge,
Histogram,
}
#[derive(Clone, PartialEq)]
#[non_exhaustive]
pub struct MetricSample {
name: String,
kind: MetricKind,
value: f64,
labels: Vec<(String, String)>,
count: Option<u64>,
}
impl Serialize for MetricSample {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let include_count = !serializer.is_human_readable() || self.count.is_some();
let mut sample =
serializer.serialize_struct("MetricSample", if include_count { 5 } else { 4 })?;
sample.serialize_field("name", &self.name)?;
sample.serialize_field("kind", &self.kind)?;
sample.serialize_field("value", &self.value)?;
sample.serialize_field("labels", &self.labels)?;
if include_count {
sample.serialize_field("count", &self.count)?;
}
sample.end()
}
}
impl fmt::Debug for MetricSample {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MetricSample")
.field("name", &self.name)
.field("kind", &self.kind)
.field("value", &self.value)
.field("label_count", &self.labels.len())
.field("count", &self.count)
.finish()
}
}
impl MetricSample {
pub fn new(
name: impl Into<String>,
kind: MetricKind,
value: f64,
) -> Result<Self, MetricsError> {
validate_metric_kind_value(kind, value)?;
let name = normalize_metric_name(name, kind)?;
Ok(Self::from_valid_parts(name, kind, value))
}
fn from_valid_parts(name: String, kind: MetricKind, value: f64) -> Self {
Self {
name,
kind,
value,
labels: Vec::new(),
count: None,
}
}
pub fn counter(name: impl Into<String>, value: u64) -> Result<Self, MetricsError> {
let name = normalize_metric_name(name, MetricKind::Counter)?;
Ok(Self::from_valid_parts(
name,
MetricKind::Counter,
value as f64,
))
}
pub fn gauge(name: impl Into<String>, value: f64) -> Result<Self, MetricsError> {
Self::new(name, MetricKind::Gauge, value)
}
pub fn histogram(name: impl Into<String>, count: u64, sum: f64) -> Result<Self, MetricsError> {
Ok(Self::new(name, MetricKind::Histogram, sum)?.with_count(count))
}
#[must_use]
pub fn with_label(mut self, key: &'static str, value: impl Into<String>) -> Self {
let _ = self.insert_label(key.to_owned(), value.into());
self
}
pub fn try_with_label(
mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<Self, MetricsError> {
self.insert_label(key.into(), value.into())?;
Ok(self)
}
fn insert_label(&mut self, key: String, value: String) -> Result<(), MetricsError> {
let key = normalize_metric_label_key(key)?;
let value = normalize_metric_label_value(value)?;
if self.kind == MetricKind::Histogram && key == "le" {
return Err(MetricsError::ReservedHistogramLabel);
}
match self
.labels
.binary_search_by(|(existing, _)| existing.cmp(&key))
{
Ok(index) => self.labels[index].1 = value,
Err(index) => {
if self.labels.len() >= MAX_METRIC_LABELS {
return Err(MetricsError::TooManyLabels {
max: MAX_METRIC_LABELS,
});
}
self.labels.insert(index, (key, value));
}
}
Ok(())
}
#[must_use]
pub fn with_labels(mut self, labels: MetricLabels) -> Self {
self.labels = labels.into_vec();
if self.kind == MetricKind::Histogram {
self.labels.retain(|(key, _)| key != "le");
}
self
}
pub fn try_with_labels(mut self, labels: MetricLabels) -> Result<Self, MetricsError> {
validate_metric_kind_labels(self.kind, &labels)?;
self.labels = labels.into_vec();
Ok(self)
}
#[must_use]
pub const fn with_count(mut self, count: u64) -> Self {
self.count = Some(count);
self
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub const fn kind(&self) -> MetricKind {
self.kind
}
#[must_use]
pub const fn value(&self) -> f64 {
self.value
}
#[must_use]
pub const fn count(&self) -> Option<u64> {
self.count
}
#[must_use]
pub fn labels(&self) -> &[(String, String)] {
&self.labels
}
}
impl<'de> Deserialize<'de> for MetricSample {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct MetricSampleWire {
name: String,
kind: MetricKind,
value: f64,
#[serde(default)]
labels: MetricLabels,
#[serde(default)]
count: Option<u64>,
}
let wire = MetricSampleWire::deserialize(deserializer)?;
let mut sample =
Self::new(wire.name, wire.kind, wire.value).map_err(serde::de::Error::custom)?;
sample = sample
.try_with_labels(wire.labels)
.map_err(serde::de::Error::custom)?;
if let Some(count) = wire.count {
sample = sample.with_count(count);
}
Ok(sample)
}
}
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[non_exhaustive]
pub struct MetricLabels(Vec<(String, String)>);
impl fmt::Debug for MetricLabels {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MetricLabels")
.field("label_count", &self.0.len())
.finish_non_exhaustive()
}
}
impl MetricLabels {
pub fn new(
labels: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Result<Self, MetricsError> {
let mut normalized = BTreeMap::<String, String>::new();
for (index, (key, value)) in labels.into_iter().enumerate() {
if index >= MAX_METRIC_LABELS {
return Err(MetricsError::TooManyLabels {
max: MAX_METRIC_LABELS,
});
}
let key = normalize_metric_label_key(key)?;
let value = normalize_metric_label_value(value)?;
normalized.insert(key, value);
}
Ok(Self(normalized.into_iter().collect()))
}
#[must_use]
pub fn from_lossy(
labels: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Self {
let mut normalized = BTreeMap::<String, String>::new();
for (key, value) in labels.into_iter().take(MAX_METRIC_LABELS) {
if let (Ok(key), Ok(value)) = (
normalize_metric_label_key(key),
normalize_metric_label_value(value),
) {
normalized.insert(key, value);
}
}
Self(normalized.into_iter().collect())
}
pub fn single(key: impl Into<String>, value: impl Into<String>) -> Result<Self, MetricsError> {
Self::new([(key, value)])
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn as_slice(&self) -> &[(String, String)] {
&self.0
}
#[must_use]
pub fn get(&self, key: &str) -> Option<&str> {
let key = normalize_metric_label_key(key).ok()?;
self.0
.iter()
.find(|(name, _)| name == &key)
.map(|(_, value)| value.as_str())
}
#[must_use]
pub fn to_btree_map(&self) -> BTreeMap<String, String> {
self.0.iter().cloned().collect()
}
#[must_use]
pub fn into_vec(self) -> Vec<(String, String)> {
self.0
}
}
impl<'de> Deserialize<'de> for MetricLabels {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Self::new(Vec::<(String, String)>::deserialize(deserializer)?)
.map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Default, PartialEq, Serialize)]
#[non_exhaustive]
pub struct MetricsSnapshot {
samples: Vec<MetricSample>,
}
impl fmt::Debug for MetricsSnapshot {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MetricsSnapshot")
.field("sample_count", &self.samples.len())
.finish_non_exhaustive()
}
}
impl MetricsSnapshot {
#[must_use]
pub fn new(samples: Vec<MetricSample>) -> Self {
Self { samples }
}
pub fn push(&mut self, sample: MetricSample) {
self.samples.push(sample);
}
#[must_use]
pub fn samples(&self) -> &[MetricSample] {
&self.samples
}
}
impl<'de> Deserialize<'de> for MetricsSnapshot {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct SnapshotObject {
samples: Vec<MetricSample>,
}
#[derive(Deserialize)]
#[serde(untagged)]
enum SnapshotWire {
Object(SnapshotObject),
Legacy(Vec<MetricSample>),
}
let samples = if deserializer.is_human_readable() {
match SnapshotWire::deserialize(deserializer)? {
SnapshotWire::Object(object) => object.samples,
SnapshotWire::Legacy(samples) => samples,
}
} else {
SnapshotObject::deserialize(deserializer)?.samples
};
Ok(Self::new(samples))
}
}
pub trait MetricRecorder: Send + Sync + 'static {
fn increment_counter(
&self,
name: &str,
value: u64,
labels: MetricLabels,
) -> Result<(), MetricsError>;
fn set_gauge(&self, name: &str, value: f64, labels: MetricLabels) -> Result<(), MetricsError>;
fn observe_histogram(
&self,
name: &str,
value: f64,
labels: MetricLabels,
) -> Result<(), MetricsError>;
fn snapshot(&self) -> Result<MetricsSnapshot, MetricsError>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct MetricRegistryLimits {
max_series: usize,
max_series_per_metric: usize,
}
impl MetricRegistryLimits {
pub const fn new(
max_series: usize,
max_series_per_metric: usize,
) -> Result<Self, MetricsError> {
if max_series == 0 || max_series_per_metric == 0 || max_series_per_metric > max_series {
return Err(MetricsError::InvalidRegistryLimits);
}
Ok(Self {
max_series,
max_series_per_metric,
})
}
#[must_use]
pub const fn max_series(self) -> usize {
self.max_series
}
#[must_use]
pub const fn max_series_per_metric(self) -> usize {
self.max_series_per_metric
}
}
impl Default for MetricRegistryLimits {
fn default() -> Self {
Self {
max_series: DEFAULT_MAX_METRIC_SERIES,
max_series_per_metric: DEFAULT_MAX_METRIC_SERIES_PER_METRIC,
}
}
}
#[non_exhaustive]
pub struct MetricRegistry {
state: Mutex<MetricRegistryState>,
limits: MetricRegistryLimits,
}
impl Default for MetricRegistry {
fn default() -> Self {
Self {
state: Mutex::new(MetricRegistryState::default()),
limits: MetricRegistryLimits::default(),
}
}
}
impl fmt::Debug for MetricRegistry {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MetricRegistry")
.field(
"metric_count",
&self.state.try_lock().ok().map(|state| state.values.len()),
)
.field("limits", &self.limits)
.finish_non_exhaustive()
}
}
impl MetricRegistry {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_limits(limits: MetricRegistryLimits) -> Self {
Self {
state: Mutex::new(MetricRegistryState::default()),
limits,
}
}
#[must_use]
pub const fn limits(&self) -> MetricRegistryLimits {
self.limits
}
pub fn increment_counter(
&self,
name: &str,
value: u64,
labels: MetricLabels,
) -> Result<(), MetricsError> {
MetricRecorder::increment_counter(self, name, value, labels)
}
pub fn set_gauge(
&self,
name: &str,
value: f64,
labels: MetricLabels,
) -> Result<(), MetricsError> {
MetricRecorder::set_gauge(self, name, value, labels)
}
pub fn observe_histogram(
&self,
name: &str,
value: f64,
labels: MetricLabels,
) -> Result<(), MetricsError> {
MetricRecorder::observe_histogram(self, name, value, labels)
}
pub fn snapshot(&self) -> Result<MetricsSnapshot, MetricsError> {
MetricRecorder::snapshot(self)
}
}
impl MetricRecorder for MetricRegistry {
fn increment_counter(
&self,
name: &str,
value: u64,
labels: MetricLabels,
) -> Result<(), MetricsError> {
let identity = MetricIdentity::new(name, MetricKind::Counter, labels)?;
let mut state = self.state.lock().map_err(|_| MetricsError::LockPoisoned)?;
if let Some(aggregate) = state.values.get_mut(&identity) {
aggregate.value = finite_metric_sum(aggregate.value, value as f64)?;
} else {
state.insert_new(
identity,
MetricAggregate {
value: value as f64,
count: None,
},
self.limits,
)?;
}
Ok(())
}
fn set_gauge(&self, name: &str, value: f64, labels: MetricLabels) -> Result<(), MetricsError> {
validate_metric_value(value)?;
let identity = MetricIdentity::new(name, MetricKind::Gauge, labels)?;
let mut state = self.state.lock().map_err(|_| MetricsError::LockPoisoned)?;
if let Some(aggregate) = state.values.get_mut(&identity) {
*aggregate = MetricAggregate { value, count: None };
} else {
state.insert_new(
identity,
MetricAggregate { value, count: None },
self.limits,
)?;
}
Ok(())
}
fn observe_histogram(
&self,
name: &str,
value: f64,
labels: MetricLabels,
) -> Result<(), MetricsError> {
validate_metric_kind_value(MetricKind::Histogram, value)?;
let identity = MetricIdentity::new(name, MetricKind::Histogram, labels)?;
let mut state = self.state.lock().map_err(|_| MetricsError::LockPoisoned)?;
if let Some(aggregate) = state.values.get_mut(&identity) {
aggregate.value = finite_metric_sum(aggregate.value, value)?;
aggregate.count = Some(aggregate.count.map_or(1, |count| count.saturating_add(1)));
} else {
state.insert_new(
identity,
MetricAggregate {
value,
count: Some(1),
},
self.limits,
)?;
}
Ok(())
}
fn snapshot(&self) -> Result<MetricsSnapshot, MetricsError> {
let state = self.state.lock().map_err(|_| MetricsError::LockPoisoned)?;
let samples = state
.values
.iter()
.map(|(identity, aggregate)| {
let mut sample =
MetricSample::new(identity.name.clone(), identity.kind, aggregate.value)?
.with_labels(identity.labels.clone());
if let Some(count) = aggregate.count {
sample = sample.with_count(count);
}
Ok(sample)
})
.collect::<Result<_, MetricsError>>()?;
Ok(MetricsSnapshot::new(samples))
}
}
fn finite_metric_sum(left: f64, right: f64) -> Result<f64, MetricsError> {
let sum = left + right;
validate_metric_value(sum)?;
Ok(sum)
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct MetricIdentity {
name: String,
kind: MetricKind,
labels: MetricLabels,
}
impl MetricIdentity {
fn new(
name: impl Into<String>,
kind: MetricKind,
labels: MetricLabels,
) -> Result<Self, MetricsError> {
validate_metric_kind_labels(kind, &labels)?;
Ok(Self {
name: normalize_metric_name(name, kind)?,
kind,
labels,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct MetricAggregate {
value: f64,
count: Option<u64>,
}
#[derive(Default)]
struct MetricRegistryState {
values: BTreeMap<MetricIdentity, MetricAggregate>,
series_per_metric: BTreeMap<String, usize>,
exported_names: BTreeMap<String, (String, MetricKind)>,
}
impl MetricRegistryState {
fn insert_new(
&mut self,
identity: MetricIdentity,
aggregate: MetricAggregate,
limits: MetricRegistryLimits,
) -> Result<(), MetricsError> {
let names = exported_metric_names(&identity.name, identity.kind);
for name in &names {
if let Some((owner, kind)) = self.exported_names.get(name) {
if owner == &identity.name && *kind != identity.kind {
return Err(MetricsError::MetricTypeConflict);
}
if owner != &identity.name {
return Err(MetricsError::MetricNameConflict);
}
}
}
if self.values.len() >= limits.max_series {
return Err(MetricsError::RegistrySeriesLimitExceeded {
max: limits.max_series,
});
}
let metric_series = self
.series_per_metric
.get(&identity.name)
.copied()
.unwrap_or_default();
if metric_series >= limits.max_series_per_metric {
return Err(MetricsError::MetricSeriesLimitExceeded {
max: limits.max_series_per_metric,
});
}
let metric_name = identity.name.clone();
for name in names {
self.exported_names
.insert(name, (metric_name.clone(), identity.kind));
}
self.values.insert(identity, aggregate);
self.series_per_metric
.insert(metric_name, metric_series.saturating_add(1));
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum MetricsError {
#[error("histogram labels must not contain the reserved 'le' label")]
ReservedHistogramLabel,
#[error("counter and histogram values must not be negative")]
NegativeValue,
#[error("metric name is already registered with a different type")]
MetricTypeConflict,
#[error("metric name conflicts with another exported metric family or sample")]
MetricNameConflict,
#[error("metrics registry lock is poisoned")]
LockPoisoned,
#[error("metric name must not be empty")]
InvalidName,
#[error("metric name must be less than or equal to {max} bytes")]
NameTooLong {
#[doc = "The configured upper bound."]
max: usize,
},
#[error("metric must contain at most {max} labels")]
TooManyLabels {
#[doc = "The configured upper bound."]
max: usize,
},
#[error("metric label key must not be empty")]
InvalidLabelKey,
#[error("metric label key must be less than or equal to {max} bytes")]
LabelKeyTooLong {
#[doc = "The configured upper bound."]
max: usize,
},
#[error("metric label value must not contain control characters")]
InvalidLabelValue,
#[error("metric label value must be less than or equal to {max} bytes")]
LabelValueTooLong {
#[doc = "The configured upper bound."]
max: usize,
},
#[error("metric value must be finite")]
InvalidValue,
#[error(
"metric registry limits must be non-zero and the per-metric limit must not exceed the total limit"
)]
InvalidRegistryLimits,
#[error("metric registry series limit of {max} was exceeded")]
RegistrySeriesLimitExceeded {
#[doc = "The configured upper bound."]
max: usize,
},
#[error("per-metric series limit of {max} was exceeded")]
MetricSeriesLimitExceeded {
#[doc = "The configured upper bound."]
max: usize,
},
}
impl ClassifyError for MetricsError {
fn error_kind(&self) -> ErrorKind {
match self {
Self::LockPoisoned => ErrorKind::Unavailable,
Self::InvalidName
| Self::ReservedHistogramLabel
| Self::NegativeValue
| Self::MetricTypeConflict
| Self::MetricNameConflict
| Self::NameTooLong { .. }
| Self::TooManyLabels { .. }
| Self::InvalidLabelKey
| Self::LabelKeyTooLong { .. }
| Self::InvalidLabelValue
| Self::LabelValueTooLong { .. }
| Self::InvalidValue
| Self::InvalidRegistryLimits
| Self::RegistrySeriesLimitExceeded { .. }
| Self::MetricSeriesLimitExceeded { .. } => ErrorKind::Validation,
}
}
fn error_code(&self) -> crate::result::ErrorCode {
crate::result::ErrorCode::internal(match self {
Self::ReservedHistogramLabel => "metric_histogram_label_reserved",
Self::NegativeValue => "metric_value_negative",
Self::MetricTypeConflict => "metric_type_conflict",
Self::MetricNameConflict => "metric_name_conflict",
Self::LockPoisoned => "metrics_registry_unavailable",
Self::InvalidName => "metric_name_invalid",
Self::NameTooLong { .. } => "metric_name_too_long",
Self::TooManyLabels { .. } => "metric_labels_too_many",
Self::InvalidLabelKey => "metric_label_key_invalid",
Self::LabelKeyTooLong { .. } => "metric_label_key_too_long",
Self::InvalidLabelValue => "metric_label_value_invalid",
Self::LabelValueTooLong { .. } => "metric_label_value_too_long",
Self::InvalidValue => "metric_value_invalid",
Self::InvalidRegistryLimits => "metrics_registry_limits_invalid",
Self::RegistrySeriesLimitExceeded { .. } => "metrics_registry_series_limit_exceeded",
Self::MetricSeriesLimitExceeded { .. } => "metric_series_limit_exceeded",
})
}
}
fn validate_metric_kind_value(kind: MetricKind, value: f64) -> Result<(), MetricsError> {
validate_metric_value(value)?;
if kind != MetricKind::Gauge && value < 0.0 {
return Err(MetricsError::NegativeValue);
}
Ok(())
}
fn validate_metric_kind_labels(
kind: MetricKind,
labels: &MetricLabels,
) -> Result<(), MetricsError> {
if kind == MetricKind::Histogram && labels.get("le").is_some() {
return Err(MetricsError::ReservedHistogramLabel);
}
Ok(())
}
fn metric_family_name(name: &str, kind: MetricKind) -> &str {
if kind == MetricKind::Counter {
name.strip_suffix("_total").unwrap_or(name)
} else {
name
}
}
fn exported_metric_names(name: &str, kind: MetricKind) -> Vec<String> {
let family = metric_family_name(name, kind);
let suffixes: &[&str] = match kind {
MetricKind::Counter => &["", "_total", "_created"],
MetricKind::Gauge => &[""],
MetricKind::Histogram => &["", "_sum", "_count", "_bucket", "_created"],
};
suffixes
.iter()
.map(|suffix| format!("{family}{suffix}"))
.collect()
}
#[must_use]
pub fn encode_openmetrics(snapshot: &MetricsSnapshot) -> String {
let mut output = String::new();
let mut emitted_types = BTreeMap::<String, MetricKind>::new();
let mut samples: Vec<_> = snapshot.samples().iter().collect();
samples.sort_by_key(|sample| metric_family_name(sample.name(), sample.kind()));
for sample in samples {
let name = sample.name();
let family = metric_family_name(name, sample.kind());
if let std::collections::btree_map::Entry::Vacant(entry) =
emitted_types.entry(family.to_owned())
{
entry.insert(sample.kind());
output.push_str("# TYPE ");
output.push_str(family);
output.push(' ');
output.push_str(openmetrics_kind(sample.kind()));
output.push('\n');
}
match (sample.kind(), sample.count()) {
(MetricKind::Histogram, Some(count)) => {
output.push_str(name);
output.push_str("_bucket");
let mut labels = sample.labels().to_vec();
labels.push(("le".to_owned(), "+Inf".to_owned()));
output.push_str(&encode_labels(&labels));
output.push(' ');
output.push_str(&count.to_string());
output.push('\n');
output.push_str(name);
output.push_str("_sum");
output.push_str(&encode_labels(sample.labels()));
output.push(' ');
output.push_str(&format_float(sample.value()));
output.push('\n');
output.push_str(name);
output.push_str("_count");
output.push_str(&encode_labels(sample.labels()));
output.push(' ');
output.push_str(&count.to_string());
output.push('\n');
}
_ => {
output.push_str(family);
if sample.kind() == MetricKind::Counter {
output.push_str("_total");
}
output.push_str(&encode_labels(sample.labels()));
output.push(' ');
output.push_str(&format_float(sample.value()));
output.push('\n');
}
}
}
output.push_str("# EOF\n");
output
}
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct MetricsExport {
content_type: String,
body: String,
}
impl fmt::Debug for MetricsExport {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MetricsExport")
.field("content_type", &self.content_type)
.field("body_bytes", &self.body.len())
.finish()
}
}
impl MetricsExport {
#[must_use]
pub fn new(content_type: impl Into<String>, body: impl Into<String>) -> Self {
Self {
content_type: content_type.into(),
body: body.into(),
}
}
#[must_use]
pub fn content_type(&self) -> &str {
&self.content_type
}
#[must_use]
pub fn body(&self) -> &str {
&self.body
}
#[must_use]
pub fn into_body(self) -> String {
self.body
}
}
pub trait MetricsExporter {
type Error;
fn export(&self, snapshot: &MetricsSnapshot) -> Result<MetricsExport, Self::Error>;
}
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct OpenMetricsExporter;
impl OpenMetricsExporter {
pub const CONTENT_TYPE: &'static str =
"application/openmetrics-text; version=1.0.0; charset=utf-8";
}
impl MetricsExporter for OpenMetricsExporter {
type Error = Infallible;
fn export(&self, snapshot: &MetricsSnapshot) -> Result<MetricsExport, Self::Error> {
Ok(MetricsExport::new(
Self::CONTENT_TYPE,
encode_openmetrics(snapshot),
))
}
}
fn normalize_metric_name(
value: impl Into<String>,
kind: MetricKind,
) -> Result<String, MetricsError> {
let value = value.into();
let value = value.trim();
if value.is_empty() {
return Err(MetricsError::InvalidName);
}
if value.len() > MAX_METRIC_NAME_LENGTH {
return Err(MetricsError::NameTooLong {
max: MAX_METRIC_NAME_LENGTH,
});
}
let mut normalized = String::with_capacity(value.len().max(1));
for byte in value.bytes() {
if byte.is_ascii_alphanumeric() || byte == b':' || byte == b'_' {
normalized.push(char::from(byte));
} else {
normalized.push('_');
}
}
if normalized.as_bytes().first().is_none_or(u8::is_ascii_digit) {
normalized.insert(0, '_');
}
if normalized.len() > MAX_METRIC_NAME_LENGTH {
return Err(MetricsError::NameTooLong {
max: MAX_METRIC_NAME_LENGTH,
});
}
if metric_family_name(&normalized, kind).is_empty() {
return Err(MetricsError::InvalidName);
}
Ok(normalized)
}
fn normalize_metric_label_key(value: impl Into<String>) -> Result<String, MetricsError> {
let value = value.into();
let value = value.trim();
if value.is_empty() {
return Err(MetricsError::InvalidLabelKey);
}
if value.len() > MAX_METRIC_LABEL_KEY_LENGTH {
return Err(MetricsError::LabelKeyTooLong {
max: MAX_METRIC_LABEL_KEY_LENGTH,
});
}
let mut normalized = String::with_capacity(value.len().max(1));
for byte in value.bytes() {
if byte.is_ascii_alphanumeric() || byte == b'_' {
normalized.push(char::from(byte));
} else {
normalized.push('_');
}
}
if normalized.as_bytes().first().is_none_or(u8::is_ascii_digit) {
normalized.insert(0, '_');
}
if normalized.len() > MAX_METRIC_LABEL_KEY_LENGTH {
return Err(MetricsError::LabelKeyTooLong {
max: MAX_METRIC_LABEL_KEY_LENGTH,
});
}
Ok(normalized)
}
fn normalize_metric_label_value(value: impl Into<String>) -> Result<String, MetricsError> {
let value = value.into();
if value.len() > MAX_METRIC_LABEL_VALUE_LENGTH {
return Err(MetricsError::LabelValueTooLong {
max: MAX_METRIC_LABEL_VALUE_LENGTH,
});
}
if value.chars().any(char::is_control) {
return Err(MetricsError::InvalidLabelValue);
}
Ok(value)
}
fn validate_metric_value(value: f64) -> Result<(), MetricsError> {
if value.is_finite() {
Ok(())
} else {
Err(MetricsError::InvalidValue)
}
}
fn openmetrics_kind(kind: MetricKind) -> &'static str {
match kind {
MetricKind::Counter => "counter",
MetricKind::Gauge => "gauge",
MetricKind::Histogram => "histogram",
}
}
fn encode_labels(labels: &[(String, String)]) -> String {
if labels.is_empty() {
return String::new();
}
let mut output = String::from("{");
for (index, (key, value)) in labels.iter().enumerate() {
if index > 0 {
output.push(',');
}
output.push_str(key);
output.push_str("=\"");
output.push_str(&escape_label_value(value));
output.push('"');
}
output.push('}');
output
}
fn escape_label_value(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for character in value.chars() {
match character {
'\\' => escaped.push_str("\\\\"),
'"' => escaped.push_str("\\\""),
'\n' => escaped.push_str("\\n"),
_ => escaped.push(character),
}
}
escaped
}
fn format_float(value: f64) -> String {
if value.is_finite() {
value.to_string()
} else {
"0".to_owned()
}
}