Skip to content

Commit

Permalink
valid metrics names
Browse files Browse the repository at this point in the history
  • Loading branch information
Gil Mizrahi committed Oct 27, 2023
1 parent 930b968 commit 6a1b90c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
3 changes: 1 addition & 2 deletions rust-connector-sdk/src/default_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ where
.unwrap();

let mut metrics_registry = Registry::new();
let metrics =
metrics::Metrics::initialize(&C::connector_name(), &mut metrics_registry).unwrap();
let metrics = metrics::Metrics::initialize(C::connector_name(), &mut metrics_registry).unwrap();
let state = C::try_init_state(&configuration, &mut metrics_registry)
.await
.unwrap();
Expand Down
17 changes: 16 additions & 1 deletion rust-connector-sdk/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,24 @@ pub struct StatusCodeMetrics {
impl Metrics {
/// Set up counters and gauges used to produce Prometheus metrics
pub fn initialize(
connector_name: &str,
connector_name: String,
metrics_registry: &mut prometheus::Registry,
) -> Result<Self, prometheus::Error> {
// Transform the connector name so it is a valid prometheus metric name
// <https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels>
let connector_name: String = connector_name
.chars()
.filter_map(|c| {
if c == '-' {
Some('_')
} else if c.is_ascii_alphanumeric() {
Some(c)
} else {
None
}
})
.collect();

let total_200 = add_int_counter_metric(
metrics_registry,
format!("{}_status_code_200_total_count", connector_name).as_str(),
Expand Down

0 comments on commit 6a1b90c

Please sign in to comment.