|
9 | 9 | // Collector-boundary cases start the real NodeSDK, so teardown restores every global SDK |
10 | 10 | // registration; otherwise a shutdown provider would poison later real-SDK cases. |
11 | 11 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 12 | +import { createServer } from "node:http"; |
| 13 | +import type { AddressInfo } from "node:net"; |
12 | 14 | import { tmpdir } from "node:os"; |
13 | 15 | import path from "node:path"; |
14 | 16 | import { context, diag, DiagLogLevel, metrics, propagation, trace } from "@opentelemetry/api"; |
@@ -36,6 +38,10 @@ import { |
36 | 38 |
|
37 | 39 | const PRELOAD_ENV = "OPENCLAW_OTEL_PRELOADED"; |
38 | 40 | const ENDPOINT_ENV_KEYS = [ |
| 41 | + "OTEL_SDK_DISABLED", |
| 42 | + "OTEL_TRACES_EXPORTER", |
| 43 | + "OTEL_METRICS_EXPORTER", |
| 44 | + "OTEL_LOGS_EXPORTER", |
39 | 45 | "OTEL_EXPORTER_OTLP_ENDPOINT", |
40 | 46 | "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", |
41 | 47 | "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", |
@@ -179,6 +185,154 @@ function captureOtelDiagnostics(): string[] { |
179 | 185 | return messages; |
180 | 186 | } |
181 | 187 |
|
| 188 | +async function startOtlpReceiver() { |
| 189 | + const requests: Array<{ |
| 190 | + contentType: string | undefined; |
| 191 | + method: string | undefined; |
| 192 | + url: string; |
| 193 | + }> = []; |
| 194 | + const server = createServer((request, response) => { |
| 195 | + request.resume(); |
| 196 | + request.on("end", () => { |
| 197 | + requests.push({ |
| 198 | + contentType: request.headers["content-type"], |
| 199 | + method: request.method, |
| 200 | + url: request.url ?? "", |
| 201 | + }); |
| 202 | + response.writeHead(200, { "content-type": "application/x-protobuf" }); |
| 203 | + response.end(); |
| 204 | + }); |
| 205 | + }); |
| 206 | + await new Promise<void>((resolve, reject) => { |
| 207 | + server.once("error", reject); |
| 208 | + server.listen(0, "127.0.0.1", resolve); |
| 209 | + }); |
| 210 | + const { port } = server.address() as AddressInfo; |
| 211 | + return { |
| 212 | + endpoint: `http://127.0.0.1:${port}`, |
| 213 | + requests, |
| 214 | + async close() { |
| 215 | + await new Promise<void>((resolve, reject) => { |
| 216 | + server.close((error) => (error ? reject(error) : resolve())); |
| 217 | + server.closeIdleConnections(); |
| 218 | + }); |
| 219 | + }, |
| 220 | + }; |
| 221 | +} |
| 222 | + |
| 223 | +function releasePreloadedOtelGlobals() { |
| 224 | + context.disable(); |
| 225 | + logs.disable(); |
| 226 | + metrics.disable(); |
| 227 | + propagation.disable(); |
| 228 | + trace.disable(); |
| 229 | + process.env[PRELOAD_ENV] = "0"; |
| 230 | +} |
| 231 | + |
| 232 | +async function emitRealSdkSignals() { |
| 233 | + trace.getTracer("openclaw-otel-routing-test").startSpan("routing-test").end(); |
| 234 | + metrics.getMeter("openclaw-otel-routing-test").createCounter("openclaw.routing.test").add(1); |
| 235 | + emit({ |
| 236 | + type: "log.record", |
| 237 | + level: "INFO", |
| 238 | + message: "OTLP routing test", |
| 239 | + }); |
| 240 | + await waitForDiagnosticEventsDrained(); |
| 241 | +} |
| 242 | + |
| 243 | +const SHARED_ENDPOINT_ROUTING_CASES = [ |
| 244 | + { |
| 245 | + label: "root", |
| 246 | + suffix: "", |
| 247 | + expected: ["/v1/traces", "/v1/metrics", "/v1/logs"], |
| 248 | + }, |
| 249 | + { |
| 250 | + label: "trailing-slash root", |
| 251 | + suffix: "/", |
| 252 | + expected: ["/v1/traces", "/v1/metrics", "/v1/logs"], |
| 253 | + }, |
| 254 | + { |
| 255 | + label: "custom collector path", |
| 256 | + suffix: "/api/public/otel", |
| 257 | + expected: [ |
| 258 | + "/api/public/otel/v1/traces", |
| 259 | + "/api/public/otel/v1/metrics", |
| 260 | + "/api/public/otel/v1/logs", |
| 261 | + ], |
| 262 | + }, |
| 263 | + { |
| 264 | + label: "signal-qualified collector path", |
| 265 | + suffix: "/api/public/otel/v1/traces", |
| 266 | + expected: [ |
| 267 | + "/api/public/otel/v1/traces", |
| 268 | + "/api/public/otel/v1/metrics", |
| 269 | + "/api/public/otel/v1/logs", |
| 270 | + ], |
| 271 | + }, |
| 272 | +] as const; |
| 273 | + |
| 274 | +test.each(SHARED_ENDPOINT_ROUTING_CASES)( |
| 275 | + "routes real exporters from a shared $label endpoint", |
| 276 | + async ({ suffix, expected }) => { |
| 277 | + const receiver = await startOtlpReceiver(); |
| 278 | + releasePreloadedOtelGlobals(); |
| 279 | + const { service, ctx } = await startOtelService({ |
| 280 | + endpoint: `${receiver.endpoint}${suffix}`, |
| 281 | + traces: true, |
| 282 | + metrics: true, |
| 283 | + logs: true, |
| 284 | + }); |
| 285 | + |
| 286 | + try { |
| 287 | + await emitRealSdkSignals(); |
| 288 | + await service.stop?.(ctx); |
| 289 | + |
| 290 | + expect(new Set(receiver.requests.map((request) => request.url))).toEqual(new Set(expected)); |
| 291 | + expect( |
| 292 | + receiver.requests.every( |
| 293 | + (request) => |
| 294 | + request.method === "POST" && request.contentType === "application/x-protobuf", |
| 295 | + ), |
| 296 | + ).toBe(true); |
| 297 | + } finally { |
| 298 | + await service.stop?.(ctx); |
| 299 | + await receiver.close(); |
| 300 | + } |
| 301 | + }, |
| 302 | + 30_000, |
| 303 | +); |
| 304 | + |
| 305 | +test("uses real signal-specific exporter endpoints verbatim", async () => { |
| 306 | + const receiver = await startOtlpReceiver(); |
| 307 | + releasePreloadedOtelGlobals(); |
| 308 | + const traceEndpoint = `${receiver.endpoint}/custom-traces?tenant=red`; |
| 309 | + const metricEndpoint = `${receiver.endpoint}/custom-metrics/`; |
| 310 | + const logEndpoint = `${receiver.endpoint}/v1/traces`; |
| 311 | + const { service, ctx } = await startOtelService({ |
| 312 | + endpoint: `${receiver.endpoint}/shared-unused`, |
| 313 | + traces: true, |
| 314 | + metrics: true, |
| 315 | + logs: true, |
| 316 | + configure: (serviceContext) => { |
| 317 | + serviceContext.config.diagnostics!.otel!.tracesEndpoint = traceEndpoint; |
| 318 | + serviceContext.config.diagnostics!.otel!.metricsEndpoint = metricEndpoint; |
| 319 | + serviceContext.config.diagnostics!.otel!.logsEndpoint = logEndpoint; |
| 320 | + }, |
| 321 | + }); |
| 322 | + |
| 323 | + try { |
| 324 | + await emitRealSdkSignals(); |
| 325 | + await service.stop?.(ctx); |
| 326 | + |
| 327 | + expect(new Set(receiver.requests.map((request) => request.url))).toEqual( |
| 328 | + new Set(["/custom-traces?tenant=red", "/custom-metrics/", "/v1/traces"]), |
| 329 | + ); |
| 330 | + } finally { |
| 331 | + await service.stop?.(ctx); |
| 332 | + await receiver.close(); |
| 333 | + } |
| 334 | +}, 30_000); |
| 335 | + |
182 | 336 | // Covers all three completeTrackedLifecycleSpan owners: run.completed, |
183 | 337 | // harness.run.completed, and message.processed. The mocked suite cannot tell the two id |
184 | 338 | // spaces apart, so a regression at any one of them is only visible here. |
|
0 commit comments