MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / traceCloudMcpRequest

Function traceCloudMcpRequest

apps/cloud/src/server.ts:113–170  ·  view source on GitHub ↗
(
  request: Request,
  _env: Env,
  ctx: ExecutionContext,
  handle: (tracedRequest: Request) => Promise<Response>,
)

Source from the content-addressed store, hash-verified

111const tracer = trace.getTracer("executor-cloud-worker");
112
113const traceCloudMcpRequest = async (
114 request: Request,
115 _env: Env,
116 ctx: ExecutionContext,
117 handle: (tracedRequest: Request) => Promise<Response>,
118): Promise<Response> => {
119 if (!installTracerProvider()) return handle(request);
120
121 const url = new URL(request.url);
122 const inbound = parseTraceparent(request.headers.get("traceparent"), null);
123 const parentContext = inbound
124 ? trace.setSpanContext(context.active(), {
125 traceId: inbound.traceId,
126 spanId: inbound.spanId,
127 traceFlags: inbound.traceFlags,
128 isRemote: true,
129 })
130 : context.active();
131
132 return tracer.startActiveSpan(
133 `http.server ${request.method}`,
134 { kind: SpanKind.SERVER },
135 parentContext,
136 async (span) => {
137 span.setAttribute(ATTR_HTTP_REQUEST_METHOD, request.method);
138 span.setAttribute(ATTR_URL_FULL, request.url);
139 span.setAttribute(ATTR_URL_PATH, url.pathname);
140 span.setAttribute(ATTR_URL_SCHEME, url.protocol.replace(/:$/, ""));
141 const spanContext = span.spanContext();
142 const headers = new Headers(request.headers);
143 headers.set(
144 "traceparent",
145 `00-${spanContext.traceId}-${spanContext.spanId}-${(spanContext.traceFlags & 0xff).toString(16).padStart(2, "0")}`,
146 );
147 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; observe response/error for span status, keep trace export alive after the Agents bridge resolves or rejects
148 try {
149 const response = await handle(new Request(request, { headers }));
150 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
151 if (response.status >= 500) {
152 span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${response.status}` });
153 }
154 return response;
155 } catch (err) {
156 // oxlint-disable-next-line executor/no-instanceof-error, executor/no-unknown-error-message -- adapter boundary: Cloudflare's fetch callback throws untyped; normalized only for the OTel span record, the original error is rethrown below
157 const cause = err instanceof Error ? err : String(err);
158 span.recordException(cause);
159 // oxlint-disable-next-line executor/no-unknown-error-message -- adapter boundary: same normalization as the recordException line above
160 const message = typeof cause === "string" ? cause : cause.message;
161 span.setStatus({ code: SpanStatusCode.ERROR, message });
162 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime
163 throw err;
164 } finally {
165 span.end();
166 ctx.waitUntil(flushTracerProvider());
167 }
168 },
169 );
170};

Callers

nothing calls this directly

Calls 11

installTracerProviderFunction · 0.90
parseTraceparentFunction · 0.90
flushTracerProviderFunction · 0.90
setAttributeMethod · 0.80
setMethod · 0.80
toStringMethod · 0.80
endMethod · 0.80
getMethod · 0.65
replaceMethod · 0.65
waitUntilMethod · 0.65
handleFunction · 0.50

Tested by

no test coverage detected