feat(event-handler): add metrics middleware for HTTP routes#5086
Open
feat(event-handler): add metrics middleware for HTTP routes#5086
Conversation
2 tasks
perpil
reviewed
Mar 12, 2026
perpil
reviewed
Mar 12, 2026
8c628c3 to
bf53f28
Compare
…imension for 404s, add statusCode metadata
7189189 to
edceaf8
Compare
sdangol
reviewed
Mar 20, 2026
|
sdangol
approved these changes
Mar 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Add metrics middleware for emitting per-request metrics from the Event Handler HTTP router using the Powertools Metrics utility.
Changes
routefield toRequestContextpopulated with the matched route pattern (e.g.GET /users/:id), ornullwhen no route matcheslatency,fault, anderrormetrics per requestNOT_FOUNDwhen no route matches to prevent dimension explosion)RequestContexta discriminated union onresponseType, enabling type-safe access to event-specific properties (e.g.requestContext.requestIdon API Gateway events but not ALB)httpMethodandpathfor all requestsstatusCodefor all requestsipAddressfromrequestContext.identity.sourceIp(V1),requestContext.http.sourceIp(V2), orX-Forwarded-Forheader (ALB fallback)userAgentfrom request headersapiGwRequestIdandapiGwApiIdfor API Gateway V1 and V2 eventsapiGwExtendedRequestIdfor API Gateway V1 events when availablegetResponseTypefunction (replaced by type guard branching in#buildRequestContext)@aws-lambda-powertools/metricsas an optional peer dependency, following the same pattern as the tracer middlewareIssue 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.