Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix generated queries to work with _total suffix
  • Loading branch information
emschwartz committed May 26, 2023
commit 0452940c3addbf13717c441755dacf8b35aa04cb
33 changes: 15 additions & 18 deletions autometrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use syn::{parse_macro_input, ImplItem, ItemFn, ItemImpl, Result, ReturnType, Typ
mod parse;
mod result_labels;

const COUNTER_NAME_PROMETHEUS: &str = "function_calls_count";
const HISTOGRAM_BUCKET_NAME_PROMETHEUS: &str = "function_calls_duration_bucket";
const GAUGE_NAME_PROMETHEUS: &str = "function_calls_concurrent";
const ADD_BUILD_INFO_LABELS: &str =
"* on (instance, job) group_left(version, commit) last_over_time(build_info[1s])";

Expand Down Expand Up @@ -234,23 +231,23 @@ fn instrument_impl_block(args: &AutometricsArgs, mut item: ItemImpl) -> Result<T
/// Create Prometheus queries for the generated metric and
/// package them up into a RustDoc string
fn create_metrics_docs(prometheus_url: &str, function: &str, track_concurrency: bool) -> String {
let request_rate = request_rate_query(COUNTER_NAME_PROMETHEUS, "function", function);
let request_rate = request_rate_query("function", function);
let request_rate_url = make_prometheus_url(
prometheus_url,
&request_rate,
&format!(
"Rate of calls to the `{function}` function per second, averaged over 5 minute windows"
),
);
let callee_request_rate = request_rate_query(COUNTER_NAME_PROMETHEUS, "caller", function);
let callee_request_rate = request_rate_query("caller", function);
let callee_request_rate_url = make_prometheus_url(prometheus_url, &callee_request_rate, &format!("Rate of calls to functions called by `{function}` per second, averaged over 5 minute windows"));

let error_ratio = &error_ratio_query(COUNTER_NAME_PROMETHEUS, "function", function);
let error_ratio = &error_ratio_query("function", function);
let error_ratio_url = make_prometheus_url(prometheus_url, error_ratio, &format!("Percentage of calls to the `{function}` function that return errors, averaged over 5 minute windows"));
let callee_error_ratio = &error_ratio_query(COUNTER_NAME_PROMETHEUS, "caller", function);
let callee_error_ratio = &error_ratio_query("caller", function);
let callee_error_ratio_url = make_prometheus_url(prometheus_url, callee_error_ratio, &format!("Percentage of calls to functions called by `{function}` that return errors, averaged over 5 minute windows"));

let latency = latency_query(HISTOGRAM_BUCKET_NAME_PROMETHEUS, "function", function);
let latency = latency_query("function", function);
let latency_url = make_prometheus_url(
prometheus_url,
&latency,
Expand All @@ -259,7 +256,7 @@ fn create_metrics_docs(prometheus_url: &str, function: &str, track_concurrency:

// Only include the concurrent calls query if the user has enabled it for this function
let concurrent_calls_doc = if track_concurrency {
let concurrent_calls = concurrent_calls_query(GAUGE_NAME_PROMETHEUS, "function", function);
let concurrent_calls = concurrent_calls_query("function", function);
let concurrent_calls_url = make_prometheus_url(
prometheus_url,
&concurrent_calls,
Expand Down Expand Up @@ -302,20 +299,20 @@ fn make_prometheus_url(url: &str, query: &str, comment: &str) -> String {
url
}

fn request_rate_query(counter_name: &str, label_key: &str, label_value: &str) -> String {
format!("sum by (function, module, commit, version) (rate({counter_name}{{{label_key}=\"{label_value}\"}}[5m]) {ADD_BUILD_INFO_LABELS})")
fn request_rate_query(label_key: &str, label_value: &str) -> String {
format!("sum by (function, module, commit, version) (rate({{__name__=~\"function_calls(_count)?(_total)?\",{label_key}=\"{label_value}\"}}[5m]) {ADD_BUILD_INFO_LABELS})")
}

fn error_ratio_query(counter_name: &str, label_key: &str, label_value: &str) -> String {
let request_rate = request_rate_query(counter_name, label_key, label_value);
format!("(sum by (function, module, commit, version) (rate({counter_name}{{{label_key}=\"{label_value}\",result=\"error\"}}[5m]) {ADD_BUILD_INFO_LABELS}))
fn error_ratio_query(label_key: &str, label_value: &str) -> String {
let request_rate = request_rate_query(label_key, label_value);
format!("(sum by (function, module, commit, version) (rate({{__name__=~\"function_calls(_count)?(_total)?\",{label_key}=\"{label_value}\",result=\"error\"}}[5m]) {ADD_BUILD_INFO_LABELS}))
/
({request_rate})",)
}

fn latency_query(bucket_name: &str, label_key: &str, label_value: &str) -> String {
fn latency_query(label_key: &str, label_value: &str) -> String {
let latency = format!(
"sum by (le, function, module, commit, version) (rate({bucket_name}{{{label_key}=\"{label_value}\"}}[5m]) {ADD_BUILD_INFO_LABELS})"
"sum by (le, function, module, commit, version) (rate(function_calls_duration_bucket{{{label_key}=\"{label_value}\"}}[5m]) {ADD_BUILD_INFO_LABELS})"
);
format!(
"label_replace(histogram_quantile(0.99, {latency}), \"percentile_latency\", \"99\", \"\", \"\")
Expand All @@ -324,6 +321,6 @@ label_replace(histogram_quantile(0.95, {latency}), \"percentile_latency\", \"95\
)
}

fn concurrent_calls_query(gauge_name: &str, label_key: &str, label_value: &str) -> String {
format!("sum by (function, module, commit, version) ({gauge_name}{{{label_key}=\"{label_value}\"}} {ADD_BUILD_INFO_LABELS})")
fn concurrent_calls_query(label_key: &str, label_value: &str) -> String {
format!("sum by (function, module, commit, version) (function_calls_concurrent{{{label_key}=\"{label_value}\"}} {ADD_BUILD_INFO_LABELS})")
}
2 changes: 1 addition & 1 deletion examples/exemplars-tracing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn main() {
let server = axum::Server::bind(&addr);

println!("\nVisit the following URL to see one of the charts along with the exemplars:");
println!("http://localhost:9090/graph?g0.expr=%23%20Rate%20of%20calls%20to%20the%20%60outer_function%60%20function%20per%20second%2C%20averaged%20over%205%20minute%20windows%0A%0Asum%20by%20(function%2C%20module%2C%20commit%2C%20version)%20(rate(function_calls_count_total%7Bfunction%3D%22outer_function%22%7D%5B5m%5D)%20*%20on%20(instance%2C%20job)%20group_left(version%2C%20commit)%20last_over_time(build_info%5B1s%5D))&g0.tab=0&g0.stacked=0&g0.show_exemplars=1&g0.range_input=1h");
println!("http://localhost:9090/graph?g0.expr=%23%20Rate%20of%20calls%20to%20the%20%60outer_function%60%20function%20per%20second%2C%20averaged%20over%205%20minute%20windows%0A%0Asum%20by%20(function%2C%20module%2C%20commit%2C%20version)%20(rate(%7B__name__%3D~%22function_calls(_count)%3F(_total)%3F%22%2Cfunction%3D%22outer_function%22%7D%5B5m%5D)%20*%20on%20(instance%2C%20job)%20group_left(version%2C%20commit)%20last_over_time(build_info%5B1s%5D))&g0.tab=0&g0.stacked=0&g0.show_exemplars=1&g0.range_input=1h");

server
.serve(app.into_make_service())
Expand Down