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 | 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 | 94 | if (chunk instanceof ArrayBuffer) return chunk.byteLength; |
| 93 | 95 | if (ArrayBuffer.isView(chunk)) return chunk.byteLength; |
| 94 | 96 | return 0; |
| @@ -197,7 +199,12 @@ const sseHeaders = (headers: Headers): Headers => { · 102 unmodified lines | ||
| 197 | 199 | }; |
| 198 | 200 | |
| 199 | 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 | 209 | const now = Date.now(); |
| 203 | 210 | const connection: SseConnection = { |
| @@ -302,10 +309,13 @@ export class CountingSpanProcessor implements SpanProcessor { · 98 unmodified lines | ||
| 302 | 309 | onEnd(span: ReadableSpan): void { |
| 303 | 310 | otelMetrics.spansEnded += 1; |
| 304 | 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 | 315 | otelMetrics.spansDropped += 1; |
| 306 | − } else { | |
| 307 | − this.estimatedQueuedSpans += 1; | |
| 316 | + return; | |
| 308 | 317 | } |
| 318 | + this.estimatedQueuedSpans += 1; | |
| 309 | 319 | this.inner.onEnd(span); |
| 310 | 320 | } |
| 311 | 321 | |