Skip to content

MetricSink

Source: src/AWS/CloudWatch/MetricSink.ts

A batching sink over CloudWatch PutMetricData (1000 datums / ~1 MB per call). Each upstream chunk is greedily packed into order-preserving batches and sent sequentially.

PutMetricData is all-or-nothing: there are no per-datum partial failures, so a failed call surfaces directly on the sink’s error channel as the typed PutMetricDataError union.

Provide CloudWatch.MetricSinkHttp (which itself needs CloudWatch.PutMetricDataHttp) on the hosting Lambda Function: Effect.provide(Layer.provideMerge(AWS.CloudWatch.MetricSinkHttp, AWS.CloudWatch.PutMetricDataHttp)).

// init — grants cloudwatch:PutMetricData; all datums publish under Namespace
const sink = yield* AWS.CloudWatch.MetricSink({
Namespace: "MyApp/Payments",
});
// runtime — datums are packed into 1000-datum PutMetricData batches
yield* Stream.fromIterable(
payments.map((payment) => ({
MetricName: "PaymentProcessed",
Dimensions: [{ Name: "Region", Value: payment.region }],
Value: payment.amount,
Unit: "Count",
}) satisfies AWS.CloudWatch.MetricSinkDatum),
).pipe(Stream.run(sink));