Skip to content
Myr

binding.ts

This file

Path

packages/project/src/metrics/binding.ts

85 lines · 2.9 KiB · TypeScript

Last changed by

Merge observability into mainoscabrielbefore adoption1985111

History of this file

Take it

Merge observability into mainoscabrielbefore adoption1985111

lines 1–85why, one step back
1/**
2 * Workers Analytics Engine: one row per point in the stage's own dataset,3 * sampled by project. A query reads a point by its columns:4 *5 * | point | blob1 | blob2 | blob3 | blob4 | double1 | double2 |6 * | -------------- | -------------- | --------------- | ------- | ----- | ------------ | ---------- |7 * | `Landed` | `Landed` | | | | 1 | |8 * | `Refused` | `Refused` | the refusal tag | stage | area | 1 | |9 * | `LaneRan` | `LaneRan` | lane | result | | `durationMs` | |10 * | `ChecksWaited` | `ChecksWaited` | | | | `waitedMs` | |11 * | `Drafted` | `Drafted` | ready or failed | | | `latencyMs` | `attempts` |12 *13 * `index1` is the project's name, cut to the 96 bytes an index holds.14 */15import { Data, Effect, Layer } from "effect";1617import { MetricPoint, Metrics } from "../metrics.ts";1819/** Mutable arrays, as the binding's own type takes them. */20export interface DataPoint {21 readonly blobs: string[];22 readonly doubles: number[];23 readonly indexes: string[];24}2526/** Structural, so the package needs no Workers types. */27export interface DatasetLike {28 readonly writeDataPoint: (point: DataPoint) => void;29}3031/** A project's name is ASCII, so its first 96 characters are its first 96 bytes. */32const INDEX_BYTES = 96;3334const columns = (35 blobs: readonly string[],36 doubles: readonly number[]37): Pick<DataPoint, "blobs" | "doubles"> => ({38 blobs: [...blobs],39 doubles: [...doubles],40});4142export const dataPointOf = (point: MetricPoint): DataPoint => {43 const { blobs, doubles } = MetricPoint.match(point, {44 ChecksWaited: ({ waitedMs }) => columns([], [waitedMs]),45 Drafted: ({ attempts, latencyMs, ready }) =>46 columns([ready ? "ready" : "failed"], [latencyMs, attempts]),47 Landed: () => columns([], [1]),48 LaneRan: ({ durationMs, lane, result }) =>49 columns([lane, result], [durationMs]),50 Refused: ({ area, stage, tag }) => columns([tag, stage, area ?? ""], [1]),51 });5253 return {54 blobs: [point._tag, ...blobs],55 doubles,56 indexes: [point.project.slice(0, INDEX_BYTES)],57 };58};5960class MetricRefused extends Data.TaggedError("MetricRefused")<{61 readonly cause: unknown;62}> {}6364export const MetricsBinding = (dataset: DatasetLike) =>65 Layer.succeed(Metrics, {66 write: (points) =>67 Effect.forEach(68 points,69 (point) =>70 Effect.try({71 catch: (cause) => new MetricRefused({ cause }),72 try: () => {73 dataset.writeDataPoint(dataPointOf(point));74 },75 }).pipe(76 Effect.catchTag("MetricRefused", ({ cause }) =>77 Effect.logWarning(78 `the metrics dataset refused a ${point._tag} point`,79 cause80 )81 )82 ),83 { discard: true }84 ),85 });