Skip to content

Feature request: Add A method to manually flush metrics #2109

Description

@JeffDGAWS

Use case

When not running within a standard Lambda handler (in my case, running within the Lambda Web Adapter), the @log_metrics decorator does not function. It is expecting the standard Lambda event,context parameters to be passed in.

Would be helpful to have a function that you could manually call before returning from a function to flush the metrics out to stdout so that they get logged.

Solution/User Experience

Based on the decorator code, I built the is as a function in my code that flushes the metrics, but would be more resilient if it were in the class.

def flush_metrics(a=None): m = metrics.serialize_metric_set() metrics.clear_metrics() print(json.dumps(m, separators=(",", ":"))) return a

Note the parameter (a) there is not used in the function at all, and is simply returned to the caller. This was done so that I could wrap the "flush" and return into one line such as:

return flush_metrics({"message": f"Count: {count}"})

Alternative solutions

No response

Acknowledgment

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions