Error if name is not a valid stringRangeError if name length not between 1 and 255 charactersRangeError if value is not a valid numberRangeError if unit is not a valid MetricUnitRangeError if resolution is not a valid MetricResolutionRangeError if adding dimension would exceed 29 total dimensionsRangeError if throwOnEmptyMetrics enabled and no metrics recordedthrowOnEmptyMetrics disabled)POWERTOOLS_METRICS_NAMESPACE - Default namespace for metrics (required for organization)POWERTOOLS_SERVICE_NAME - Default service name (added as dimension)POWERTOOLS_METRICS_FUNCTION_NAME - Function name for cold start metric dimensionPOWERTOOLS_METRICS_DISABLED - Disable metrics emission (useful for testing)POWERTOOLS_DEV - Enable development modeRequires experimentalDecorators enabled in tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}LambdaInterface from @aws-lambda-powertools/commons/typeshandlerClass.handler.bind(handlerClass)@middy/core version 3.x, 4.x, 5.x, or 6.x as peer dependency.use(logMetrics(...))middy()publishStoredMetrics() calledsingleMetric mode, each metric flushes immediatelypublishStoredMetrics()publishStoredMetrics()publishStoredMetrics()ColdStart metric only emitted on actual cold starts (value 1)For each setting, resolution follows this priority (highest to lowest):
Example:
// If all three are set:
// - Constructor: namespace = 'explicit'
// - ConfigService: namespace = 'config'
// - Env: POWERTOOLS_METRICS_NAMESPACE = 'env'
// Result: Uses 'explicit'
const metrics = new Metrics({
namespace: 'explicit', // Wins
customConfigService: new CustomConfigService()
});For cold start metric function_name dimension, resolved in order:
captureColdStartMetric('name')functionNamePOWERTOOLS_METRICS_FUNCTION_NAMEIf none available, ColdStart metric emitted without function_name dimension.
experimentalDecorators: truesingleMetric() creates new instance per call (memory overhead)When POWERTOOLS_METRICS_DISABLED=true: