Nesting log_metrics only results in one set of metrics being logged
See original GitHub issueExpected Behaviour
When using two Metrics
objects and two nested call to log_metrics
I expected both sets of metrics to be serialized. The reason for two Metrics
object is that every metric is output twice, with a different set of dimensions. Using single_metric
for everything works but seems sub-optimal from a performance standpoint.
Example:
app = ... # FastAPI app
# API handler metrics with dimensions: Stage, service
api_metrics = Metrics()
api_metrics.set_default_dimensions(Stage=os.environ['STAGE'])
# API handler metrics with dimensions: Method, Resource, Stage, service
detailed_api_metrics = Metrics()
detailed_api_metrics.set_default_dimensions(Stage=os.environ['STAGE'])
handler = Mangum(app)
handler = logging.getLogger().inject_lambda_context(handler, clear_state=True)
# Add metrics last to properly flush metrics.
handler = api_metrics.log_metrics(handler, capture_cold_start_metric=True)
handler = detailed_api_metrics.log_metrics(handler)
Current Behaviour
Looking in the CloudWatch logs only the JSON for one set of metrics (api_metrics
above) is output.
Code snippet
from aws_lambda_powertools.metrics import Metrics, MetricUnit
# API handler metrics with dimensions: Stage, service
api_metrics = Metrics()
api_metrics.set_default_dimensions(Stage='Test')
# API handler metrics with dimensions: Method, Resource, Stage, service
detailed_api_metrics = Metrics()
detailed_api_metrics.set_default_dimensions(Stage='Test')
def handler(event, context):
detailed_api_metrics.add_dimension(
name='Method', value='GET')
detailed_api_metrics.add_dimension(
name='Resource', value='/some/path')
api_metrics.add_metric(name='Count',
unit=MetricUnit.Count, value=1)
detailed_api_metrics.add_metric(
name='Count', unit=MetricUnit.Count, value=1)
# Add metrics last to properly flush metrics.
handler = api_metrics.log_metrics(handler, capture_cold_start_metric=True)
handler = detailed_api_metrics.log_metrics(handler)
Possible Solution
No response
Steps to Reproduce
- Run the above code on
Lambda
. - Look in the logs.
- See that the JSON from one set of metrics is missing.
AWS Lambda Powertools for Python version
1.25.10
AWS Lambda function runtime
3.9
Packaging format used
PyPi
Debugging logs
I do see the two following things in the log:
/opt/python/aws_lambda_powertools/metrics/metrics.py:189: UserWarning: No metrics to publish, skipping
warnings.warn("No metrics to publish, skipping")
Note that the two Metrics
objects are used unconditionally right after each other so it would be weird for one to have metrics and one to have not.
{
"_aws": {
"Timestamp": 1666944145372,
"CloudWatchMetrics": [
{
"Namespace": "Benetics/Backend",
"Dimensions": [
[
"Stage",
"service"
]
],
"Metrics": [
{
"Name": "Count",
"Unit": "Count"
},
{
"Name": "4xx",
"Unit": "Count"
},
{
"Name": "5xx",
"Unit": "Count"
},
{
"Name": "Latency",
"Unit": "Milliseconds"
}
]
}
]
},
"Stage": "prod",
"service": "Api",
"Count": [
1,
1
],
"4xx": [
0,
0
],
"5xx": [
0,
0
],
"Latency": [
184,
184
]
}
Issue Analytics
- State:
- Created a year ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Log-based metrics overview - Google Cloud
Log -based metrics apply only within a single Google Cloud project. ... Logging provides a set of metrics for usage values such as...
Read more >Specification: Embedded metric format - Amazon CloudWatch
The CloudWatch embedded metric format is a JSON specification used to instruct CloudWatch Logs to automatically extract metric values embedded in structured ...
Read more >Structure Overview — PyTorch-Metrics 0.11.0 documentation
TorchMetrics is a Metrics API created for easy metric development and usage ... If such a group of metrics is found only one...
Read more >wandb.log - Documentation
Logging nested metrics is encouraged and is supported in the W&B UI. ... wandb.log is not intended to be called more than a...
Read more >MLflow Tracking — MLflow 2.0.1 documentation
The MLflow Tracking component is an API and UI for logging parameters, code versions, metrics, ... Automatic logging allows you to log metrics,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@heitorlessa thanks! I’ve now integrated 2.2.0 in our backend and things seem to be working well so far!
@tibbe this is now available as part of v2.2.0 release (Lambda Layer v13): https://github.com/awslabs/aws-lambda-powertools-python/releases/tag/v2.2.0
Let me know if that doesn’t address your original ask.