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

Need help with a simple use case using Counter #43

Closed
julienMichaud opened this issue Dec 7, 2019 · 2 comments
Closed

Need help with a simple use case using Counter #43

julienMichaud opened this issue Dec 7, 2019 · 2 comments

Comments

@julienMichaud
Copy link

Hi,

Im very new to Flask and Python in general.
I would like to expose metrics of a simple app I created.

The thing is I don't understand how to increment the value of a Counter for example..

@app.route('/newaliment', methods=['GET', 'POST'])
@login_required
@metrics.counter('aliments', 'number_of_aliments_added')
def newaliment():
    form = AlimentsForm()
    if form.validate_on_submit():
        aliment = Aliment(aliment_name = form.aliment_name.data, description =form.description.data, author=current_user )
        db.session.add(aliment)
        db.session.commit()
        flash('Congratulations, you added a new aliment !')
        return redirect(url_for('newaliment'))
    return render_template('newAliment.html', title='NewAliment', form=form)

I initialize my counter but how can I increment it by "1" when an aliment is added in my database please ?

Thanks for your help and excuse my stupid question.

@rycus86
Copy link
Owner

rycus86 commented Dec 8, 2019

Hi! No need to apologize, this is a completely valid question!

This library provides decorators for your Flask managed endpoints, e.g. the @metrics.counter will count the number of times your /newaliment has been called. If you want to keep track of a different counter and manage it yourself, you could get a Counter from the underlying library like this (double-check if I haven't messed up the syntax) :

from prometheus_flask_exporter import Counter

my_counter = Counter('name', 'description')

    ...
    db.session.commit()
    my_counter.inc()

Does this help?

@julienMichaud
Copy link
Author

Make total sense, thanks for your help and best of luck with the fires in your country !

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