Skip to content

Commit

Permalink
Add client_requests timing module to metrics (#802)
Browse files Browse the repository at this point in the history
* Add client_requests timing module to metrics

- Add a timing span to anything that can be instrumented and returns a Result.

  Example:
  ```ignore
  let client = GatewayClient::new(channel);

  client.info(req)
    .with_timing("iot_fetch_info")
    .await?;
  ```

  This will result in a prometheus metric
  >> client_request_duration_seconds{name = "iot_fetch_info", quantile="xxx"}

- Install the `ApiTimingLayer`.

  Adding `.with_span_events(FmtSpan::CLOSE)` to a regular format layer will
  print the timing spans to stdout as well.

  Example:
  ```ignore
  tracing_subscriber::registry()
    .with(tracing_subscriber::fmt::layer().with_span_events(FmtSpan::CLOSE))
    .with(metrics::client_requests::client_request_timing_layer("histogram_name"))
    .init();
  ```

- Remove unused `install_metrics` function, replace with nested
  `install` function that `start_metrics` delegates to. This allows us to
  start metrics in tests without needing to make a `Settings` struct.

* Update metrics (#805)

* Update metrics crate

Also bumps metrics-exporter-prometheus.

The biggest change from 0.21 -> 0.22 is in this PR
metrics-rs/metrics#394

* allow for versions greater than 0.22

this may change if the api to metrics changes _again_ before a major version.

* make string values consts

Makes the values hide a little bit less in the code
  • Loading branch information
michaeldjeffrey authored May 6, 2024
1 parent d29595c commit 2531d93
Show file tree
Hide file tree
Showing 31 changed files with 440 additions and 152 deletions.
170 changes: 128 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ reqwest = { version = "0", default-features = false, features = [
] }
beacon = { git = "https://github.com/helium/proto", branch = "master" }
humantime = "2"
metrics = "0.21"
metrics = ">=0.22"
metrics-exporter-prometheus = "0"
tracing = "0"
tracing-subscriber = { version = "0", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion boost_manager/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub async fn last_reward_processed_time(
db: &Pool<Postgres>,
datetime: DateTime<Utc>,
) -> anyhow::Result<()> {
metrics::gauge!(LAST_REWARD_PROCESSED_TIME, datetime.timestamp() as f64);
metrics::gauge!(LAST_REWARD_PROCESSED_TIME).set(datetime.timestamp() as f64);
meta::store(db, LAST_REWARD_PROCESSED_TIME, datetime.timestamp()).await?;

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions boost_manager/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ where

async fn check_failed_activations(&self) -> Result<()> {
let num_marked_failed = db::update_failed_activations(&self.pool).await?;
metrics::counter!("failed_activations", num_marked_failed);
metrics::counter!("failed_activations").increment(num_marked_failed);
let total_failed_count = db::get_failed_activations_count(&self.pool).await?;
metrics::gauge!("db_failed_row_count", total_failed_count as f64);
metrics::gauge!("db_failed_row_count").set(total_failed_count as f64);
if total_failed_count > 0 {
tracing::warn!("{} failed status activations ", total_failed_count);
};
Expand All @@ -159,7 +159,7 @@ where
summed_activations_count: u64,
) -> Result<()> {
tracing::info!("processed batch of {} activations successfully", batch_size);
metrics::counter!("success_activations", summed_activations_count);
metrics::counter!("success_activations").increment(summed_activations_count);
db::update_success_batch(&self.pool, ids).await?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions db_store/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ macro_rules! query_exec_timed {
( $name:literal, $query:expr, $meth:ident, $exec:expr ) => {{
match poc_metrics::record_duration!(concat!($name, "_duration"), $query.$meth($exec).await) {
Ok(x) => {
metrics::increment_counter!(concat!($name, "_count"), "status" => "ok");
metrics::counter!(concat!($name, "_count"), "status" => "ok").increment(1);
Ok(x)
}
Err(e) => {
metrics::increment_counter!(concat!($name, "_count"), "status" => "error");
metrics::counter!(concat!($name, "_count"), "status" => "error").increment(1);
Err(Error::SqlError(e))
}
}
Expand Down
Loading

0 comments on commit 2531d93

Please sign in to comment.