|
| 1 | +// Cloud: the telemetry contract, end to end. A tool call that hits an |
| 2 | +// upstream error wall must be visible in the EXPORTED spans — not just |
| 3 | +// handled gracefully for the caller. This is the regression class where the |
| 4 | +// product silently goes dark to operators: `ToolResult.fail` rides the |
| 5 | +// Effect success channel (a healthy-looking span), and an attribute stamped |
| 6 | +// on the wrong span simply never arrives in the trace store, which looks |
| 7 | +// identical to health. So the assertion runs against the OTLP store the dev |
| 8 | +// stack actually exported to (the suite's motel — the same exporter layer |
| 9 | +// that ships prod spans to Axiom), driving the whole production topology: |
| 10 | +// HTTP API → execution engine → sandbox → OpenAPI invoke → a real upstream |
| 11 | +// returning 502 → span batch → OTLP export. |
| 12 | +// |
| 13 | +// Pins two regressions found live in prod (2026-06-12): http.status_code was |
| 14 | +// annotated inside the inner `OpenApi.invoke` span so the `plugin.openapi. |
| 15 | +// invoke` span queries target carried it on 0 of ~19.5k spans; and failed |
| 16 | +// tool calls were indistinguishable from successes on `executor.tool.execute`. |
| 17 | +import { randomBytes } from "node:crypto"; |
| 18 | +import { createServer } from "node:http"; |
| 19 | + |
| 20 | +import { expect } from "@effect/vitest"; |
| 21 | +import { Effect } from "effect"; |
| 22 | +import { composePluginApi } from "@executor-js/api/server"; |
| 23 | +import { openApiHttpPlugin } from "@executor-js/plugin-openapi/api"; |
| 24 | +import { AuthTemplateSlug, ConnectionName, IntegrationSlug } from "@executor-js/sdk/shared"; |
| 25 | + |
| 26 | +import { scenario } from "../src/scenario"; |
| 27 | +import { Api, Target, Telemetry } from "../src/services"; |
| 28 | + |
| 29 | +const api = composePluginApi([openApiHttpPlugin()] as const); |
| 30 | + |
| 31 | +/** Two operations: /ok answers 200, /fail answers 502 — the success and |
| 32 | + * expected-upstream-failure outcome classes the telemetry must separate. */ |
| 33 | +const upstreamSpec = (baseUrl: string): string => |
| 34 | + JSON.stringify({ |
| 35 | + openapi: "3.0.3", |
| 36 | + info: { title: "Telemetry Upstream", version: "1.0.0" }, |
| 37 | + servers: [{ url: baseUrl }], |
| 38 | + paths: { |
| 39 | + "/ok": { |
| 40 | + get: { |
| 41 | + operationId: "ok", |
| 42 | + summary: "Succeeds", |
| 43 | + tags: ["probe"], |
| 44 | + responses: { "200": { description: "" } }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + "/fail": { |
| 48 | + get: { |
| 49 | + operationId: "fail", |
| 50 | + summary: "Always 502", |
| 51 | + tags: ["probe"], |
| 52 | + responses: { "200": { description: "" } }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | +/** A real upstream on 127.0.0.1: /ok → 200 JSON, anything else → 502 JSON. */ |
| 59 | +const serveUpstream = Effect.acquireRelease( |
| 60 | + Effect.callback<{ readonly baseUrl: string; readonly close: () => void }>((resume) => { |
| 61 | + const server = createServer((request, response) => { |
| 62 | + const ok = request.url?.startsWith("/ok") ?? false; |
| 63 | + response.writeHead(ok ? 200 : 502, { "content-type": "application/json" }); |
| 64 | + response.end(ok ? '{"fine":true}' : '{"error":{"message":"bad gateway"}}'); |
| 65 | + }); |
| 66 | + server.listen(0, "127.0.0.1", () => { |
| 67 | + const address = server.address(); |
| 68 | + const port = typeof address === "object" && address ? address.port : 0; |
| 69 | + resume( |
| 70 | + Effect.succeed({ |
| 71 | + baseUrl: `http://127.0.0.1:${port}`, |
| 72 | + close: () => { |
| 73 | + server.close(); |
| 74 | + server.closeAllConnections(); |
| 75 | + }, |
| 76 | + }), |
| 77 | + ); |
| 78 | + }); |
| 79 | + }), |
| 80 | + (server) => Effect.sync(server.close), |
| 81 | +); |
| 82 | + |
| 83 | +scenario( |
| 84 | + "Telemetry · a failing tool call is visible in the exported spans", |
| 85 | + { timeout: 180_000 }, |
| 86 | + Effect.scoped( |
| 87 | + Effect.gen(function* () { |
| 88 | + const target = yield* Target; |
| 89 | + const { client: apiClient } = yield* Api; |
| 90 | + const telemetry = yield* Telemetry; |
| 91 | + const identity = yield* target.newIdentity(); |
| 92 | + const client = yield* apiClient(api, identity); |
| 93 | + |
| 94 | + const upstream = yield* serveUpstream; |
| 95 | + |
| 96 | + // Identifier-safe slug: it becomes a property path in the sandbox code. |
| 97 | + const slug = IntegrationSlug.make(`telscn${randomBytes(4).toString("hex")}`); |
| 98 | + yield* client.openapi.addSpec({ |
| 99 | + payload: { |
| 100 | + spec: { kind: "blob", value: upstreamSpec(upstream.baseUrl) }, |
| 101 | + slug, |
| 102 | + baseUrl: upstream.baseUrl, |
| 103 | + authenticationTemplate: [ |
| 104 | + { |
| 105 | + slug: "apiKey", |
| 106 | + type: "apiKey", |
| 107 | + headers: { Authorization: ["Bearer ", { type: "variable", name: "token" }] }, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + }); |
| 112 | + yield* client.connections.create({ |
| 113 | + payload: { |
| 114 | + owner: "org", |
| 115 | + name: ConnectionName.make("main"), |
| 116 | + integration: slug, |
| 117 | + template: AuthTemplateSlug.make("apiKey"), |
| 118 | + value: "telemetry-scenario-token", |
| 119 | + }, |
| 120 | + }); |
| 121 | + |
| 122 | + const tools = yield* client.tools.list({ query: {} }); |
| 123 | + const addressOf = (op: string) => { |
| 124 | + const tool = tools.find( |
| 125 | + (entry) => |
| 126 | + String(entry.integration) === String(slug) && String(entry.address).endsWith(`.${op}`), |
| 127 | + ); |
| 128 | + expect(tool, `the ${op} tool is in the catalog`).toBeDefined(); |
| 129 | + return String(tool!.address); |
| 130 | + }; |
| 131 | + const failAddress = addressOf("fail"); |
| 132 | + const okAddress = addressOf("ok"); |
| 133 | + |
| 134 | + // Drive both outcome classes through the full production path. The |
| 135 | + // failing call still completes for the caller — that is exactly why |
| 136 | + // the exported span is the only place an operator can see it. |
| 137 | + for (const address of [okAddress, failAddress]) { |
| 138 | + const execution = yield* client.executions.execute({ |
| 139 | + payload: { code: `return await ${address}({});` }, |
| 140 | + }); |
| 141 | + expect(execution.status, `the ${address} execution completes`).toBe("completed"); |
| 142 | + } |
| 143 | + |
| 144 | + // The failure: outcome attributes on the tool span... |
| 145 | + const failSpan = yield* telemetry.expectSpan({ |
| 146 | + operation: "executor.tool.execute", |
| 147 | + attributes: { "mcp.tool.name": failAddress }, |
| 148 | + }); |
| 149 | + expect(failSpan.span.tags, "a failed tool call is marked on the exported span").toMatchObject( |
| 150 | + { |
| 151 | + "executor.tool.outcome": "fail", |
| 152 | + "executor.tool.error_code": "upstream_http_error", |
| 153 | + "executor.tool.error_status": "502", |
| 154 | + }, |
| 155 | + ); |
| 156 | + expect( |
| 157 | + failSpan.span.tags["executor.tenant"], |
| 158 | + "the span carries tenant attribution (no trace-join needed to ask 'whose error?')", |
| 159 | + ).toBeTruthy(); |
| 160 | + |
| 161 | + // ...and the upstream status on the HTTP span queries actually target. |
| 162 | + const invokeSpan = yield* telemetry.expectSpan({ |
| 163 | + operation: "plugin.openapi.invoke", |
| 164 | + attributes: { "plugin.openapi.base_url": upstream.baseUrl, "http.status_code": "502" }, |
| 165 | + }); |
| 166 | + expect( |
| 167 | + invokeSpan.span.tags["plugin.openapi.method"], |
| 168 | + "the invoke span names the method", |
| 169 | + ).toBe("GET"); |
| 170 | + |
| 171 | + // The success is distinguishable from the failure. |
| 172 | + const okSpan = yield* telemetry.expectSpan({ |
| 173 | + operation: "executor.tool.execute", |
| 174 | + attributes: { "mcp.tool.name": okAddress }, |
| 175 | + }); |
| 176 | + expect(okSpan.span.tags["executor.tool.outcome"], "a successful call is marked ok").toBe( |
| 177 | + "ok", |
| 178 | + ); |
| 179 | + }), |
| 180 | + ), |
| 181 | +); |
0 commit comments