Skip to content

Commit

Permalink
Improve "http_request_duration_seconds" metric and add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
vruello committed Nov 23, 2024
1 parent e908b58 commit 236d6dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
15 changes: 12 additions & 3 deletions common/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ impl Outputs {
pub struct Monitoring {
listen_address: String,
listen_port: u16,
http_requests_histogram_buckets: Option<Vec<f64>>,
http_request_duration_buckets: Option<Vec<f64>>,
count_received_events_per_machine: Option<bool>,
count_event_size_per_machine: Option<bool>,
count_http_request_body_network_size_per_machine: Option<bool>,
Expand All @@ -389,8 +389,8 @@ impl Monitoring {
self.listen_port
}

pub fn http_requests_histogram_buckets(&self) -> &[f64] {
match &self.http_requests_histogram_buckets {
pub fn http_request_duration_buckets(&self) -> &[f64] {
match &self.http_request_duration_buckets {
Some(bucket) => bucket,
None => &[
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
Expand Down Expand Up @@ -651,6 +651,10 @@ mod tests {
s.monitoring().unwrap().count_received_events_per_machine(),
false
);
assert_eq!(
s.monitoring().unwrap().http_request_duration_buckets(),
&[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,]
);
}

const CONFIG_TLS_POSTGRES_WITH_CLI: &str = r#"
Expand Down Expand Up @@ -683,6 +687,7 @@ mod tests {
[monitoring]
listen_address = "127.0.0.1"
listen_port = 9090
http_request_duration_buckets = [0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0]
count_event_size_per_machine = true
count_http_request_body_network_size_per_machine = true
count_http_request_body_real_size_per_machine = true
Expand Down Expand Up @@ -714,6 +719,10 @@ mod tests {
s.monitoring().unwrap().count_received_events_per_machine(),
true
);
assert_eq!(
s.monitoring().unwrap().http_request_duration_buckets(),
&[0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0]
);
}

const CONFIG_TLS_POSTGRES_WITH_OUTPUTS: &str = r#"
Expand Down
4 changes: 4 additions & 0 deletions openwec.conf.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@
# Listen port of the Prometheus-compatible endpoint
# listen_port =

# [Optional]
# Request duration buckets (in seconds) used by the "http_request_duration_seconds" histogram
# http_request_duration_buckets = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]

# [Optional]
# If set, a "machine" label will be added to the "openwec_received_events_total" metric
# Warning: this may cause a HUGE increase in metric cardinality
Expand Down
34 changes: 17 additions & 17 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ use libgssapi::error::MajorFlags;
use log::{debug, error, info, trace, warn};
use metrics::{counter, histogram};
use monitoring::{
HTTP_REQUESTS_DURATION_SECONDS_HISTOGRAM, HTTP_REQUESTS_MACHINE, HTTP_REQUESTS_METHOD,
HTTP_REQUESTS_STATUS, HTTP_REQUESTS_URI, HTTP_REQUEST_BODY_NETWORK_SIZE_BYTES_COUNTER,
HTTP_REQUEST_BODY_REAL_SIZE_BYTES_COUNTER,
HTTP_REQUEST_BODY_NETWORK_SIZE_BYTES_COUNTER, HTTP_REQUEST_BODY_REAL_SIZE_BYTES_COUNTER,
HTTP_REQUEST_DURATION_SECONDS_HISTOGRAM, HTTP_REQUEST_MACHINE, HTTP_REQUEST_METHOD,
HTTP_REQUEST_STATUS, HTTP_REQUEST_URI,
};
use quick_xml::writer::Writer;
use soap::Serializable;
Expand Down Expand Up @@ -194,14 +194,14 @@ async fn get_request_payload(
if monitoring_conf.count_http_request_body_network_size_per_machine() =>
{
counter!(HTTP_REQUEST_BODY_NETWORK_SIZE_BYTES_COUNTER,
HTTP_REQUESTS_METHOD => request_data.method().to_string(),
HTTP_REQUESTS_URI => request_data.uri().to_string(),
HTTP_REQUESTS_MACHINE => request_data.principal().to_string())
HTTP_REQUEST_METHOD => request_data.method().to_string(),
HTTP_REQUEST_URI => request_data.uri().to_string(),
HTTP_REQUEST_MACHINE => request_data.principal().to_string())
}
_ => {
counter!(HTTP_REQUEST_BODY_NETWORK_SIZE_BYTES_COUNTER,
HTTP_REQUESTS_METHOD => request_data.method().to_string(),
HTTP_REQUESTS_URI => request_data.uri().to_string())
HTTP_REQUEST_METHOD => request_data.method().to_string(),
HTTP_REQUEST_URI => request_data.uri().to_string())
}
};
http_request_body_network_size_bytes_counter.increment(data.len().try_into()?);
Expand All @@ -220,14 +220,14 @@ async fn get_request_payload(
if monitoring_conf.count_http_request_body_real_size_per_machine() =>
{
counter!(HTTP_REQUEST_BODY_REAL_SIZE_BYTES_COUNTER,
HTTP_REQUESTS_METHOD => request_data.method().to_string(),
HTTP_REQUESTS_URI => request_data.uri().to_string(),
HTTP_REQUESTS_MACHINE => request_data.principal().to_string())
HTTP_REQUEST_METHOD => request_data.method().to_string(),
HTTP_REQUEST_URI => request_data.uri().to_string(),
HTTP_REQUEST_MACHINE => request_data.principal().to_string())
}
_ => {
counter!(HTTP_REQUEST_BODY_REAL_SIZE_BYTES_COUNTER,
HTTP_REQUESTS_METHOD => request_data.method().to_string(),
HTTP_REQUESTS_URI => request_data.uri().to_string())
HTTP_REQUEST_METHOD => request_data.method().to_string(),
HTTP_REQUEST_URI => request_data.uri().to_string())
}
};
http_request_body_real_size_bytes_counter.increment(bytes.len().try_into()?);
Expand Down Expand Up @@ -438,10 +438,10 @@ fn log_response(
) {
let duration = start.elapsed().as_secs_f64();

histogram!(HTTP_REQUESTS_DURATION_SECONDS_HISTOGRAM,
HTTP_REQUESTS_METHOD => method.to_owned(),
HTTP_REQUESTS_STATUS => status.to_string(),
HTTP_REQUESTS_URI => uri.to_owned())
histogram!(HTTP_REQUEST_DURATION_SECONDS_HISTOGRAM,
HTTP_REQUEST_METHOD => method.to_owned(),
HTTP_REQUEST_STATUS => status.to_string(),
HTTP_REQUEST_URI => uri.to_owned())
.record(duration);

// MDC is thread related, so it should be safe to use it in a non-async
Expand Down
16 changes: 8 additions & 8 deletions server/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub const EVENTS_MACHINE: &str = "machine";

pub const FAILED_EVENTS_COUNTER: &str = "openwec_event_output_failures_total";

pub const HTTP_REQUESTS_DURATION_SECONDS_HISTOGRAM: &str = "http_request_duration_seconds";
pub const HTTP_REQUESTS_METHOD: &str = "method";
pub const HTTP_REQUESTS_URI: &str = "uri";
pub const HTTP_REQUESTS_STATUS: &str = "status";
pub const HTTP_REQUESTS_MACHINE: &str = "machine";
pub const HTTP_REQUEST_DURATION_SECONDS_HISTOGRAM: &str = "http_request_duration_seconds";
pub const HTTP_REQUEST_METHOD: &str = "method";
pub const HTTP_REQUEST_URI: &str = "uri";
pub const HTTP_REQUEST_STATUS: &str = "status";
pub const HTTP_REQUEST_MACHINE: &str = "machine";

pub const HTTP_REQUEST_BODY_NETWORK_SIZE_BYTES_COUNTER: &str =
"http_request_body_network_size_bytes_total";
Expand All @@ -45,8 +45,8 @@ pub fn init(settings: &Monitoring) -> Result<()> {
let builder = PrometheusBuilder::new()
.with_http_listener(addr)
.set_buckets_for_metric(
Matcher::Full(HTTP_REQUESTS_DURATION_SECONDS_HISTOGRAM.to_string()),
settings.http_requests_histogram_buckets(),
Matcher::Full(HTTP_REQUEST_DURATION_SECONDS_HISTOGRAM.to_string()),
settings.http_request_duration_buckets(),
)?;

info!("Starting monitoring server on {}", addr);
Expand All @@ -69,7 +69,7 @@ pub fn init(settings: &Monitoring) -> Result<()> {
"Number of events that could not be written to outputs by openwec"
);
describe_histogram!(
HTTP_REQUESTS_DURATION_SECONDS_HISTOGRAM,
HTTP_REQUEST_DURATION_SECONDS_HISTOGRAM,
Unit::Seconds,
"HTTP requests duration histogram"
);
Expand Down

0 comments on commit 236d6dc

Please sign in to comment.