The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Axiom from "@alchemy.run/axiom"; |
| 2 | import * as Alchemy from "alchemy"; |
| 3 | import * as Output from "alchemy/Output"; |
| 4 | import { Effect } from "effect"; |
| 5 | import { Traces } from "./Datasets.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Saved Axiom views (APL queries) for the alchemy CLI's OTEL signals. |
| 9 | * |
| 10 | * All views target `${stage}-traces`. Axiom's metrics datasets cannot be |
| 11 | * queried via APL ("Please use the builder to query metrics datasets"), |
| 12 | * but every metric we emit has an equivalent span — `cli.<command>` for |
| 13 | * invocations, `provider.<op>` for resource lifecycle ops with |
| 14 | * `duration` for latency and `error` for status — so we derive |
| 15 | * everything from traces. |
| 16 | * |
| 17 | * Each view's `datasets` entry references `traces.name` (an Output) rather |
| 18 | * than a literal `${stage}-traces` string so Alchemy sequences view creation |
| 19 | * after the dataset exists. Without this, Axiom rejects the view with |
| 20 | * `BadRequest: failed to validate view: entity not found`. |
| 21 | * |
| 22 | * Attribute paths: |
| 23 | * - Resource attributes (per-process, set on `OtlpTracer.resource`): |
| 24 | * `['resource.custom']['alchemy.user.id']`, |
| 25 | * `['resource.custom']['alchemy.version']`, … |
| 26 | * - Span attributes (per-span, set on `Effect.withSpan(... { attributes })`): |
| 27 | * `['attributes.custom']['alchemy.resource.type']`, |
| 28 | * `['attributes.custom']['alchemy.resource.op']`, … |
| 29 | * |
| 30 | * `error` (bool) and `status.code` are first-class span fields. |
| 31 | */ |
| 32 | |
| 33 | const viewProps = <A>(fn: (ctx: { stage: string; traces: Output.Output<string> }) => A) => |
| 34 | Effect.all([Alchemy.Stack, Traces]).pipe( |
| 35 | Effect.map(([stack, traces]) => fn({ stage: stack.stage, traces: traces.name })), |
| 36 | ); |
| 37 | |
| 38 | export const ActiveUsersHourly = Axiom.View( |
| 39 | "ActiveUsersHourly", |
| 40 | viewProps(({ stage, traces }) => ({ |
| 41 | name: `${stage}-active-users-hourly`, |
| 42 | description: "Distinct alchemy.user.id per hour, last 7d", |
| 43 | datasets: [traces], |
| 44 | aplQuery: ` |
| 45 | ['${stage}-traces'] |
| 46 | | where _time > ago(7d) |
| 47 | | extend uid=tostring(['resource.custom']['alchemy.user.id']) |
| 48 | | summarize users=dcount(uid) by bin(_time, 1h) |
| 49 | | order by _time asc |
| 50 | `, |
| 51 | })), |
| 52 | ); |
| 53 | |
| 54 | export const ActiveUsersByVersion = Axiom.View( |
| 55 | "ActiveUsersByVersion", |
| 56 | viewProps(({ stage, traces }) => ({ |
| 57 | name: `${stage}-active-users-by-version`, |
| 58 | description: "Distinct users grouped by alchemy.version (last 7d)", |
| 59 | datasets: [traces], |
| 60 | aplQuery: ` |
| 61 | ['${stage}-traces'] |
| 62 | | where _time > ago(7d) |
| 63 | | extend uid=tostring(['resource.custom']['alchemy.user.id']), |
| 64 | version=tostring(['resource.custom']['alchemy.version']) |
| 65 | | summarize users=dcount(uid) by version |
| 66 | | order by users desc |
| 67 | `, |
| 68 | })), |
| 69 | ); |
| 70 | |
| 71 | export const ActiveUsersByCi = Axiom.View( |
| 72 | "ActiveUsersByCi", |
| 73 | viewProps(({ stage, traces }) => ({ |
| 74 | name: `${stage}-active-users-by-ci`, |
| 75 | description: "CI vs local users (last 7d)", |
| 76 | datasets: [traces], |
| 77 | aplQuery: ` |
| 78 | ['${stage}-traces'] |
| 79 | | where _time > ago(7d) |
| 80 | | extend uid=tostring(['resource.custom']['alchemy.user.id']), |
| 81 | ci=tostring(['resource.custom']['alchemy.ci']) |
| 82 | | summarize users=dcount(uid) by ci |
| 83 | `, |
| 84 | })), |
| 85 | ); |
| 86 | |
| 87 | export const ResourcesUsed = Axiom.View( |
| 88 | "ResourcesUsed", |
| 89 | viewProps(({ stage, traces }) => ({ |
| 90 | name: `${stage}-resources-used`, |
| 91 | description: "Top resource types by lifecycle op count (last 7d)", |
| 92 | datasets: [traces], |
| 93 | aplQuery: ` |
| 94 | ['${stage}-traces'] |
| 95 | | where _time > ago(7d) |
| 96 | | where name startswith "provider." |
| 97 | | extend rt=tostring(['attributes.custom']['alchemy.resource.type']), |
| 98 | op=tostring(['attributes.custom']['alchemy.resource.op']) |
| 99 | | summarize ops=count() by rt, op |
| 100 | | order by ops desc |
| 101 | `, |
| 102 | })), |
| 103 | ); |
| 104 | |
| 105 | export const DeployDestroyLatency = Axiom.View( |
| 106 | "DeployDestroyLatency", |
| 107 | viewProps(({ stage, traces }) => ({ |
| 108 | name: `${stage}-deploy-destroy-latency`, |
| 109 | description: "p50/p95/p99 of cli.deploy and cli.destroy spans", |
| 110 | datasets: [traces], |
| 111 | aplQuery: ` |
| 112 | ['${stage}-traces'] |
| 113 | | where _time > ago(7d) |
| 114 | | where name in ("cli.deploy", "cli.destroy") |
| 115 | | summarize p50=percentile(duration, 50), |
| 116 | p95=percentile(duration, 95), |
| 117 | p99=percentile(duration, 99) |
| 118 | by name, bin(_time, 1h) |
| 119 | | order by _time asc |
| 120 | `, |
| 121 | })), |
| 122 | ); |
| 123 | |
| 124 | export const ResourceLatency = Axiom.View( |
| 125 | "ResourceLatency", |
| 126 | viewProps(({ stage, traces }) => ({ |
| 127 | name: `${stage}-resource-latency`, |
| 128 | description: "p50/p95 of provider.<op> spans by resource_type and op (last 7d)", |
| 129 | datasets: [traces], |
| 130 | aplQuery: ` |
| 131 | ['${stage}-traces'] |
| 132 | | where _time > ago(7d) |
| 133 | | where name startswith "provider." |
| 134 | | extend rt=tostring(['attributes.custom']['alchemy.resource.type']), |
| 135 | op=tostring(['attributes.custom']['alchemy.resource.op']) |
| 136 | | summarize p50=percentile(duration, 50), |
| 137 | p95=percentile(duration, 95), |
| 138 | count=count() |
| 139 | by rt, op |
| 140 | | order by p95 desc |
| 141 | `, |
| 142 | })), |
| 143 | ); |
| 144 | |
| 145 | export const CliInvocations = Axiom.View( |
| 146 | "CliInvocations", |
| 147 | viewProps(({ stage, traces }) => ({ |
| 148 | name: `${stage}-cli-invocations`, |
| 149 | description: "cli.<command> span counts grouped by command and success/error", |
| 150 | datasets: [traces], |
| 151 | aplQuery: ` |
| 152 | ['${stage}-traces'] |
| 153 | | where _time > ago(7d) |
| 154 | | where name startswith "cli." |
| 155 | | extend command=extract("cli\\\\.(.+)", 1, name), |
| 156 | status=iff(tobool(['error']), "error", "success") |
| 157 | | summarize count=count() by command, status, bin(_time, 1h) |
| 158 | | order by _time asc |
| 159 | `, |
| 160 | })), |
| 161 | ); |
| 162 | |
| 163 | export const ResourceErrorRate = Axiom.View( |
| 164 | "ResourceErrorRate", |
| 165 | viewProps(({ stage, traces }) => ({ |
| 166 | name: `${stage}-resource-error-rate`, |
| 167 | description: "provider.<op> spans split by status (success vs error) per hour", |
| 168 | datasets: [traces], |
| 169 | aplQuery: ` |
| 170 | ['${stage}-traces'] |
| 171 | | where _time > ago(7d) |
| 172 | | where name startswith "provider." |
| 173 | | extend rt=tostring(['attributes.custom']['alchemy.resource.type']), |
| 174 | status=iff(tobool(['error']), "error", "success") |
| 175 | | summarize total=count() by rt, status, bin(_time, 1h) |
| 176 | | order by _time asc |
| 177 | `, |
| 178 | })), |
| 179 | ); |