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

custom metrics return function memory location #170

Open
AHVG opened this issue Feb 2, 2024 · 4 comments
Open

custom metrics return function memory location #170

AHVG opened this issue Feb 2, 2024 · 4 comments

Comments

@AHVG
Copy link

AHVG commented Feb 2, 2024

The entire metrics issue is implemented in another .py file and is imported when used in a flask route to check its metrics. Additionally, custom metrics were created using lambdas and these memory locations refer to them.

Below is an example.

flask_http_request_total{method="<function init.<locals>.<lambda> at 0x7f7657e81580>",path="<function init.<locals>.<lambda> at 0x7f7657e816c0>",path_without_uuid="<function init.<locals>.<lambda> at 0x7f7657e81760>",status="<function init.<locals>.<lambda> at 0x7f7657e81620>"} 0.0

@rycus86
Copy link
Owner

rycus86 commented Feb 2, 2024

Hi,
Can you give an example of the code as well? Once we can reproduce it, we can see how it can be fixed, I'm not immediately sure what info lambda functions provide there.

@AHVG
Copy link
Author

AHVG commented Feb 2, 2024

unfortunately I won't be able to send everything, just a small snippet, as the code is too big to fit here. But the idea is there, of how we are using the library. Furthermore, from what I can see, it is only the histogram that has this problem.

# metrics.py
from prometheus_flask_exporter import PrometheusMetrics
METRICS_PORT = os.environ.get('METRICS_PORT')


def init(flask_app: Flask):
    """
    Receives Flask instance to initialize metrics.
    """

    if not METRICS_PORT:
        logging.warning(f"Metrics could NOT enabled: port is missing ({METRICS_PORT})")
        return

    global prometheus_metrics
    global metrics_counter
    global metrics_histogram

    prometheus_metrics = PrometheusMetrics(flask_app, 
                                    excluded_paths=["^/static/.*$"], 
                                    export_defaults=False,
                                    path=None #disables '/metrics' route
                                    )
    metrics_counter = prometheus_metrics.counter(
        'flask_http_request_total', 
        'Total number of HTTP requests for all Flask requests.',
        labels={'method': lambda: request.method, 
                'status': lambda r: r.status_code, 
                'path': lambda: censor_uuids(request.path)})

    metrics_histogram = prometheus_metrics.histogram(
            'flask_http_request_duration_seconds', 
            'Flask HTTP request duration in seconds for all Flask requests.',
            labels={'method': lambda: request.method,
                    'status': lambda r: r.status_code, 
                    'path': lambda: censor_uuids(request.path)})

def record_metrics(func):
    """Decorator to record metrics of flask route if metrics are enabled"""

    if prometheus_metrics:
        @metrics_counter
        @metrics_histogram
        @wraps(func)
        def wrapper(*args, **kwargs):
            result = func(*args, **kwargs)
            return result
        
        return wrapper
    
    else:
        return func
# main.py
import metrics

from flask import Flask

application = Flask(...)
metrics.init(application)

@application.route('/how')
@metrics.record_metrics
def how():
    return render_template("how.html", is_logged=oidc.user_loggedin)

I understand that possibly with the information I have provided it may be difficult to find a solution, so there is no need to stress if it is a hassle.

@rycus86
Copy link
Owner

rycus86 commented Feb 2, 2024

Thanks a lot! This should help reproduce it!
I'll try to have a look next week.

@AHVG
Copy link
Author

AHVG commented Feb 2, 2024

I think I solved the problem by changing the version of prometheus-flask-exporter from 0.22.3 to 0.23.0, but I definitely don't understand why.

Thank you for the quick response and attention.

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