Description
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
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Java, TypeScript