Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to spread metrics throughout the codebase? #643

Open
tjweaver opened this issue Mar 15, 2023 · 1 comment
Open

How to spread metrics throughout the codebase? #643

tjweaver opened this issue Mar 15, 2023 · 1 comment

Comments

@tjweaver
Copy link

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.

@KevDi
Copy link

KevDi commented Apr 1, 2023

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 ìntsordoubles` and then create Metrics from them.

If i had the time next week i can post some example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants