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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add metrics #2

Closed
pawamoy opened this issue Jul 27, 2020 · 3 comments
Closed

How to add metrics #2

pawamoy opened this issue Jul 27, 2020 · 3 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@pawamoy
Copy link

pawamoy commented Jul 27, 2020

Hello! Thanks for the projet 馃檪

I read the README and I'm left wondering how I can add metrics to the already defined ones. Is this even possible yet? For example, I'd like to add the average transfered data size per request/response. How would I add this into the instrumentator?

@trallnag
Copy link
Owner

trallnag commented Jul 27, 2020

Do you want the transfered data metric just for a few specific endpoints or all endpoints transparently?

Option 1: Instrument the specific endpoints manually by directly using the Prometheus library.

Option 2: Not supported. I could put it on my bucket list. Though I'm not sure if I would hardcode the new metrics or do something like @rycus86 has done in his package for instrumenting Flask apps https://github.com/rycus86/prometheus_flask_exporter.

And thanks

@trallnag trallnag added the question Further information is requested label Jul 27, 2020
@pawamoy
Copy link
Author

pawamoy commented Jul 27, 2020

Thanks for the quick answer!

Do you want the transfered data metric just for a few specific endpoints or all endpoints transparently?

Depends! It was just an example 馃檪 Could be both.

Oh yes, it seems prometheus_flask_exporter allows to do what I'm looking for.

So I would say: don't hardcode it! Instead, provide a way for users to add their own custom, exported metrics.

@trallnag
Copy link
Owner

trallnag commented Aug 15, 2020

@pawamoy you can now select from a few already existing instrumentations or add your own. Check the updated README.md for more info.

Recording size of requests / responses:

instrumentator.add(
    metrics.request_size(
        should_include_handler=False,
        should_include_method=False,
        should_include_status=False,
    )
).add(
    metrics.response_size(
        should_include_handler=False,
        should_include_method=False,
        should_include_status=False,
    )
)

Or creating your own:

def http_requested_languages_total() -> Callable[[Info], None]:
    METRIC = Counter(
        "http_requested_languages_total", 
        "Number of times a certain language has been requested.", 
        labelnames=("langs",)
    )

    def instrumentation(info: Info) -> None:
        langs = set()
        lang_str = info.request.headers["Accept-Language"]
        for element in lang_str.split(",")
            element = element.split(";")[0].strip().lower()
            langs.add(element)
        for language in langs:
            METRIC.labels(language).inc()

    return instrumentation

instrumentator.add(http_requested_languages_total())

@trallnag trallnag self-assigned this Aug 15, 2020
@trallnag trallnag added the enhancement New feature or request label Aug 15, 2020
@trallnag trallnag pinned this issue Aug 27, 2020
@trallnag trallnag reopened this Oct 7, 2020
@trallnag trallnag closed this as completed Oct 7, 2020
@trallnag trallnag unpinned this issue Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants