Is create_metrics_logger a part of the public API of this package?
SamStephens opened this issue · 1 comments
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?
I don't suppose you ever came up with a solution for this?