The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as cloudwatch from "@distilled.cloud/aws/cloudwatch"; |
| 2 | import { makeBatchedSink } from "alchemy/Util/BatchedSink"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as AWSBindings from "../AWSBindings.ts"; |
| 6 | import { MetricSink, type MetricSinkDatum, type MetricSinkProps } from "./MetricSink.ts"; |
| 7 | import { PutMetricData } from "./PutMetricData.ts"; |
| 8 | |
| 9 | const encoder = new TextEncoder(); |
| 10 | |
| 11 | /** |
| 12 | * PutMetricData accepts up to 1 MB per POST; approximate each datum's wire |
| 13 | * size by its JSON encoding and leave headroom for the request envelope |
| 14 | * (`Action`, `Namespace`, form-encoding overhead). |
| 15 | */ |
| 16 | const MAX_BYTES = 1_000_000; |
| 17 | |
| 18 | export const MetricSinkHttp = Layer.effect( |
| 19 | MetricSink, |
| 20 | Effect.gen(function* () { |
| 21 | const bind = yield* AWSBindings.Bind; |
| 22 | const putMetricData = yield* PutMetricData; |
| 23 | |
| 24 | return Effect.fn(function* (props: MetricSinkProps) { |
| 25 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 26 | yield* bind`AWS.CloudWatch.MetricSink(${props.Namespace})`({ |
| 27 | policyStatements: [ |
| 28 | { |
| 29 | Effect: "Allow", |
| 30 | // PutMetricData does not support resource-level permissions. |
| 31 | Action: ["cloudwatch:PutMetricData"], |
| 32 | Resource: ["*"], |
| 33 | }, |
| 34 | ], |
| 35 | }); |
| 36 | } |
| 37 | const put = yield* putMetricData(); |
| 38 | return makeBatchedSink< |
| 39 | MetricSinkDatum, |
| 40 | cloudwatch.PutMetricDataResponse, |
| 41 | cloudwatch.PutMetricDataError |
| 42 | >({ |
| 43 | maxRecords: 1000, |
| 44 | maxBytes: MAX_BYTES, |
| 45 | sizeOf: (datum) => encoder.encode(JSON.stringify(datum)).length, |
| 46 | send: (batch) => |
| 47 | put({ |
| 48 | Namespace: props.Namespace, |
| 49 | MetricData: [...batch], |
| 50 | }), |
| 51 | // PutMetricData is all-or-nothing — no per-datum partial failures to |
| 52 | // extract, so there is no `unprocessed`/`rejected` handling here. |
| 53 | }); |
| 54 | }); |
| 55 | }), |
| 56 | ); |