Skip to main content

rpc/forms/
metrics.rs

1//! The `nomisync_form_requests_total` series: one count per request at its entry adapter.
2
3use server::account_state::STALE_ACCOUNT;
4use server::commodity_state::STALE_COMMODITY;
5use server::logical::STALE_REVISION;
6use server::script_state::STALE_SCRIPT;
7use server::tag_state::STALE_TAG;
8use server::telemetry::recorder::FORM_REQUESTS;
9use server::template_state::STALE_TEMPLATE;
10
11use super::Failure;
12use crate::envelope::ErrorCode;
13use crate::host_failure::condition_code;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum FormAction {
17    Load,
18    Submit,
19    Query,
20}
21
22impl FormAction {
23    #[must_use]
24    pub const fn as_str(self) -> &'static str {
25        match self {
26            Self::Load => "load",
27            Self::Submit => "submit",
28            Self::Query => "query",
29        }
30    }
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum FormOutcome {
35    Ok,
36    Invalid,
37    UnknownForm,
38    Malformed,
39    MalformedId,
40    Missing,
41    Refused,
42    Stale,
43    Internal,
44}
45
46impl FormOutcome {
47    #[must_use]
48    pub const fn as_str(self) -> &'static str {
49        match self {
50            Self::Ok => "ok",
51            Self::Invalid => "invalid",
52            Self::UnknownForm => "unknown_form",
53            Self::Malformed => "malformed",
54            Self::MalformedId => "malformed_id",
55            Self::Missing => "missing",
56            Self::Refused => "refused",
57            Self::Stale => "stale",
58            Self::Internal => "internal",
59        }
60    }
61}
62
63impl FormOutcome {
64    fn from_condition(code: Option<&str>) -> Self {
65        match code {
66            Some(ErrorCode::NOT_FOUND) => Self::Missing,
67            Some(ErrorCode::ARGS) => Self::Refused,
68            _ => Self::Internal,
69        }
70    }
71}
72
73impl From<&Failure> for FormOutcome {
74    fn from(failure: &Failure) -> Self {
75        match failure {
76            Failure::Invalid(errors) if errors.iter().any(is_stale) => Self::Stale,
77            Failure::Invalid(_) => Self::Invalid,
78            Failure::UnknownForm(_) => Self::UnknownForm,
79            Failure::MalformedId { .. } => Self::MalformedId,
80            Failure::Missing(_) => Self::Missing,
81            Failure::Refused(_) => Self::Refused,
82            Failure::Internal(err) => Self::from_condition(condition_code(err)),
83        }
84    }
85}
86
87fn is_stale(error: &forms::FieldError) -> bool {
88    error.field == "revision"
89        && [
90            STALE_REVISION,
91            STALE_ACCOUNT,
92            STALE_COMMODITY,
93            STALE_TAG,
94            STALE_TEMPLATE,
95            STALE_SCRIPT,
96        ]
97        .contains(&error.message.as_str())
98}
99
100/// The compiled-in form name, so an arbitrary client string never becomes a label.
101fn form_label(name: &str) -> &'static str {
102    forms::by_name(name).map_or("unknown", |form| form.name)
103}
104
105pub fn record_form_request(form: &str, action: FormAction, outcome: FormOutcome) {
106    metrics::counter!(
107        FORM_REQUESTS,
108        "form" => form_label(form),
109        "action" => action.as_str(),
110        "outcome" => outcome.as_str()
111    )
112    .increment(1);
113}
114
115/// Counts `result` as one request and hands it back.
116pub fn count_result<T>(
117    form: &str,
118    action: FormAction,
119    result: Result<T, Failure>,
120) -> Result<T, Failure> {
121    let outcome = result
122        .as_ref()
123        .err()
124        .map_or(FormOutcome::Ok, FormOutcome::from);
125    record_form_request(form, action, outcome);
126    result
127}