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
Prev Previous commit
Next Next commit
Work on fixing tests, remove const_format dep
  • Loading branch information
emschwartz committed May 16, 2023
commit 9817601e085e28b37ae329c7295cbc1d2e947e01
5 changes: 1 addition & 4 deletions autometrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ readme = "README.md"
default = ["opentelemetry"]
metrics = ["dep:metrics"]
opentelemetry = ["opentelemetry_api"]
prometheus = ["const_format", "dep:prometheus", "once_cell"]
prometheus = ["dep:prometheus", "once_cell"]
prometheus-client = ["dep:prometheus-client", "once_cell"]
prometheus-exporter = [
"metrics-exporter-prometheus",
Expand Down Expand Up @@ -45,9 +45,6 @@ opentelemetry-prometheus = { version = "0.11", optional = true }
opentelemetry_sdk = { version = "0.18", default-features = false, features = ["metrics"], optional = true }
prometheus = { version = "0.13", default-features = false, optional = true }

# Used for prometheus feature
const_format = { version = "0.2", features = ["rust_1_51"], optional = true }

# Used for prometheus-client feature
prometheus-client = { version = "0.21.1", optional = true }

Expand Down
10 changes: 9 additions & 1 deletion autometrics/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ pub const HISTOGRAM_NAME: &str = "function.calls.duration";
pub const GAUGE_NAME: &str = "function.calls.concurrent";
pub const BUILD_INFO_NAME: &str = "build_info";

// Prometheus-flavored metric names
pub const COUNTER_NAME_PROMETHEUS: &str = "function_calls_count";
pub const HISTOGRAM_NAME_PROMETHEUS: &str = "function_calls_duration";
pub const GAUGE_NAME_PROMETHEUS: &str = "function_calls_concurrent";

// Descriptions
pub const COUNTER_DESCRIPTION: &str = "Autometrics counter for tracking function calls";
pub const HISTOGRAM_DESCRIPTION: &str = "Autometrics histogram for tracking function call duration";
Expand All @@ -19,8 +24,11 @@ pub const RESULT_KEY: &'static str = "result";
pub const OK_KEY: &'static str = "ok";
pub const ERROR_KEY: &'static str = "error";
pub const OBJECTIVE_NAME: &'static str = "objective.name";
pub const OBJECTIVE_NAME_PROMETHEUS: &'static str = "objective_name";
pub const OBJECTIVE_PERCENTILE: &'static str = "objective.percentile";
pub const OBJECTIVE_LATENCY_THRESHOLD: &'static str = "objective.latency_threshold";
pub const OBJECTIVE_PERCENTILE_PROMETHEUS: &'static str = "objective_percentile";
pub const OBJECTIVE_LATENCY_THRESHOLD: &'static str = "objective.latency.threshold";
pub const OBJECTIVE_LATENCY_THRESHOLD_PROMETHEUS: &'static str = "objective_latency_threshold";
pub const VERSION_KEY: &'static str = "version";
pub const COMMIT_KEY: &'static str = "commit";
pub const BRANCH_KEY: &'static str = "branch";
4 changes: 2 additions & 2 deletions autometrics/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub type ResultAndReturnTypeLabels = (&'static str, Option<&'static str>);
derive(EncodeLabelSet, Debug, Clone, PartialEq, Eq, Hash)
)]
pub struct BuildInfoLabels {
pub(crate) version: &'static str,
pub(crate) commit: &'static str,
pub(crate) branch: &'static str,
pub(crate) commit: &'static str,
pub(crate) version: &'static str,
}

impl BuildInfoLabels {
Expand Down
9 changes: 1 addition & 8 deletions autometrics/src/tracker/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ use prometheus::{
};
use std::{sync::Once, time::Instant};

const COUNTER_NAME_PROMETHEUS: &str = str_replace!(COUNTER_NAME, ".", "_");
const HISTOGRAM_NAME_PROMETHEUS: &str = str_replace!(HISTOGRAM_NAME, ".", "_");
const GAUGE_NAME_PROMETHEUS: &str = str_replace!(GAUGE_NAME, ".", "_");
const OBJECTIVE_NAME_PROMETHEUS: &str = str_replace!(OBJECTIVE_NAME, ".", "_");
const OBJECTIVE_PERCENTILE_PROMETHEUS: &str = str_replace!(OBJECTIVE_PERCENTILE, ".", "_");
const OBJECTIVE_LATENCY_PROMETHEUS: &str = str_replace!(OBJECTIVE_LATENCY_THRESHOLD, ".", "_");

static SET_BUILD_INFO: Once = Once::new();

static COUNTER: Lazy<IntCounterVec> = Lazy::new(|| {
Expand Down Expand Up @@ -54,7 +47,7 @@ static HISTOGRAM: Lazy<HistogramVec> = Lazy::new(|| {
MODULE_KEY,
OBJECTIVE_NAME_PROMETHEUS,
OBJECTIVE_PERCENTILE_PROMETHEUS,
OBJECTIVE_LATENCY_PROMETHEUS
OBJECTIVE_LATENCY_THRESHOLD_PROMETHEUS
]
)
.expect("Failed to register function_calls_duration histogram")
Expand Down
14 changes: 11 additions & 3 deletions autometrics/src/tracker/prometheus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ static REGISTRY_AND_METRICS: Lazy<(Registry, Metrics)> = Lazy::new(|| {
let mut registry = <Registry>::default();

let counter = Family::<CounterLabels, Counter>::default();
registry.register(COUNTER_NAME, COUNTER_DESCRIPTION, counter.clone());
registry.register(
COUNTER_NAME_PROMETHEUS,
COUNTER_DESCRIPTION,
counter.clone(),
);

let histogram = Family::<HistogramLabels, Histogram>::new_with_constructor(|| {
Histogram::new(HISTOGRAM_BUCKETS.into_iter())
});
registry.register(HISTOGRAM_NAME, HISTOGRAM_DESCRIPTION, histogram.clone());
registry.register(
HISTOGRAM_NAME_PROMETHEUS,
HISTOGRAM_DESCRIPTION,
histogram.clone(),
);

let gauge = Family::<GaugeLabels, Gauge>::default();
registry.register(GAUGE_NAME, GAUGE_DESCRIPTION, gauge.clone());
registry.register(GAUGE_NAME_PROMETHEUS, GAUGE_DESCRIPTION, gauge.clone());

let build_info = Family::<BuildInfoLabels, Gauge>::default();
registry.register(BUILD_INFO_NAME, BUILD_INFO_DESCRIPTION, build_info.clone());
Expand Down
36 changes: 20 additions & 16 deletions autometrics/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn single_function() {
let metrics = autometrics::encode_global_metrics().unwrap();
println!("{}", metrics);
let call_count_metric: Regex = Regex::new(
r#"function_calls_count\{\S*function="hello_world"\S*module="integration_test"\S*\} 2"#,
r#"function_calls_count(?:_total)?\{\S*function="hello_world"\S*module="integration_test"\S*\} 2"#,
)
.unwrap();
let duration_metric: Regex = Regex::new(
Expand Down Expand Up @@ -51,9 +51,9 @@ fn impl_block() {

let metrics = autometrics::encode_global_metrics().unwrap();
let test_fn_count: Regex =
Regex::new(r#"function_calls_count\{\S*function="test_fn"\S*\} 1"#).unwrap();
Regex::new(r#"function_calls_count(?:_total)?\{\S*function="test_fn"\S*\} 1"#).unwrap();
let test_method_count: Regex =
Regex::new(r#"function_calls_count\{\S*function="test_method"\S*\} 1"#).unwrap();
Regex::new(r#"function_calls_count(?:_total)?\{\S*function="test_method"\S*\} 1"#).unwrap();
let test_fn_duration: Regex =
Regex::new(r#"function_calls_duration_bucket\{\S*function="test_fn"\S*\}"#).unwrap();
let test_method_duration: Regex =
Expand Down Expand Up @@ -83,13 +83,15 @@ fn result() {
result_fn(false).ok();

let metrics = autometrics::encode_global_metrics().unwrap();
let error_count: Regex =
Regex::new(r#"function_calls_count\{\S*function="result_fn"\S*result="error"\S*\} 2"#)
.unwrap();
let error_count: Regex = Regex::new(
r#"function_calls_count(?:_total)?\{\S*function="result_fn"\S*result="error"\S*\} 2"#,
)
.unwrap();
assert!(error_count.is_match(&metrics));
let ok_count: Regex =
Regex::new(r#"function_calls_count\{\S*function="result_fn"\S*result="ok"\S*\} 1"#)
.unwrap();
let ok_count: Regex = Regex::new(
r#"function_calls_count(?:_total)?\{\S*function="result_fn"\S*result="ok"\S*\} 1"#,
)
.unwrap();
assert!(ok_count.is_match(&metrics));
}

Expand All @@ -106,9 +108,10 @@ fn ok_if() {
ok_if_fn();

let metrics = autometrics::encode_global_metrics().unwrap();
let error_count: Regex =
Regex::new(r#"function_calls_count\{\S*function="ok_if_fn"\S*result="error"\S*\} 1"#)
.unwrap();
let error_count: Regex = Regex::new(
r#"function_calls_count(?:_total)?\{\S*function="ok_if_fn"\S*result="error"\S*\} 1"#,
)
.unwrap();
assert!(error_count.is_match(&metrics));
}

Expand All @@ -125,9 +128,10 @@ fn error_if() {
error_if_fn();

let metrics = autometrics::encode_global_metrics().unwrap();
let error_count: Regex =
Regex::new(r#"function_calls_count\{\S*function="error_if_fn"\S*result="error"\S*\} 1"#)
.unwrap();
let error_count: Regex = Regex::new(
r#"function_calls_count(?:_total)?\{\S*function="error_if_fn"\S*result="error"\S*\} 1"#,
)
.unwrap();
assert!(error_count.is_match(&metrics));
}

Expand All @@ -148,7 +152,7 @@ fn caller_label() {

let metrics = autometrics::encode_global_metrics().unwrap();
let call_count: Regex = Regex::new(
r#"function_calls_count\{\S*caller="function_1"\S*function="function_2"\S*\} 1"#,
r#"function_calls_count(?:_total)?\{\S*caller="function_1"\S*function="function_2"\S*\} 1"#,
)
.unwrap();
assert!(call_count.is_match(&metrics));
Expand Down