awslabs / aws-embedded-metrics-python

Amazon CloudWatch Embedded Metric Format Client Library
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html
Apache License 2.0
202 stars 34 forks source link

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

Open SamStephens opened 1 year ago

SamStephens commented 1 year ago

There's some discussion of this in https://github.com/awslabs/aws-embedded-metrics-python/issues/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 commented 8 months ago

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