The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as Credentials from "@distilled.cloud/aws/Credentials"; |
| 2 | import type * as SigV4 from "@distilled.cloud/aws/SigV4"; |
| 3 | import * as Binding from "alchemy/Binding"; |
| 4 | import type { SourceError } from "effect/ConfigProvider"; |
| 5 | import type * as Effect from "effect/Effect"; |
| 6 | import type { PrometheusApiError } from "./PrometheusTypes.ts"; |
| 7 | import type { Workspace } from "./Workspace.ts"; |
| 8 | |
| 9 | export interface RemoteWriteSample { |
| 10 | /** Sample value. */ |
| 11 | value: number; |
| 12 | /** |
| 13 | * Sample timestamp in epoch **milliseconds**. |
| 14 | * @default the time of the remote-write call |
| 15 | */ |
| 16 | timestamp?: number; |
| 17 | } |
| 18 | |
| 19 | export interface RemoteWriteSeries { |
| 20 | /** Metric name (becomes the `__name__` label). */ |
| 21 | name: string; |
| 22 | /** Additional labels for the series. */ |
| 23 | labels?: Record<string, string>; |
| 24 | /** Samples to append, oldest first. */ |
| 25 | samples: RemoteWriteSample[]; |
| 26 | } |
| 27 | |
| 28 | export interface RemoteWriteRequest { |
| 29 | /** Time series to write. */ |
| 30 | timeseries: RemoteWriteSeries[]; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Runtime binding for `aps:RemoteWrite` — push metric samples into an AMP |
| 35 | * {@link Workspace} via its Prometheus remote-write endpoint |
| 36 | * (`api/v1/remote_write`), SigV4-signed with the host Function's credentials. |
| 37 | * |
| 38 | * The protobuf + snappy remote-write body is encoded internally — callers |
| 39 | * pass plain metric names, labels, and samples. |
| 40 | * |
| 41 | * ### Writing Metrics |
| 42 | * **Example:** Push a Counter Sample |
| 43 | * ```typescript |
| 44 | * const remoteWrite = yield* AMP.RemoteWrite(workspace); |
| 45 | * |
| 46 | * yield* remoteWrite({ |
| 47 | * timeseries: [{ |
| 48 | * name: "jobs_processed_total", |
| 49 | * labels: { queue: "default" }, |
| 50 | * samples: [{ value: 42 }], |
| 51 | * }], |
| 52 | * }); |
| 53 | * ``` |
| 54 | * |
| 55 | * **Example:** Backfill Samples with Explicit Timestamps |
| 56 | * ```typescript |
| 57 | * yield* remoteWrite({ |
| 58 | * timeseries: [{ |
| 59 | * name: "temperature_celsius", |
| 60 | * labels: { sensor: "a1" }, |
| 61 | * samples: [ |
| 62 | * { value: 20.1, timestamp: Date.now() - 60_000 }, |
| 63 | * { value: 20.4, timestamp: Date.now() }, |
| 64 | * ], |
| 65 | * }], |
| 66 | * }); |
| 67 | * ``` |
| 68 | * |
| 69 | * @binding |
| 70 | */ |
| 71 | export interface RemoteWrite extends Binding.Service< |
| 72 | RemoteWrite, |
| 73 | "AWS.AMP.RemoteWrite", |
| 74 | ( |
| 75 | workspace: Workspace, |
| 76 | ) => Effect.Effect< |
| 77 | ( |
| 78 | request: RemoteWriteRequest, |
| 79 | ) => Effect.Effect< |
| 80 | void, |
| 81 | PrometheusApiError | Credentials.CredentialsError | SourceError | SigV4.SigningError |
| 82 | > |
| 83 | > |
| 84 | > {} |
| 85 | export const RemoteWrite = Binding.Service<RemoteWrite>("AWS.AMP.RemoteWrite"); |