Unified · Split
| @@ -87,8 +87,10 @@ const scriptVersionFromEnv = (env: Env): string => { · 86 unmodified lines | |||
| 87 | ); | 87 | ); |
| 88 | }; | 88 | }; |
| 89 | 89 | ||
| 90 | const utf8ByteCounter = new TextEncoder(); | ||
| 91 | |||
| 90 | const byteLengthOf = (chunk: unknown): number => { | 92 | const 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 | ||
| 199 | export const wrapMcpSseResponse = (request: Request, env: Env, response: Response): Response => { | 201 | 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; | ||
| 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 | ||