Skip to content

Feature request: Support setting functionName for cold-starts via middy #3642

Closed
@steven10172

Description

@steven10172

Use case

I would like to use the logMetrics middleware and specify an alternative function name. This would allow for names to be more static since CDK may sometimes replaces functions and the trailing characters are random.

Solution/User Experience

I would recommend updating the middy ExtraOptions interface to accept another property called functionName that if defined is used instead of request.context.functionName.

Example usage:

export const handler = middy()
  .use(injectLambdaContext(logger, { resetKeys: true }))
  .use(
    logMetrics(metrics, {
      captureColdStartMetric: true,
      defaultDimensions: { functionName: LambdaNames.GET_ASSET },
    }),
  )
  .handler(getAssetHandler);

Alternative solutions

Another solution would be to expose an object to the ExtraOptions interface that would allow attaching any dimensions to the cold start metric. This can then be used to override the function_name dimension, though caution would need to be exercised to ensure the value is not overwritten within the cold start check (ref)

Example usage:

export const handler = middy()
  .use(injectLambdaContext(logger, { resetKeys: true }))
  .use(
    logMetrics(metrics, {
      captureColdStartMetric: true,
      coldStartDimensions: { function_name: LambdaNames.GET_ASSET },
      defaultDimensions: { functionName: LambdaNames.GET_ASSET },
    }),
  )
  .handler(getAssetHandler);

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