metrics

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 7 Imported by: 0

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"
	"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 := json.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"
	"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, httpmetrics.Options{CaptureRequestCount: true}))
	if err := app.Get("/health", func(*httpapi.RequestContext) (any, error) {
		return "ok", nil
	}); err != nil {
		panic(err)
	}
	event := json.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

Types

type Options added in v1.0.0

type Options struct {
	// CaptureRequestCount emits request=1 with unit Count per middleware execution.
	// The default emits only latency, fault and error.
	CaptureRequestCount bool
}

Options controls optional HTTP metrics beyond the TypeScript reference contract.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL