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

Is create_metrics_logger a part of the public API of this package? #111

Open
SamStephens opened this issue Sep 14, 2023 · 1 comment
Open

Comments

@SamStephens
Copy link

SamStephens commented Sep 14, 2023

There's some discussion of this in #71, but I wanted to ask the question more directly.

The reason I ask is that there are metric logging scenarios where there is no single method available to apply the @metric_scope annotation to.

An example, and the problem I am specifically looking at, is emitting per request metrics from a Flask application. Unless I'm missing something, there is no singular method you can apply the @metric_scope annotation to in Flask.

Instead, you need to create a metric logger in a before_request hook, and then emit metrics in a teardown_request hook, with the metric logger then available to you in the request itself to add additional metrics if desired.

@app.before_request
def add_metric_logger_to_request_context():
    app.logger.info('add_metric_logger_to_request_context')
    g.metric_logger = create_metrics_logger()
    g.start_time = datetime.datetime.now(datetime.timezone.utc)


@app.teardown_request
async def decorate_and_emit_metrics(error):
    app.logger.info('decorate_and_emit_metrics')
    elapsed = (datetime.datetime.now(datetime.timezone.utc) - g.start_time).total_seconds()

    g.metric_logger.put_metric("Elapsed", elapsed, "Seconds")

    await g.metric_logger.flush()

Is this a supported way to use the package, or am I setting my self up for future pain by depending on the internals of the package?

@Jaygee-jargon
Copy link

I don't suppose you ever came up with a solution for this?

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