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
87 );87 );
88};88};
89 89
90const utf8ByteCounter = new TextEncoder();
91
90const byteLengthOf = (chunk: unknown): number => {92const byteLengthOf = (chunk: unknown): number => {
91 if (typeof chunk === "string") return chunk.length;93 if (typeof chunk === "string") return utf8ByteCounter.encode(chunk).length;
92 if (chunk instanceof ArrayBuffer) return chunk.byteLength;94 if (chunk instanceof ArrayBuffer) return chunk.byteLength;
93 if (ArrayBuffer.isView(chunk)) return chunk.byteLength;95 if (ArrayBuffer.isView(chunk)) return chunk.byteLength;
94 return 0;96 return 0;
@@ -197,7 +199,12 @@ const sseHeaders = (headers: Headers): Headers => { · 102 unmodified lines
197};199};
198 200
199export const wrapMcpSseResponse = (request: Request, env: Env, response: Response): Response => {201export 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;
201 208
202 const now = Date.now();209 const now = Date.now();
203 const connection: SseConnection = {210 const connection: SseConnection = {
@@ -302,10 +309,13 @@ export class CountingSpanProcessor implements SpanProcessor { · 98 unmodified lines
302 onEnd(span: ReadableSpan): void {309 onEnd(span: ReadableSpan): void {
303 otelMetrics.spansEnded += 1;310 otelMetrics.spansEnded += 1;
304 if (this.estimatedQueuedSpans >= this.maxQueueSize) {311 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).
305 otelMetrics.spansDropped += 1;315 otelMetrics.spansDropped += 1;
306 } else {316 return;
307 this.estimatedQueuedSpans += 1;
308 }317 }
318 this.estimatedQueuedSpans += 1;
309 this.inner.onEnd(span);319 this.inner.onEnd(span);
310 }320 }
311 321

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