History

Address review: abort-free byte counts, SSE-only wrap guard, no drop/export double-count

Rhys Sullivan committed 4 months ago

Commit 653af151 parent d9d3b561 file · +14 −4Browse files

Unified · Split

apps/cloud/src/observability/memory-metrics.ts+14−4
@@ -87,8 +87,10 @@ const scriptVersionFromEnv = (env: Env): string => { · 86 unmodified lines
8787 );
8888 };
8989
90+const utf8ByteCounter = new TextEncoder();
91+
9092 const byteLengthOf = (chunk: unknown): number => {
91− if (typeof chunk === "string") return chunk.length;
93+ if (typeof chunk === "string") return utf8ByteCounter.encode(chunk).length;
9294 if (chunk instanceof ArrayBuffer) return chunk.byteLength;
9395 if (ArrayBuffer.isView(chunk)) return chunk.byteLength;
9496 return 0;
@@ -197,7 +199,12 @@ const sseHeaders = (headers: Headers): Headers => { · 102 unmodified lines
197199 };
198200
199201 export const wrapMcpSseResponse = (request: Request, env: Env, response: Response): Response => {
200− if (request.method !== "GET" || response.body === null) return response;
202+ if (
203+ request.method !== "GET" ||
204+ response.body === null ||
205+ !(response.headers.get("content-type") ?? "").includes("text/event-stream")
206+ )
207+ return response;
201208
202209 const now = Date.now();
203210 const connection: SseConnection = {
@@ -302,10 +309,13 @@ export class CountingSpanProcessor implements SpanProcessor { · 98 unmodified lines
302309 onEnd(span: ReadableSpan): void {
303310 otelMetrics.spansEnded += 1;
304311 if (this.estimatedQueuedSpans >= this.maxQueueSize) {
312+ // At the bound the inner processor would drop this span anyway; drop it
313+ // here instead of forwarding, so a dropped span is never also counted as
314+ // exported (which would double-count and inflate the drop signal).
305315 otelMetrics.spansDropped += 1;
306− } else {
307− this.estimatedQueuedSpans += 1;
316+ return;
308317 }
318+ this.estimatedQueuedSpans += 1;
309319 this.inner.onEnd(span);
310320 }
311321

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t