-
I am working on genericizing some code commonly used between several of my microservices. This raised some issues, one of them being able to pass a metric's labels to a function. After looking through the API, particularly at Full code for the function: pub(crate) fn new_with_metric_name(
db: Database,
engine: EngineId,
metric_name: &'static str,
labels: Option<&[(SharedString, SharedString)]>,
) -> Self {
let histogram = match labels {
None => register_histogram!(metric_name),
Some(labels) => register_histogram!(metric_name, labels),
};
describe_histogram!(
metric_name,
metrics::Unit::Seconds,
"Time taken to insert data into the database"
);
Inserter { db, engine, histogram, _message_phantom: Default::default() }
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
That's certainly one way to do it, yeah. In this case, based on the given construction, both the metric name and labels are considered "non static", so it will end up calling pub(crate) fn new_with_metric_name<L>(
db: Database,
engine: EngineId,
metric_name: &'static str,
labels: Option<L>,
) -> Self
where
L: metrics::IntoLabels,
{
} |
Beta Was this translation helpful? Give feedback.
That's certainly one way to do it, yeah.
In this case, based on the given construction, both the metric name and labels are considered "non static", so it will end up calling
metrics::Key::from_parts(metric_name, labels)
to construct the key. That method depends on IntoLabels, which you could also use here to generalize your function, if you wanted to do so: