The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeSdk from "@effect/opentelemetry/NodeSdk" |
| 2 | import * as OtelTracer from "@effect/opentelemetry/OtelTracer" |
| 3 | import * as Resource from "@effect/opentelemetry/Resource" |
| 4 | import { assert, describe, it } from "@effect/vitest" |
| 5 | import * as OtelApi from "@opentelemetry/api" |
| 6 | import { AsyncHooksContextManager } from "@opentelemetry/context-async-hooks" |
| 7 | import { InMemorySpanExporter, SimpleSpanProcessor, type SpanExporter } from "@opentelemetry/sdk-trace-base" |
| 8 | import * as Cause from "effect/Cause" |
| 9 | import * as EffectContext from "effect/Context" |
| 10 | import * as Effect from "effect/Effect" |
| 11 | import * as Exit from "effect/Exit" |
| 12 | import * as HttpRouter from "effect/http/HttpRouter" |
| 13 | import * as HttpServerResponse from "effect/http/HttpServerResponse" |
| 14 | import * as Layer from "effect/Layer" |
| 15 | import * as Option from "effect/Option" |
| 16 | import * as EffectTracer from "effect/Tracer" |
| 17 | import * as Version from "effect/Version" |
| 18 | |
| 19 | const TracingLayer = OtelTracer.layer.pipe( |
| 20 | Layer.provide(NodeSdk.layerTracerProvider([new SimpleSpanProcessor(new InMemorySpanExporter())])), |
| 21 | Layer.provide(Resource.layerEmpty) |
| 22 | ) |
| 23 | |
| 24 | // needed to test context propagation |
| 25 | const contextManager = new AsyncHooksContextManager() |
| 26 | OtelApi.context.setGlobalContextManager(contextManager.enable()) |
| 27 | |
| 28 | describe("Tracer", () => { |
| 29 | it.effect("uses the shared Effect version as the tracer instrumentation scope", () => { |
| 30 | const exporter = new InMemorySpanExporter() |
| 31 | const layer = NodeSdk.layer(Effect.sync(() => ({ |
| 32 | resource: { serviceName: "test", serviceVersion: "service-version" }, |
| 33 | spanProcessor: [new SimpleSpanProcessor(exporter)] |
| 34 | }))) |
| 35 | return Effect.gen(function*() { |
| 36 | yield* Effect.void.pipe(Effect.withSpan("test")) |
| 37 | const scope = exporter.getFinishedSpans()[0]!.instrumentationScope |
| 38 | assert.strictEqual(scope.name, "effect") |
| 39 | assert.strictEqual(scope.version, Version.getCurrentVersion()) |
| 40 | }).pipe(Effect.provide(layer)) |
| 41 | }) |
| 42 | |
| 43 | it("exports the HTTP server span when disposed right after the response", async () => { |
| 44 | const exported: Array<string> = [] |
| 45 | const exporter: SpanExporter = { |
| 46 | export: (spans, done) => { |
| 47 | exported.push(...spans.map((span) => span.name)) |
| 48 | done({ code: 0 }) |
| 49 | }, |
| 50 | shutdown: () => Promise.resolve() |
| 51 | } |
| 52 | const { dispose, handler } = HttpRouter.toWebHandler( |
| 53 | HttpRouter.add("GET", "/", Effect.succeed(HttpServerResponse.text("ok"))).pipe( |
| 54 | Layer.provideMerge(NodeSdk.layer(() => ({ |
| 55 | resource: { serviceName: "test" }, |
| 56 | spanProcessor: new SimpleSpanProcessor(exporter) |
| 57 | }))) |
| 58 | ), |
| 59 | { disableLogger: true } |
| 60 | ) |
| 61 | |
| 62 | const response = await handler(new Request("http://localhost/")) |
| 63 | assert.strictEqual(await response.text(), "ok") |
| 64 | await dispose() |
| 65 | |
| 66 | assert.deepStrictEqual(exported, ["GET"]) |
| 67 | }) |
| 68 | |
| 69 | describe("provided", () => { |
| 70 | it.effect("withSpan", () => |
| 71 | Effect.gen(function*() { |
| 72 | const span = yield* Effect.currentSpan |
| 73 | assert.instanceOf(span, OtelTracer.OtelSpan) |
| 74 | }).pipe( |
| 75 | Effect.withSpan("ok"), |
| 76 | Effect.provide(TracingLayer) |
| 77 | )) |
| 78 | |
| 79 | it.effect("withSpan links", () => |
| 80 | Effect.gen(function*() { |
| 81 | const linkedSpan = yield* Effect.makeSpanScoped("B") |
| 82 | const span = yield* Effect.currentSpan.pipe( |
| 83 | Effect.withSpan("A"), |
| 84 | Effect.linkSpans(linkedSpan) |
| 85 | ) |
| 86 | assert.instanceOf(span, OtelTracer.OtelSpan) |
| 87 | assert.lengthOf(span.links, 1) |
| 88 | }).pipe( |
| 89 | Effect.provide(TracingLayer) |
| 90 | )) |
| 91 | |
| 92 | it.effect("nested withSpan sets correct parent chain", () => |
| 93 | Effect.gen(function*() { |
| 94 | const child = yield* Effect.currentSpan.pipe( |
| 95 | Effect.withSpan("child"), |
| 96 | Effect.withSpan("parent") |
| 97 | ) |
| 98 | assert.instanceOf(child, OtelTracer.OtelSpan) |
| 99 | assert.strictEqual(child.name, "child") |
| 100 | assert.isDefined(child.parent) |
| 101 | assert.strictEqual((child.parent.valueOrUndefined! as OtelTracer.OtelSpan).name, "parent") |
| 102 | }).pipe( |
| 103 | Effect.provide(TracingLayer) |
| 104 | )) |
| 105 | |
| 106 | it.effect("inherits an OpenTelemetry span started inside an Effect span", () => |
| 107 | Effect.gen(function*() { |
| 108 | const services = yield* Effect.context<never>() |
| 109 | const tracer = yield* OtelTracer.OtelTracer |
| 110 | yield* Effect.promise(() => |
| 111 | tracer.startActiveSpan("otel-parent", async (parent) => { |
| 112 | try { |
| 113 | const child = await Effect.runPromise(Effect.currentSpan.pipe( |
| 114 | Effect.withSpan("child"), |
| 115 | Effect.provideContext(services) |
| 116 | )) |
| 117 | |
| 118 | assert.strictEqual(child.traceId, parent.spanContext().traceId) |
| 119 | assert.strictEqual(Option.getOrThrow(child.parent).spanId, parent.spanContext().spanId) |
| 120 | } finally { |
| 121 | parent.end() |
| 122 | } |
| 123 | }) |
| 124 | ).pipe(Effect.withSpan("outer")) |
| 125 | }).pipe(Effect.provide(TracingLayer))) |
| 126 | |
| 127 | it.effect("does not inherit an ambient Effect span when re-entering from JavaScript", () => |
| 128 | Effect.gen(function*() { |
| 129 | const services = yield* Effect.context<never>() |
| 130 | yield* Effect.gen(function*() { |
| 131 | const outer = yield* Effect.currentSpan |
| 132 | const child = yield* Effect.promise(() => |
| 133 | Effect.runPromise(Effect.currentSpan.pipe( |
| 134 | Effect.withSpan("child"), |
| 135 | Effect.provideContext(services) |
| 136 | )) |
| 137 | ) |
| 138 | |
| 139 | assert.isTrue(Option.isNone(child.parent)) |
| 140 | assert.notStrictEqual(child.traceId, outer.traceId) |
| 141 | }).pipe(Effect.withSpan("outer")) |
| 142 | }).pipe(Effect.provide(TracingLayer))) |
| 143 | |
| 144 | it.effect("a propagation-disabled Effect span is not a parent", () => |
| 145 | Effect.gen(function*() { |
| 146 | const child = yield* Effect.currentSpan.pipe( |
| 147 | Effect.withSpan("child"), |
| 148 | Effect.withSpan("disabled", { annotations: EffectTracer.DisablePropagation.context(true) }) |
| 149 | ) |
| 150 | |
| 151 | assert.isTrue(Option.isNone(child.parent)) |
| 152 | }).pipe(Effect.provide(TracingLayer))) |
| 153 | |
| 154 | it.effect("supervisor sets context", () => |
| 155 | Effect.sync(() => { |
| 156 | const context = OtelApi.context.active() |
| 157 | assert.isDefined(OtelApi.trace.getSpan(context)) |
| 158 | }).pipe( |
| 159 | Effect.withSpan("ok"), |
| 160 | Effect.provide(TracingLayer) |
| 161 | )) |
| 162 | |
| 163 | it.effect("supervisor sets context generator", () => |
| 164 | Effect.gen(function*() { |
| 165 | yield* Effect.yieldNow |
| 166 | const context = OtelApi.context.active() |
| 167 | assert.isDefined(OtelApi.trace.getSpan(context)) |
| 168 | }).pipe( |
| 169 | Effect.withSpan("ok"), |
| 170 | Effect.provide(TracingLayer) |
| 171 | )) |
| 172 | |
| 173 | it.effect("currentOtelSpan", () => |
| 174 | Effect.gen(function*() { |
| 175 | const span = yield* Effect.currentSpan |
| 176 | const otelSpan = yield* OtelTracer.currentOtelSpan |
| 177 | assert.strictEqual((span as OtelTracer.OtelSpan).span, otelSpan) |
| 178 | }).pipe( |
| 179 | Effect.withSpan("ok"), |
| 180 | Effect.provide(TracingLayer) |
| 181 | )) |
| 182 | |
| 183 | it.effect.each([OtelApi.SpanStatusCode.UNSET, OtelApi.SpanStatusCode.OK])( |
| 184 | "honors non-error wrapper status %s", |
| 185 | (status) => |
| 186 | Effect.gen(function*() { |
| 187 | const span = yield* Effect.currentSpan |
| 188 | const wrapper = yield* OtelTracer.currentOtelSpan |
| 189 | wrapper.setStatus({ code: status }) |
| 190 | wrapper.end() |
| 191 | assert.strictEqual(span.status._tag, "Ended") |
| 192 | if (span.status._tag === "Ended") { |
| 193 | assert.strictEqual(span.status.exit._tag, "Success") |
| 194 | } |
| 195 | }).pipe(Effect.withSpan("repro")) |
| 196 | ) |
| 197 | |
| 198 | it.effect("honors an error wrapper status", () => |
| 199 | Effect.gen(function*() { |
| 200 | const span = yield* Effect.currentSpan |
| 201 | const wrapper = yield* OtelTracer.currentOtelSpan |
| 202 | wrapper.setStatus({ code: OtelApi.SpanStatusCode.ERROR }) |
| 203 | wrapper.end() |
| 204 | assert.strictEqual(span.status._tag, "Ended") |
| 205 | if (span.status._tag === "Ended") { |
| 206 | assert.strictEqual(span.status.exit._tag, "Failure") |
| 207 | } |
| 208 | }).pipe(Effect.withSpan("repro"))) |
| 209 | |
| 210 | it.effect("preserves the sampling decision of generic external spans", () => |
| 211 | Effect.gen(function*() { |
| 212 | const span = yield* Effect.currentSpan |
| 213 | assert.instanceOf(span, OtelTracer.OtelSpan) |
| 214 | assert.strictEqual(span.span.spanContext().traceFlags, OtelApi.TraceFlags.NONE) |
| 215 | }).pipe( |
| 216 | Effect.withSpan("child"), |
| 217 | Effect.withParentSpan(EffectTracer.externalSpan({ |
| 218 | traceId: "1".repeat(32), |
| 219 | spanId: "2".repeat(16), |
| 220 | sampled: false |
| 221 | })), |
| 222 | Effect.provide(TracingLayer) |
| 223 | )) |
| 224 | |
| 225 | it("preserves identity, trace state and locality on an active OpenTelemetry parent", () => { |
| 226 | const parent: OtelApi.SpanContext = { |
| 227 | traceId: "1".repeat(32), |
| 228 | spanId: "2".repeat(16), |
| 229 | traceFlags: OtelApi.TraceFlags.SAMPLED, |
| 230 | traceState: OtelApi.createTraceState("vendor=value"), |
| 231 | isRemote: false |
| 232 | } |
| 233 | const active = OtelApi.trace.setSpanContext(OtelApi.ROOT_CONTEXT, parent) |
| 234 | let receivedParent: OtelApi.SpanContext | undefined |
| 235 | const tracer = { |
| 236 | startSpan(_name: string, _options: unknown, context: OtelApi.Context) { |
| 237 | assert.strictEqual(OtelApi.trace.getSpan(context), OtelApi.trace.getSpan(active)) |
| 238 | receivedParent = OtelApi.trace.getSpanContext(context) |
| 239 | return { |
| 240 | spanContext: () => ({ |
| 241 | traceId: "3".repeat(32), |
| 242 | spanId: "4".repeat(16), |
| 243 | traceFlags: OtelApi.TraceFlags.SAMPLED |
| 244 | }) |
| 245 | } as OtelApi.Span |
| 246 | } |
| 247 | } as OtelApi.Tracer |
| 248 | |
| 249 | const child = new OtelTracer.OtelSpan( |
| 250 | { active: () => active } as OtelApi.ContextAPI, |
| 251 | OtelApi.trace, |
| 252 | tracer, |
| 253 | { |
| 254 | name: "child", |
| 255 | parent: Option.none(), |
| 256 | annotations: EffectContext.empty(), |
| 257 | links: [], |
| 258 | startTime: 0n, |
| 259 | kind: "internal", |
| 260 | root: false, |
| 261 | sampled: true |
| 262 | } |
| 263 | ) |
| 264 | |
| 265 | assert.instanceOf(child, OtelTracer.OtelSpan) |
| 266 | assert.deepStrictEqual( |
| 267 | [receivedParent?.traceState?.serialize(), receivedParent?.isRemote], |
| 268 | ["vendor=value", false] |
| 269 | ) |
| 270 | }) |
| 271 | |
| 272 | it.effect.each([OtelApi.TraceFlags.SAMPLED, OtelApi.TraceFlags.NONE])( |
| 273 | "inherits getter-based OpenTelemetry parent ids with trace flags %s", |
| 274 | (traceFlags) => |
| 275 | Effect.gen(function*() { |
| 276 | class GetterSpanContext implements OtelApi.SpanContext { |
| 277 | get traceId() { |
| 278 | return "1".repeat(32) |
| 279 | } |
| 280 | get spanId() { |
| 281 | return "2".repeat(16) |
| 282 | } |
| 283 | get traceFlags() { |
| 284 | return OtelApi.TraceFlags.SAMPLED |
| 285 | } |
| 286 | } |
| 287 | const parent = new GetterSpanContext() |
| 288 | const active = OtelApi.trace.setSpanContext(OtelApi.ROOT_CONTEXT, parent) |
| 289 | const services = yield* Effect.context<never>() |
| 290 | const child = yield* Effect.promise(() => |
| 291 | OtelApi.context.with(active, () => |
| 292 | Effect.runPromise(Effect.currentSpan.pipe( |
| 293 | Effect.withSpan("child", { |
| 294 | annotations: traceFlags === OtelApi.TraceFlags.NONE |
| 295 | ? OtelTracer.OtelTraceFlags.context(traceFlags) |
| 296 | : EffectContext.empty() |
| 297 | }), |
| 298 | Effect.provideContext(services) |
| 299 | ))) |
| 300 | ) |
| 301 | assert.strictEqual(child.traceId, parent.traceId) |
| 302 | assert.strictEqual(Option.getOrThrow(child.parent).spanId, parent.spanId) |
| 303 | assert.strictEqual((child as OtelTracer.OtelSpan).span.spanContext().traceFlags, traceFlags) |
| 304 | }).pipe(Effect.provide(TracingLayer)) |
| 305 | ) |
| 306 | |
| 307 | it.effect("records every pretty error", () => |
| 308 | Effect.gen(function*() { |
| 309 | const exporter = new InMemorySpanExporter() |
| 310 | const spanProcessor = new SimpleSpanProcessor(exporter) |
| 311 | const firstFailure = Cause.fail(new Error("first")) |
| 312 | const secondFailure = Cause.fail(new Error("second")) |
| 313 | const cause = Cause.combine(firstFailure, secondFailure) |
| 314 | |
| 315 | yield* Effect.failCause(cause).pipe( |
| 316 | Effect.withSpan("error-span"), |
| 317 | Effect.andThen(Effect.never), // keep the exporter alive |
| 318 | Effect.provide(NodeSdk.layer(() => ({ |
| 319 | resource: { |
| 320 | serviceName: "test" |
| 321 | }, |
| 322 | spanProcessor: [spanProcessor] |
| 323 | }))), |
| 324 | Effect.forkChild({ startImmediately: true }) |
| 325 | ) |
| 326 | |
| 327 | const spanData = exporter.getFinishedSpans()[0] |
| 328 | if (spanData === undefined) { |
| 329 | return yield* Effect.die("Missing span data") |
| 330 | } |
| 331 | const exceptionEvents = spanData.events.filter((event) => event.name === "exception") |
| 332 | assert.lengthOf(exceptionEvents, 2) |
| 333 | assert.strictEqual(spanData.status.message, "first") |
| 334 | })) |
| 335 | |
| 336 | it.effect("renders nested error causes in the stacktrace", () => |
| 337 | Effect.gen(function*() { |
| 338 | const exporter = new InMemorySpanExporter() |
| 339 | const spanProcessor = new SimpleSpanProcessor(exporter) |
| 340 | const error = new Error("outer failure", { cause: new Error("inner cause") }) |
| 341 | |
| 342 | yield* Effect.die(error).pipe( |
| 343 | Effect.withSpan("error-span"), |
| 344 | Effect.andThen(Effect.never), // keep the exporter alive |
| 345 | Effect.provide(NodeSdk.layer(() => ({ |
| 346 | resource: { |
| 347 | serviceName: "test" |
| 348 | }, |
| 349 | spanProcessor: [spanProcessor] |
| 350 | }))), |
| 351 | Effect.forkChild({ startImmediately: true }) |
| 352 | ) |
| 353 | |
| 354 | const spanData = exporter.getFinishedSpans()[0] |
| 355 | if (spanData === undefined) { |
| 356 | return yield* Effect.die("Missing span data") |
| 357 | } |
| 358 | const exceptionEvent = spanData.events.find((event) => event.name === "exception") |
| 359 | assert(exceptionEvent !== undefined) |
| 360 | const stacktrace = exceptionEvent.attributes?.["exception.stacktrace"] |
| 361 | assert.isString(stacktrace) |
| 362 | assert.include(stacktrace as string, "[cause]: Error: inner cause") |
| 363 | })) |
| 364 | |
| 365 | it.effect("leaves interruption Unset with effect.fiber.interrupted", () => { |
| 366 | const exporter = new InMemorySpanExporter() |
| 367 | return Effect.gen(function*() { |
| 368 | const span = yield* Effect.makeSpan("test") |
| 369 | span.end(span.status.startTime + 1n, Exit.interrupt()) |
| 370 | const spans = exporter.getFinishedSpans() |
| 371 | assert.lengthOf(spans, 1) |
| 372 | assert.deepStrictEqual(spans[0].status, { code: OtelApi.SpanStatusCode.UNSET }) |
| 373 | assert.deepStrictEqual(spans[0].events, []) |
| 374 | assert.deepStrictEqual(spans[0].attributes, { "effect.fiber.interrupted": true }) |
| 375 | }).pipe(Effect.provide(NodeSdk.layer(() => ({ |
| 376 | resource: { serviceName: "test" }, |
| 377 | spanProcessor: [new SimpleSpanProcessor(exporter)] |
| 378 | })))) |
| 379 | }) |
| 380 | it.effect("exports homogeneous primitive arrays as array attributes", () => |
| 381 | Effect.gen(function*() { |
| 382 | const exporter = new InMemorySpanExporter() |
| 383 | const spanProcessor = new SimpleSpanProcessor(exporter) |
| 384 | |
| 385 | yield* Effect.void.pipe( |
| 386 | Effect.withSpan("array-span", { |
| 387 | attributes: { |
| 388 | strings: ["a", "b"], |
| 389 | numbers: [1, 2], |
| 390 | booleans: [true, false], |
| 391 | mixed: [1, "a"] |
| 392 | } |
| 393 | }), |
| 394 | Effect.andThen(Effect.never), // keep the exporter alive |
| 395 | Effect.provide(NodeSdk.layer(() => ({ |
| 396 | resource: { |
| 397 | serviceName: "test" |
| 398 | }, |
| 399 | spanProcessor: [spanProcessor] |
| 400 | }))), |
| 401 | Effect.forkChild({ startImmediately: true }) |
| 402 | ) |
| 403 | |
| 404 | const spanData = exporter.getFinishedSpans()[0] |
| 405 | if (spanData === undefined) { |
| 406 | return yield* Effect.die("Missing span data") |
| 407 | } |
| 408 | assert.deepStrictEqual(spanData.attributes.strings, ["a", "b"]) |
| 409 | assert.deepStrictEqual(spanData.attributes.numbers, [1, 2]) |
| 410 | assert.deepStrictEqual(spanData.attributes.booleans, [true, false]) |
| 411 | assert.isString(spanData.attributes.mixed) |
| 412 | })) |
| 413 | |
| 414 | it.effect("withSpanContext", () => |
| 415 | Effect.gen(function*() { |
| 416 | const effect = Effect.gen(function*() { |
| 417 | const span = yield* Effect.currentParentSpan |
| 418 | assert(span._tag === "Span") |
| 419 | if (span.parent._tag === "None") { |
| 420 | return yield* Effect.die("No parent span") |
| 421 | } |
| 422 | return span.parent.value |
| 423 | }).pipe(Effect.withSpan("child")) |
| 424 | |
| 425 | const services = yield* Effect.context<never>() |
| 426 | |
| 427 | yield* Effect.promise(async () => { |
| 428 | await OtelApi.trace.getTracer("test").startActiveSpan("otel-span", { |
| 429 | root: true, |
| 430 | attributes: { "root": "yes" } |
| 431 | }, async (span) => { |
| 432 | try { |
| 433 | const parent = await effect.pipe( |
| 434 | OtelTracer.withSpanContext(span.spanContext()), |
| 435 | Effect.provideContext(services), |
| 436 | Effect.runPromise |
| 437 | ) |
| 438 | const { spanId, traceId } = span.spanContext() |
| 439 | assert.containsSubset(parent, { |
| 440 | spanId, |
| 441 | traceId |
| 442 | }) |
| 443 | } finally { |
| 444 | span.end() |
| 445 | } |
| 446 | }) |
| 447 | }) |
| 448 | }).pipe( |
| 449 | Effect.provide(TracingLayer) |
| 450 | )) |
| 451 | }) |
| 452 | |
| 453 | describe("not provided", () => { |
| 454 | it.effect("withSpan", () => |
| 455 | Effect.gen(function*() { |
| 456 | const span = yield* Effect.currentSpan |
| 457 | assert.notInstanceOf(span, OtelTracer.OtelSpan) |
| 458 | }).pipe( |
| 459 | Effect.withSpan("ok") |
| 460 | )) |
| 461 | }) |
| 462 | }) |