Skip to content

feat(event-handler): add metrics middleware for HTTP routes#5086

Open
svozza wants to merge 12 commits intomainfrom
event-handler/tracer-middleware
Open

feat(event-handler): add metrics middleware for HTTP routes#5086
svozza wants to merge 12 commits intomainfrom
event-handler/tracer-middleware

Conversation

@svozza
Copy link
Contributor

@svozza svozza commented Mar 12, 2026

Summary

Add metrics middleware for emitting per-request metrics from the Event Handler HTTP router using the Powertools Metrics utility.

Changes

  • Add route field to RequestContext populated with the matched route pattern (e.g. GET /users/:id), or null when no route matches
  • Create metrics middleware that emits latency, fault, and error metrics per request
  • Use the matched route as a metric dimension (uses NOT_FOUND when no route matches to prevent dimension explosion)
  • Make RequestContext a discriminated union on responseType, enabling type-safe access to event-specific properties (e.g. requestContext.requestId on API Gateway events but not ALB)
  • Add request metadata to metrics:
    • httpMethod and path for all requests
    • statusCode for all requests
    • ipAddress from requestContext.identity.sourceIp (V1), requestContext.http.sourceIp (V2), or X-Forwarded-For header (ALB fallback)
    • userAgent from request headers
    • apiGwRequestId and apiGwApiId for API Gateway V1 and V2 events
    • apiGwExtendedRequestId for API Gateway V1 events when available
  • Remove unused getResponseType function (replaced by type guard branching in #buildRequestContext)
  • Add @aws-lambda-powertools/metrics as an optional peer dependency, following the same pattern as the tracer middleware
  • Expose the metrics middleware as a subpath export to avoid requiring the metrics dependency
  • Unit tests are covered

Issue number: closes #4602


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Mar 12, 2026
@pull-request-size pull-request-size bot added size/XL PRs between 500-999 LOC, often PRs that grown with feedback and removed size/L PRs between 100-499 LOC labels Mar 12, 2026
Copy link

@perpil perpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x-forwarded-for has some gotchas

@svozza svozza self-assigned this Mar 13, 2026
@svozza svozza marked this pull request as ready for review March 13, 2026 09:38
@svozza svozza requested a review from sdangol March 13, 2026 09:39
@svozza svozza changed the title feat(event-handler): add metrics middleware for HTTP routes feat(event-handler): add metrics middleware for HTTP routes Mar 13, 2026
@svozza svozza force-pushed the event-handler/tracer-middleware branch from 8c628c3 to bf53f28 Compare March 18, 2026 21:13
@svozza svozza force-pushed the event-handler/tracer-middleware branch from 7189189 to edceaf8 Compare March 20, 2026 11:45
@sonarqubecloud
Copy link

@svozza svozza changed the title feat(event-handler): add metrics middleware for HTTP routes feat(event-handler): add metrics middleware for HTTP routes Mar 20, 2026
@svozza svozza requested a review from sdangol March 20, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL PRs between 500-999 LOC, often PRs that grown with feedback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Event Handler Request Logging

3 participants