GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): export server total counters as monotonic OTLP sums (#43… · patroza/t3code@77cd243

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/observability
    • Metrics.test.ts
    • Metrics.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/observability/Metrics.test.ts‎

Copy file name to clipboard
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

‎apps/server/src/observability/Metrics.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/observability/Metrics.ts
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { compactMetricAttributes, outcomeFromExit } from "./Attributes.ts";
99

1010
export const rpcRequestsTotal = Metric.counter("t3_rpc_requests_total", {
1111
description: "Total RPC requests handled by the websocket RPC server.",
12+
incremental: true,
1213
});
1314

1415
export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", {
@@ -17,6 +18,7 @@ export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", {
1718

1819
export const orchestrationCommandsTotal = Metric.counter("t3_orchestration_commands_total", {
1920
description: "Total orchestration commands dispatched, by outcome.",
21+
incremental: true,
2022
});
2123

2224
export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", {
@@ -27,6 +29,7 @@ export const orchestrationEffectClaimsTotal = Metric.counter(
2729
"t3_orchestration_effect_claims_total",
2830
{
2931
description: "Total completed orchestration effect outbox claim attempts by result.",
32+
incremental: true,
3033
},
3134
);
3235

@@ -48,10 +51,12 @@ export const otlpExportAttemptFailuresTotal = Metric.counter(
4851

4952
export const providerSessionsTotal = Metric.counter("t3_provider_sessions_total", {
5053
description: "Total provider session lifecycle operations.",
54+
incremental: true,
5155
});
5256

5357
export const providerTurnsTotal = Metric.counter("t3_provider_turns_total", {
5458
description: "Total provider turn lifecycle operations.",
59+
incremental: true,
5560
});
5661

5762
export const providerTurnDuration = Metric.timer("t3_provider_turn_duration", {
@@ -84,6 +89,7 @@ export const serverEventLoopDelayMax = Metric.gauge("t3_server_event_loop_delay_
8489

8590
export const runsTotal = Metric.counter("t3_runs_total", {
8691
description: "Runs that finished, by outcome. A later rollback does not count again.",
92+
incremental: true,
8793
});
8894

8995
export const runDuration = Metric.timer("t3_run_duration", {
@@ -96,6 +102,7 @@ export const runQueueWait = Metric.timer("t3_run_queue_wait", {
96102

97103
export const gitCommandsTotal = Metric.counter("t3_git_commands_total", {
98104
description: "Total git commands executed by the server runtime.",
105+
incremental: true,
99106
});
100107

101108
export const gitCommandDuration = Metric.timer("t3_git_command_duration", {
@@ -104,10 +111,12 @@ export const gitCommandDuration = Metric.timer("t3_git_command_duration", {
104111

105112
export const terminalSessionsTotal = Metric.counter("t3_terminal_sessions_total", {
106113
description: "Total terminal sessions started.",
114+
incremental: true,
107115
});
108116

109117
export const webhookDeliveriesTotal = Metric.counter("t3_webhook_deliveries_total", {
110118
description: "Webhook requests handled, by outcome and source.",
119+
incremental: true,
111120
});
112121

113122
export const webhookDeliveryDuration = Metric.timer("t3_webhook_delivery_duration", {
@@ -121,14 +130,17 @@ export const webhookHeldDelay = Metric.timer("t3_webhook_held_delay", {
121130

122131
export const webhookRunsTotal = Metric.counter("t3_webhook_runs_total", {
123132
description: "Runs started from webhook deliveries, by outcome.",
133+
incremental: true,
124134
});
125135

126136
export const secretRequestsTotal = Metric.counter("t3_secret_requests_total", {
127137
description: "Secrets agents asked users for, by how each request ended.",
138+
incremental: true,
128139
});
129140

130141
export const secretRefsConsumedTotal = Metric.counter("t3_secret_refs_consumed_total", {
131142
description: "Secret refs tools tried to use, by result.",
143+
incremental: true,
132144
});
133145

134146
export const metricAttributes = (

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.