Documentation
¶
Overview ¶
Package metrics adds CloudWatch EMF middleware to the Lambda HTTP router.
New adapts a core Metrics instance to HTTP middleware. Register it with the router's Use method during application setup. Route execution records latency, fault and error metrics using request-scoped metric state. Options.CaptureRequestCount opts in to a request metric with unit Count and value 1 per middleware execution. The default follows TypeScript v2.35.0.
Publication lifecycle ¶
Each request owns a scope that publishes when middleware execution finishes, before any streaming body transfer. The parent scope remains unchanged. Publication failures propagate while retaining business errors. The application supplies namespace, service, output and metric policy.
Aggregate request with Sum for published execution counts, including errors and panics. It does not measure unique client requests or completed streams; retries and EMF delivery can repeat samples. Reserve the request metric and route dimension for this middleware when enabling it. Additional metrics incur CloudWatch costs.
This separate module keeps the core HTTP router free of observability dependencies.
For usage and compatibility details, see the user guide.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(m *powermetrics.Metrics, options ...Options) httpapi.Middleware
New emits latency, fault and error metrics with a bounded route dimension. Options can enable an additional request counter without changing the defaults. Each request owns a metric scope, including metrics added downstream using m.WithContext(request.Context). It publishes before streaming body transfer, matching the reference middleware. The parent scope remains unchanged. Publication failures propagate as errors without discarding business errors.
Example ¶
package main
import (
"bytes"
"context"
jsonv1 "encoding/json"
"fmt"
httpapi "github.com/rambow-cloud/powertools-lambda-go/eventhandler/http"
httpmetrics "github.com/rambow-cloud/powertools-lambda-go/eventhandler/http/metrics"
"github.com/rambow-cloud/powertools-lambda-go/metrics"
)
func main() {
var output bytes.Buffer
m, err := metrics.New(metrics.WithNamespace("Example/Orders"),
metrics.WithServiceName("orders"), metrics.WithOutput(&output), metrics.WithDisabled(false))
if err != nil {
panic(err)
}
app := httpapi.New(httpapi.Options{})
app.Use(httpmetrics.New(m))
if err := app.Get("/health", func(*httpapi.RequestContext) (any, error) {
return "ok", nil
}); err != nil {
panic(err)
}
event := jsonv1.RawMessage(`{"version":"2.0","routeKey":"$default","rawPath":"/health","rawQueryString":"","headers":{},"requestContext":{"http":{"method":"GET"},"domainName":"api.example.test"},"isBase64Encoded":false}`)
response, err := app.Resolve(context.Background(), event)
fmt.Println(response.StatusCode, output.Len() > 0, err)
}
Output: 200 true <nil>
Example (RequestCount) ¶
package main
import (
"bytes"
"context"
jsonv1 "encoding/json"
json "encoding/json/v2"
"fmt"
httpapi "github.com/rambow-cloud/powertools-lambda-go/eventhandler/http"
httpmetrics "github.com/rambow-cloud/powertools-lambda-go/eventhandler/http/metrics"
"github.com/rambow-cloud/powertools-lambda-go/metrics"
)
func main() {
var output bytes.Buffer
m, err := metrics.New(metrics.WithNamespace("Example/Orders"),
metrics.WithServiceName("orders"), metrics.WithOutput(&output), metrics.WithDisabled(false))
if err != nil {
panic(err)
}
app := httpapi.New(httpapi.Options{})
app.Use(httpmetrics.New(m, httpmetrics.Options{CaptureRequestCount: true}))
if err := app.Get("/health", func(*httpapi.RequestContext) (any, error) {
return "ok", nil
}); err != nil {
panic(err)
}
event := jsonv1.RawMessage(`{"version":"2.0","routeKey":"$default","rawPath":"/health","rawQueryString":"","headers":{},"requestContext":{"http":{"method":"GET"},"domainName":"api.example.test"},"isBase64Encoded":false}`)
_, err = app.Resolve(context.Background(), event)
if err != nil {
panic(err)
}
var document map[string]any
if err := json.Unmarshal(output.Bytes(), &document); err != nil {
panic(err)
}
// Use Sum to aggregate the published request metric in CloudWatch.
fmt.Println(document["request"], document["route"])
}
Output: 1 GET /health