Skip to content

Add guidance on emitting custom metrics (EMF) from durable functions #147

@yaythomas

Description

@yaythomas

Problem

When using aws-embedded-metrics (or similar custom metric libraries) inside a durable function, metrics emitted in the main handler context are emitted multiple times due to replay. Wrapping metric emission in context.step() ensures metrics are emitted exactly once.

This is a non-obvious gotcha — the same code works correctly in a standard Lambda but produces inflated metric data in a durable function.

Current state

The monitoring docs cover CloudWatch metrics published by the service, but there is no guidance on emitting custom metrics (EMF, Powertools, PutMetricData) from within durable function code. The replay-awareness requirement is not documented.

Suggested improvement

  • Add a section to the best practices or monitoring docs: "Emitting custom metrics from durable functions"
  • Document the replay gotcha: always wrap metric emission in context.step() to avoid duplicate data points
  • Provide a code example showing the correct pattern, e.g.:
// ❌ Wrong — emitted on every replay
metrics.putMetric("OrderProcessed", 1);

// ✅ Correct — emitted exactly once
await context.step("emit-metric", async () => {
  metrics.putMetric("OrderProcessed", 1);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions