Skip to content

Commit

Permalink
feat: emit errors in prover API metrics (#2890)
Browse files Browse the repository at this point in the history
## What ❔

Report reasons of failed calls in prover API metrics.

## Why ❔

To have more observability

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
Artemka374 authored Sep 17, 2024
1 parent f848d93 commit 2ac7cc5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
11 changes: 2 additions & 9 deletions core/node/external_proof_integration_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ use types::{ExternalProof, ProofGenerationDataResponse};
use zksync_basic_types::L1BatchNumber;

pub use crate::processor::Processor;
use crate::{
metrics::{CallOutcome, Method},
middleware::MetricsMiddleware,
};
use crate::{metrics::Method, middleware::MetricsMiddleware};

/// External API implementation.
#[derive(Debug)]
Expand All @@ -37,11 +34,7 @@ impl Api {
axum::middleware::from_fn(move |req: Request, next: Next| async move {
let middleware = MetricsMiddleware::new(method);
let response = next.run(req).await;
let outcome = match response.status().is_success() {
true => CallOutcome::Success,
false => CallOutcome::Failure,
};
middleware.observe(outcome);
middleware.observe(response.status());
response
})
};
Expand Down
11 changes: 2 additions & 9 deletions core/node/external_proof_integration_api/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ use std::time::Duration;

use vise::{EncodeLabelSet, EncodeLabelValue, Histogram, LabeledFamily, Metrics};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet, EncodeLabelValue)]
#[metrics(label = "outcome", rename_all = "snake_case")]
pub(crate) enum CallOutcome {
Success,
Failure,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet, EncodeLabelValue)]
#[metrics(label = "type", rename_all = "snake_case")]
pub(crate) enum Method {
Expand All @@ -20,8 +13,8 @@ pub(crate) enum Method {
#[derive(Debug, Metrics)]
#[metrics(prefix = "external_proof_integration_api")]
pub(crate) struct ProofIntegrationApiMetrics {
#[metrics(labels = ["method", "outcome"], buckets = vise::Buckets::LATENCIES)]
pub call_latency: LabeledFamily<(Method, CallOutcome), Histogram<Duration>, 2>,
#[metrics(labels = ["method", "status"], buckets = vise::Buckets::LATENCIES)]
pub call_latency: LabeledFamily<(Method, u16), Histogram<Duration>, 2>,
}

#[vise::register]
Expand Down
8 changes: 5 additions & 3 deletions core/node/external_proof_integration_api/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axum::http::StatusCode;
use tokio::time::Instant;

use crate::metrics::{CallOutcome, Method, METRICS};
use crate::metrics::{Method, METRICS};

#[derive(Debug)]
pub(crate) struct MetricsMiddleware {
Expand All @@ -16,7 +17,8 @@ impl MetricsMiddleware {
}
}

pub fn observe(&self, outcome: CallOutcome) {
METRICS.call_latency[&(self.method, outcome)].observe(self.started_at.elapsed());
pub fn observe(&self, status_code: StatusCode) {
METRICS.call_latency[&(self.method, status_code.as_u16())]
.observe(self.started_at.elapsed());
}
}

0 comments on commit 2ac7cc5

Please sign in to comment.