How to spread metrics throughout the codebase?
Opened this issue · 1 comments
It's not very clear to me the best practice for plumbing the metrics throughout my codebase? The example just uses a simple main function; however, my use case is much more complex with multiple different classes using composition.
Basically How do I avoid doing this:
#include <iostream>
#include <prometheus/registry.h>
#include <prometheus/counter.h>
using namespace prometheus;
class FirstClass {
SecondClass sec_class();
void func(Family<Counter>& counter){
sec_class.printVal(counter);
}
}
class SecondClass{
void printVal(Family<Counter>& counter){
counter.Add({{"label_one", "val"}}).Increment();
cout<<"Hello"<<endl;
}
}
int main(){
auto registry = std::make_shared<prometheus::Registry>();
auto& counter = BuildCounter().Name("metric").Help("str").Register(*registry);
FirstClass first_class();
first_class.func(counter);
return 0;
}
If I needed to increment a counter in one of the functions in those classes how do I sanely pass down that Counter Object without having to modify every single constructor/function declaration to accept a bunch of Counter/Gauge/etc.. metrics? Since global variables are discouraged, I honestly don't see a realistic way to do this.
I've thought about member reference variables and passing the objects through constructors but that does not work either since the registry owns the metrics objects and I would still have to change all constructor signatures. and using a map does not help with Family is templated to use each different metric. Therefore if a class needed both Guage and Counter they would have to stored in separate maps and then dependency injected into the constructor.
any guidance would be very appreciated.
I also had the same issue for one of our projects. What i did was implement my own Collectable Class which inherits from ::prometheus::Collactable
in this i create the MetricFamily
Objects like Gauge, Counter
etc.
Inside my Classes i manage things i want to counter as simple ìntsor
doubles` and then create Metrics from them.
If i had the time next week i can post some example.