Skip to content

Commit

Permalink
Provide metrics for prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHecart committed Sep 21, 2023
1 parent 3e36640 commit 2edb8be
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 6 deletions.
157 changes: 151 additions & 6 deletions io/zenoh-transport/src/common/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ macro_rules! stats_struct {
$(#[$meta:meta])*
$vis:vis struct $struct_name:ident {
$(
$(#[$field_meta:meta])*
$field_vis:vis $field_name:ident,
$(# HELP $field_help:literal)?
$(# TYPE $field_type:literal)?
$(#[$field_meta:meta])*
$field_vis:vis $field_name:ident,
)*
}
) => {
paste::paste! {
$vis struct $struct_name {
parent: Option<std::sync::Arc<$struct_name>>,
$(
$(#[$field_meta:meta])*
$(#[$field_meta])*
$field_vis $field_name: AtomicUsize,
)*
}

$(#[$meta])*
$vis struct [<$struct_name Report>] {
$(
$(#[$field_meta:meta])*
$(#[$field_meta])*
$field_vis $field_name: usize,
)*
}
Expand Down Expand Up @@ -74,6 +76,41 @@ macro_rules! stats_struct {
}
}
}

impl [<$struct_name Report>] {
$vis fn openmetrics_text(&self) -> String {
let mut s = String::new();
$(
$(
s.push_str("# HELP ");
s.push_str(stringify!($field_name));
s.push_str(" ");
s.push_str($field_help);
s.push_str("\n");
)?
$(
s.push_str("# TYPE ");
s.push_str(stringify!($field_name));
s.push_str(" ");
s.push_str($field_type);
s.push_str("\n");
)?
s.push_str(stringify!($field_name));
s.push_str(" ");
s.push_str(self.$field_name.to_string().as_str());
s.push_str("\n");
)*
s
}
}

impl Default for [<$struct_name Report>] {
fn default() -> Self {
Self {
$($field_name: 0,)*
}
}
}
}
}
}
Expand All @@ -83,41 +120,149 @@ use std::sync::atomic::{AtomicUsize, Ordering};
stats_struct! {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TransportStats {
# HELP "Counter of sent bytes."
# TYPE "counter"
pub tx_bytes,

# HELP "Counter of sent transport messages."
# TYPE "counter"
pub tx_t_msgs,

# HELP "Counter of sent network messages."
# TYPE "counter"
pub tx_n_msgs,

# HELP "Counter of dropped network messages."
# TYPE "counter"
pub tx_n_dropped,

# HELP "Counter of sent zenoh put messages."
# TYPE "counter"
pub tx_z_put_user_msgs,

# HELP "Counter of sent bytes in zenoh put message payloads."
# TYPE "counter"
pub tx_z_put_user_pl_bytes,

# HELP "Counter of sent zenoh put messages."
# TYPE "counter"
pub tx_z_put_admin_msgs,

# HELP "Counter of sent bytes in zenoh put message payloads."
# TYPE "counter"
pub tx_z_put_admin_pl_bytes,

# HELP "Counter of sent zenoh del messages."
# TYPE "counter"
pub tx_z_del_user_msgs,

# HELP "Counter of sent zenoh del messages."
# TYPE "counter"
pub tx_z_del_admin_msgs,

# HELP "Counter of sent zenoh query messages."
# TYPE "counter"
pub tx_z_query_user_msgs,

# HELP "Counter of sent bytes in zenoh query message payloads."
# TYPE "counter"
pub tx_z_query_user_pl_bytes,

# HELP "Counter of sent zenoh query messages."
# TYPE "counter"
pub tx_z_query_admin_msgs,

# HELP "Counter of sent bytes in zenoh query message payloads."
# TYPE "counter"
pub tx_z_query_admin_pl_bytes,

# HELP "Counter of sent zenoh reply messages."
# TYPE "counter"
pub tx_z_reply_user_msgs,

# HELP "Counter of sent bytes in zenoh reply message payloads."
# TYPE "counter"
pub tx_z_reply_user_pl_bytes,

# HELP "Counter of sent zenoh reply messages."
# TYPE "counter"
pub tx_z_reply_admin_msgs,

# HELP "Counter of sent bytes in zenoh reply message payloads."
# TYPE "counter"
pub tx_z_reply_admin_pl_bytes,
pub tx_bytes,


# HELP "Counter of received bytes."
# TYPE "counter"
pub rx_bytes,

# HELP "Counter of received transport messages."
# TYPE "counter"
pub rx_t_msgs,

# HELP "Counter of received network messages."
# TYPE "counter"
pub rx_n_msgs,

# HELP "Counter of dropped network messages."
# TYPE "counter"
pub rx_n_dropped,

# HELP "Counter of received zenoh put messages."
# TYPE "counter"
pub rx_z_put_user_msgs,

# HELP "Counter of received bytes in zenoh put message payloads."
# TYPE "counter"
pub rx_z_put_user_pl_bytes,

# HELP "Counter of received zenoh put messages."
# TYPE "counter"
pub rx_z_put_admin_msgs,

# HELP "Counter of received bytes in zenoh put message payloads."
# TYPE "counter"
pub rx_z_put_admin_pl_bytes,

# HELP "Counter of received zenoh del messages."
# TYPE "counter"
pub rx_z_del_user_msgs,

# HELP "Counter of received zenoh del messages."
# TYPE "counter"
pub rx_z_del_admin_msgs,

# HELP "Counter of received zenoh query messages."
# TYPE "counter"
pub rx_z_query_user_msgs,

# HELP "Counter of received bytes in zenoh query message payloads."
# TYPE "counter"
pub rx_z_query_user_pl_bytes,

# HELP "Counter of received zenoh query messages."
# TYPE "counter"
pub rx_z_query_admin_msgs,

# HELP "Counter of received bytes in zenoh query message payloads."
# TYPE "counter"
pub rx_z_query_admin_pl_bytes,

# HELP "Counter of received zenoh reply messages."
# TYPE "counter"
pub rx_z_reply_user_msgs,

# HELP "Counter of received bytes in zenoh reply message payloads."
# TYPE "counter"
pub rx_z_reply_user_pl_bytes,

# HELP "Counter of received zenoh reply messages."
# TYPE "counter"
pub rx_z_reply_admin_msgs,

# HELP "Counter of received bytes in zenoh reply message payloads."
# TYPE "counter"
pub rx_z_reply_admin_pl_bytes,
pub rx_bytes,
}
}
38 changes: 38 additions & 0 deletions zenoh/src/net/runtime/adminspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl AdminSpace {

let mut handlers: HashMap<_, Handler> = HashMap::new();
handlers.insert(root_key.clone(), Arc::new(router_data));
handlers.insert(
format!("@/router/{zid_str}/metrics").try_into().unwrap(),
Arc::new(router_metrics),
);
handlers.insert(
format!("@/router/{zid_str}/linkstate/routers")
.try_into()
Expand Down Expand Up @@ -485,6 +489,40 @@ fn router_data(context: &AdminContext, query: Query) {
}
}

fn router_metrics(context: &AdminContext, query: Query) {
let reply_key: OwnedKeyExpr = format!("@/router/{}/metrics", context.zid_str)
.try_into()
.unwrap();
#[allow(unused_mut)]
let mut metrics = format!(
r#"# HELP zenoh_build Informations about zenoh.
# TYPE zenoh_build gauge
zenoh_build{{version="{}"}} 1
"#,
context.version
);

#[cfg(feature = "stats")]
metrics.push_str(
&context
.runtime
.manager()
.get_stats()
.report()
.openmetrics_text(),
);

if let Err(e) = query
.reply(Ok(Sample::new(
reply_key,
Value::from(metrics.as_bytes().to_vec()).encoding(KnownEncoding::TextPlain.into()),
)))
.res()
{
log::error!("Error sending AdminSpace reply: {:?}", e);
}
}

fn routers_linkstate_data(context: &AdminContext, query: Query) {
let reply_key: OwnedKeyExpr = format!("@/router/{}/linkstate/routers", context.zid_str)
.try_into()
Expand Down

0 comments on commit 2edb8be

Please sign in to comment.