Skip to content

Feature request: Avoid flushing metrics when no metrics to flush #3023

Closed
@bml1g12

Description

@bml1g12

Use case

We are using

    "@middy/core": "^4.0.2",
    "@aws-lambda-powertools/metrics": "^1.0.1",

Metrics with this setup:

const log = new Logger({
  // POWERTOOLS_LOG_LEVEL environment variable sets log level
  serviceName: config.serviceName,
  persistentLogAttributes: {
    release: config.releaseVersion,
    environment: config.stage,
  },
})

const metrics = new Metrics({
  serviceName: config.serviceName,
  namespace: "xxx",
  defaultDimensions: {
    aws_account_id: config.awsAccountId,
    aws_region: config.region,
  },
})

const tracer = new Tracer({ serviceName: config.serviceName })
// You can use tracer.provider attribute to access all methods provided by the AWS X-Ray SDK.
// This is useful when you need a feature available in X-Ray that is not available in the
// Tracer utility, for example, SQL queries tracing, or a custom logger.
tracer.provider.setLogger(log)

const withMiddy = (handler: Handler) =>
  middy(handler)
    .use(captureLambdaHandler(tracer))
    .use(logMetrics(metrics, { captureColdStartMetric: false }))

And if we do not log any metrics cloudwatch logs report what I think is a redundant log that makes logs a little harder to read through and of course expends a tiny amount of money to emit:


2024-09-04T12:33:44.468Z
{
    "_aws": {
        "Timestamp": 1725453224468,
        "CloudWatchMetrics": [
            {
                "Namespace": "xxx",
                "Dimensions": [
                    [
                        "service",
                        "aws_account_id",
                        "aws_region"
                    ]
                ],
                "Metrics": []
            }
        ]
    },
    "service": "xxx",
    "aws_account_id": "xxx",
    "aws_region": "ap-northeast-1"
}

Solution/User Experience

I believe the AWS Powertools Library for Python does not produce such empty logs, I think the ideal behaviour would be to omit these logs entirely if there's no metrics to log?

Alternative solutions

We could of course omit including the library entirely for each lambda, but given the middleware is often used to apply generic boilerplate to all lambdas in a repo, I don't think that would be appropriate if we wanted to only report metrics for some lambdas.

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilitymetricsThis item relates to the Metrics Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions