use std::time::Duration;
use crate::bus::MessageKind;
use crate::telemetry::service_label;
use super::prometheus::render_prometheus;
use super::registry::{
registry, DispatchKey, GraphqlRequestKey, MetricsSnapshot, OutboxMessageKey,
TransportFailureKey, TransportMessageKey,
};
pub fn describe_service(service: Option<&str>) {
registry().describe_service(service_label(service));
}
pub fn record_microsvc_dispatch(
service: Option<&str>,
kind: MessageKind,
message: &str,
status: &str,
duration: Duration,
) {
registry().record_microsvc_dispatch(DispatchKey {
service: service_label(service),
message_kind: kind.as_str().to_string(),
message: message.to_string(),
status: status.to_string(),
duration_seconds: duration.as_secs_f64(),
});
}
pub fn record_transport_message(
service: Option<&str>,
transport: &str,
kind: MessageKind,
outcome: &str,
) {
registry().record_transport_message(TransportMessageKey {
service: service_label(service),
transport: transport.to_string(),
message_kind: kind.as_str().to_string(),
outcome: outcome.to_string(),
});
}
pub fn record_transport_failure(
service: Option<&str>,
transport: &str,
failure_class: &str,
action: &str,
) {
registry().record_transport_failure(TransportFailureKey {
service: service_label(service),
transport: transport.to_string(),
failure_class: failure_class.to_string(),
action: action.to_string(),
});
}
pub fn record_graphql_request(
service: Option<&str>,
root_field: &str,
status: &str,
duration: Duration,
) {
registry().record_graphql_request(GraphqlRequestKey {
service: service_label(service),
root_field: root_field.to_string(),
status: status.to_string(),
duration_seconds: duration.as_secs_f64(),
});
}
pub fn record_outbox_message(service: Option<&str>, outcome: &str) {
record_outbox_messages(service, outcome, 1);
}
pub fn record_outbox_messages(service: Option<&str>, outcome: &str, count: usize) {
if count == 0 {
return;
}
registry().record_outbox_messages(
OutboxMessageKey {
service: service_label(service),
outcome: outcome.to_string(),
},
count as u64,
);
}
pub fn set_outbox_backlog(
service: Option<&str>,
pending: usize,
oldest_pending_age: Option<Duration>,
) {
registry().set_outbox_backlog(
service_label(service),
pending as f64,
oldest_pending_age.map(|duration| duration.as_secs_f64()),
);
}
pub fn prometheus_text() -> String {
render_prometheus(&snapshot())
}
pub(crate) fn snapshot() -> MetricsSnapshot {
registry().snapshot()
}