Recommended pattern for app metrics spread over multiple modules #1688
Replies: 2 comments 2 replies
-
I'm not an expert but I think the best practice is indeed to store the instruments in a structure, global if needed. Otherwise, you can do something like the following code, but then you end up with "stringly" typed counters and also paying the instantiation cost every time. let meter1 = opentelemetry::global::meter("test");
let counter1 = meter1.f64_counter("test1").init();
counter1.add(1.0, &[]);
let meter2 = opentelemetry::global::meter("test");
let counter2 = meter2.f64_counter("test1").init();
counter2.add(10.0, &[]);
/// test.test1 = 11.0 |
Beta Was this translation helpful? Give feedback.
-
Thanks @cijothomas , that's exactly what I thought. Do you believe a getter function at the meter level should exist to avoid using a global once_cell? Re-reading my initial post could lead to the interpretation that I want to initialize the counter/instrument multiple times. That was not my intent, I was thinking along the lines of: struct AppMetrics {
var my_cnt: Counter<u64>,
}
fn do_some_work() {
// no need to reference AppMetrics as a global structure or once_cell
opentelemetry::global::meter("app").get_u64_counter("my_cnt").add(1, &[]);
}
fn main() {
/* ...
initialize a Meter and set it as global
...
*/
let app_metrics = AppMetrics {
my_cnt: meter.u64_counter("my_cnt").init();
};
do_some_work();
/* ... */
} |
Beta Was this translation helpful? Give feedback.
-
I am conflicted about the correct approach to increment a metrics counter from a function inside of a separate module.
At the moment I am collecting all counters in a struct, which is instantiated inside of main() and gets passed into each function call. It would also be possible to initialize a once_cell to hold the struct as a global construct and reference it from inside each function. I am not quite sure if any of that is the correct approach.
Is there something similar to this:
wich would allow access to a counter (or any other metrics) without depending on the global struct holding them?
Any insight would be highly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions