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
Option to add new metrics or new lable #34
Comments
Hi Idan, You can add new metrics easily, but you can't add labels to the default metrics.
In this case, the endpoint will be tracked with Adding more labels to the built-in metrics is not currently supported. Does this help? |
Thanks for the response.
|
Yeah, I think it might be nice for this library to support this use case, where you either want to extend the default metrics, or apply a metric collector to each of the endpoints. |
exactly, it would be great... |
I've added support for this now. You could either add the same metric to all endpoints: app = Flask(__name__)
metrics = PrometheusMetrics(app)
@app.route('/simple')
def simple_get():
pass
metrics.register_default(
metrics.counter(
'by_path_counter', 'Request count by request paths',
labels={'path': lambda: request.path}
)
) Or just add the same metric to some of the endpoints (not necessarily all of them) : app = Flask(__name__)
metrics = PrometheusMetrics(app)
by_path_counter = metrics.counter(
'by_path_counter', 'Request count by request paths',
labels={'path': lambda: request.path}
)
@app.route('/simple')
@by_path_counter
def simple_get():
pass
@app.route('/plain')
@by_path_counter
def plain():
pass
@app.route('/not/tracked/by/path')
def not_tracked_by_path():
pass Let me know if this helps! |
Thanks for the fix, but i'm getting this error when trying your example:
|
@BuSHari could you provide a small example that fails to test with? |
Is this helps? user_info = metrics.register_default(
metrics.counter(
'flask_http_user_info', 'Get mote user info',
labels={'path': lambda: request.path}
)
)
@api.route('/', methods=['GET', 'POST'])
@user_info
class Serial(Resource):
@api.login_required(oauth_scopes=['serials:read'])
def get(self):
pass |
And this is an example if i try it for all endpoints: from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics
from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics
# Check if app run trough gunicorn
is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")
if is_gunicorn:
metrics = GunicornPrometheusMetrics(app=None)
else:
metrics = PrometheusMetrics(app=None)
def create_app(flask_config_name=None, **kwargs):
import threading
threading.stack_size(2 * 1024 * 1024)
app = Flask(__name__, **kwargs)
# Activate metrics '/metrics'
metrics.init_app(app)
# static information as metric
metrics.info('app_info', 'Application info', version='1.0.0')
gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
env_flask_config_name = os.getenv('FLASK_CONFIG')
with app.app_context():
from . import extensions
extensions.init_app(app)
from .templates.db_admin import add_db_admin_cmds
add_db_admin_cmds(app)
from . import modules
modules.init_app(app)
metrics.register_default(
metrics.counter(
'by_path_counter', 'Request count by request paths',
labels={'path': lambda: request.path}
)
)
return app |
Yep, so the
Your second example with Gunicorn should work though. |
ok thank you. Thank you... |
All right, I'll have a look later if I can. |
thank you, I use restplus |
OK, it should be fixed in version See https://github.com/rycus86/prometheus_flask_exporter/blob/master/examples/restplus-default-metrics/server.py for an example, though it's pretty much what you had above, your example should now work without any changes. Let me know how this works out for you! |
Great! |
Hello,
I there an option to add a new metrics or new lable to the existing metrics?
for example, i want to add a lable with the requester ip to the default metric
or add a new metric that contain the location data of the requester ip.
I tried to use the group by, but it overwrite the path lable, and i cant add more than one lable.
Thanks,
Idan
The text was updated successfully, but these errors were encountered: