GeezerVison is showing github.com — fix(server): export server total counters as monotonic OTLP sums (#43… · patroza/t3code@77cd243
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/observability
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +79Lines changed: 79 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { assert, it } from "@effect/vitest"; | | 2 | +import * as Effect from "effect/Effect"; | | 3 | +import * as Layer from "effect/Layer"; | | 4 | +import * as Metric from "effect/Metric"; | | 5 | +import { HttpClient, HttpClientResponse } from "effect/http"; | | 6 | +import { OtlpMetrics, OtlpSerialization } from "effect/observability"; | | 7 | +import { describe } from "vite-plus/test"; | | 8 | + | | 9 | +import * as Metrics from "./Metrics.ts"; | | 10 | + | | 11 | +const TOTAL_COUNTERS = Object.values(Metrics).filter( | | 12 | + (value): value is Metric.Counter<number> => Metric.isMetric(value) && value.type === "Counter", | | 13 | +); | | 14 | + | | 15 | +interface OtlpMetricsPayload { | | 16 | + readonly resourceMetrics?: ReadonlyArray<{ | | 17 | + readonly scopeMetrics?: ReadonlyArray<{ | | 18 | + readonly metrics?: ReadonlyArray<{ | | 19 | + readonly name?: string; | | 20 | + readonly sum?: { readonly isMonotonic?: boolean }; | | 21 | + }>; | | 22 | + }>; | | 23 | + }>; | | 24 | +} | | 25 | + | | 26 | +describe("Metrics", () => { | | 27 | + it.effect("exports every *_total counter as a monotonic OTLP sum", () => { | | 28 | + const exported: Array<OtlpMetricsPayload> = []; | | 29 | + const client = HttpClient.make((request) => { | | 30 | + if (request.body._tag === "Uint8Array") { | | 31 | + exported.push( | | 32 | + JSON.parse(new TextDecoder().decode(request.body.body)) as OtlpMetricsPayload, | | 33 | + ); | | 34 | + } | | 35 | + return Effect.succeed( | | 36 | + HttpClientResponse.fromWeb(request, new Response(null, { status: 200 })), | | 37 | + ); | | 38 | + }); | | 39 | + const layer = OtlpMetrics.layer({ | | 40 | + url: "http://localhost:4318/v1/metrics", | | 41 | + resource: { serviceName: "t3-test" }, | | 42 | + exportInterval: "1 hour", | | 43 | + shutdownTimeout: "1 second", | | 44 | + }).pipe( | | 45 | + Layer.provide(OtlpSerialization.layerJson), | | 46 | + Layer.provide(Layer.succeed(HttpClient.HttpClient, client)), | | 47 | + ); | | 48 | + | | 49 | + return Effect.gen(function* () { | | 50 | + const registry = new Map(); | | 51 | + yield* Effect.scoped( | | 52 | + Effect.gen(function* () { | | 53 | + yield* Layer.build(layer); | | 54 | + for (const counter of TOTAL_COUNTERS) { | | 55 | + yield* Metric.update(counter, 1); | | 56 | + } | | 57 | + }), | | 58 | + ).pipe(Effect.provideService(Metric.MetricRegistry, registry)); | | 59 | + | | 60 | + const sumsByName = new Map( | | 61 | + exported | | 62 | + .flatMap((payload) => payload.resourceMetrics ?? []) | | 63 | + .flatMap((resourceMetric) => resourceMetric.scopeMetrics ?? []) | | 64 | + .flatMap((scopeMetric) => scopeMetric.metrics ?? []) | | 65 | + .flatMap((metric) => | | 66 | + typeof metric.name === "string" && metric.sum !== undefined | | 67 | + ? [[metric.name, metric.sum.isMonotonic] as const] | | 68 | + : [], | | 69 | + ), | | 70 | + ); | | 71 | + | | 72 | + assert.isAbove(TOTAL_COUNTERS.length, 0); | | 73 | + assert.strictEqual(sumsByName.size, TOTAL_COUNTERS.length); | | 74 | + for (const [name, isMonotonic] of sumsByName) { | | 75 | + assert.isTrue(isMonotonic, `${name} should export with sum.isMonotonic = true`); | | 76 | + } | | 77 | + }); | | 78 | + }); | | 79 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/observability/Metrics.ts +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ import { compactMetricAttributes, outcomeFromExit } from "./Attributes.ts"; | 9 | 9 |
| 10 | 10 | export const rpcRequestsTotal = Metric.counter("t3_rpc_requests_total", { | 11 | 11 | description: "Total RPC requests handled by the websocket RPC server.", | | 12 | + incremental: true, | 12 | 13 | }); | 13 | 14 |
| 14 | 15 | export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", { | @@ -17,6 +18,7 @@ export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", { | 17 | 18 |
| 18 | 19 | export const orchestrationCommandsTotal = Metric.counter("t3_orchestration_commands_total", { | 19 | 20 | description: "Total orchestration commands dispatched, by outcome.", | | 21 | + incremental: true, | 20 | 22 | }); | 21 | 23 |
| 22 | 24 | export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", { | @@ -27,6 +29,7 @@ export const orchestrationEffectClaimsTotal = Metric.counter( | 27 | 29 | "t3_orchestration_effect_claims_total", | 28 | 30 | { | 29 | 31 | description: "Total completed orchestration effect outbox claim attempts by result.", | | 32 | + incremental: true, | 30 | 33 | }, | 31 | 34 | ); | 32 | 35 |
| @@ -48,10 +51,12 @@ export const otlpExportAttemptFailuresTotal = Metric.counter( | 48 | 51 |
| 49 | 52 | export const providerSessionsTotal = Metric.counter("t3_provider_sessions_total", { | 50 | 53 | description: "Total provider session lifecycle operations.", | | 54 | + incremental: true, | 51 | 55 | }); | 52 | 56 |
| 53 | 57 | export const providerTurnsTotal = Metric.counter("t3_provider_turns_total", { | 54 | 58 | description: "Total provider turn lifecycle operations.", | | 59 | + incremental: true, | 55 | 60 | }); | 56 | 61 |
| 57 | 62 | export const providerTurnDuration = Metric.timer("t3_provider_turn_duration", { | @@ -84,6 +89,7 @@ export const serverEventLoopDelayMax = Metric.gauge("t3_server_event_loop_delay_ | 84 | 89 |
| 85 | 90 | export const runsTotal = Metric.counter("t3_runs_total", { | 86 | 91 | description: "Runs that finished, by outcome. A later rollback does not count again.", | | 92 | + incremental: true, | 87 | 93 | }); | 88 | 94 |
| 89 | 95 | export const runDuration = Metric.timer("t3_run_duration", { | @@ -96,6 +102,7 @@ export const runQueueWait = Metric.timer("t3_run_queue_wait", { | 96 | 102 |
| 97 | 103 | export const gitCommandsTotal = Metric.counter("t3_git_commands_total", { | 98 | 104 | description: "Total git commands executed by the server runtime.", | | 105 | + incremental: true, | 99 | 106 | }); | 100 | 107 |
| 101 | 108 | export const gitCommandDuration = Metric.timer("t3_git_command_duration", { | @@ -104,10 +111,12 @@ export const gitCommandDuration = Metric.timer("t3_git_command_duration", { | 104 | 111 |
| 105 | 112 | export const terminalSessionsTotal = Metric.counter("t3_terminal_sessions_total", { | 106 | 113 | description: "Total terminal sessions started.", | | 114 | + incremental: true, | 107 | 115 | }); | 108 | 116 |
| 109 | 117 | export const webhookDeliveriesTotal = Metric.counter("t3_webhook_deliveries_total", { | 110 | 118 | description: "Webhook requests handled, by outcome and source.", | | 119 | + incremental: true, | 111 | 120 | }); | 112 | 121 |
| 113 | 122 | export const webhookDeliveryDuration = Metric.timer("t3_webhook_delivery_duration", { | @@ -121,14 +130,17 @@ export const webhookHeldDelay = Metric.timer("t3_webhook_held_delay", { | 121 | 130 |
| 122 | 131 | export const webhookRunsTotal = Metric.counter("t3_webhook_runs_total", { | 123 | 132 | description: "Runs started from webhook deliveries, by outcome.", | | 133 | + incremental: true, | 124 | 134 | }); | 125 | 135 |
| 126 | 136 | export const secretRequestsTotal = Metric.counter("t3_secret_requests_total", { | 127 | 137 | description: "Secrets agents asked users for, by how each request ended.", | | 138 | + incremental: true, | 128 | 139 | }); | 129 | 140 |
| 130 | 141 | export const secretRefsConsumedTotal = Metric.counter("t3_secret_refs_consumed_total", { | 131 | 142 | description: "Secret refs tools tried to use, by result.", | | 143 | + incremental: true, | 132 | 144 | }); | 133 | 145 |
| 134 | 146 | export const metricAttributes = ( | |
|