metrics.ts
This file
Path
packages/project/src/metrics.ts
82 lines · 3.1 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
Merge observability into mainoscabrielbefore adoption1985111
lines 1–82why, one step back1/**2 * The facts the platform counts and times: landings, refusals, check lanes,3 * the wait before an answer's checks start, and draft rounds. A point names4 * public things only (a project, a refusal's tag and area, a stage, a lane)5 * and numbers: never a path, a layer, a held-back check, a command, a log or6 * a value. The project writes each one to its outbox in the same commit as7 * the fact it counts, keyed by that fact, so a replayed call writes none.8 */9import { LaneOutcome } from "@repo/protocol/domain/answer";10import type { LaneResult } from "@repo/protocol/domain/answer";11import { StageName } from "@repo/protocol/domain/outcome";12import {13 ShownRefusal,14 shownRefusal,15} from "@repo/protocol/domain/recent-landing";16import { LaneName, ProjectName } from "@repo/protocol/ids";17import type { Refusal } from "@repo/protocol/refusal";18import { Context, DateTime, Effect, Schema } from "effect";1920const Millis = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));2122export const MetricPoint = Schema.TaggedUnion({23 /** How long an answer waited, from its submission, before its checks started. */24 ChecksWaited: { project: ProjectName, waitedMs: Millis },25 /** A draft round, from its start to its settle. */26 Drafted: {27 attempts: Schema.Int,28 latencyMs: Millis,29 project: ProjectName,30 ready: Schema.Boolean,31 },32 /** A commit the platform landed on a project's main. */33 Landed: { project: ProjectName },34 /** One check lane's run on an answer's merged tree, as the Checker timed it. */35 LaneRan: {36 durationMs: Millis,37 lane: LaneName,38 project: ProjectName,39 result: LaneOutcome,40 },41 /** One refusal of an answer: its tag, its area when it names one, and the stage that gave it. */42 Refused: { ...ShownRefusal.fields, project: ProjectName, stage: StageName },43});4445export type MetricPoint = typeof MetricPoint.Type;4647export interface MetricsApi {48 /** Fire and forget: a point the store cannot take is dropped with a warning, never retried. */49 readonly write: (points: readonly MetricPoint[]) => Effect.Effect<void>;50}5152/** The default writes nothing, so a world with no dataset counts nothing. */53export const Metrics = Context.Reference<MetricsApi>("project/Metrics", {54 defaultValue: () => ({ write: () => Effect.void }),55});5657export const refusedPoints = (58 project: ProjectName,59 stage: StageName,60 refusals: readonly Refusal[]61): readonly MetricPoint[] =>62 refusals.map((refusal) =>63 MetricPoint.cases.Refused.make({ ...shownRefusal(refusal), project, stage })64 );6566/** The lane's name, result and time alone: its command and log stay with the verdict. */67export const lanePoints = (68 project: ProjectName,69 lanes: readonly LaneResult[]70): readonly MetricPoint[] =>71 lanes.map(({ durationMs, lane, result }) =>72 MetricPoint.cases.LaneRan.make({73 durationMs: Math.max(durationMs, 0),74 lane,75 project,76 result,77 })78 );7980/** Never below zero: two instants a clock gave in order may still read backwards. */81export const millisBetween = (from: DateTime.Utc, to: DateTime.Utc): number =>82 Math.max(DateTime.toEpochMillis(to) - DateTime.toEpochMillis(from), 0);