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 OtelLogger from "@effect/opentelemetry/OtelLogger" |
| 3 | import * as OtelTracer from "@effect/opentelemetry/OtelTracer" |
| 4 | import * as Resource from "@effect/opentelemetry/Resource" |
| 5 | import { assert, describe, it } from "@effect/vitest" |
| 6 | import * as OtelApi from "@opentelemetry/api" |
| 7 | import { SeverityNumber } from "@opentelemetry/api-logs" |
| 8 | import { AsyncHooksContextManager } from "@opentelemetry/context-async-hooks" |
| 9 | import { InMemoryLogRecordExporter, type LogRecordProcessor, SimpleLogRecordProcessor } from "@opentelemetry/sdk-logs" |
| 10 | import { InMemorySpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base" |
| 11 | import * as Cause from "effect/Cause" |
| 12 | import * as Clock from "effect/Clock" |
| 13 | import * as ConfigProvider from "effect/ConfigProvider" |
| 14 | import * as Context from "effect/Context" |
| 15 | import * as Effect from "effect/Effect" |
| 16 | import * as Layer from "effect/Layer" |
| 17 | import * as References from "effect/References" |
| 18 | import { TestClock } from "effect/testing" |
| 19 | import * as Tracer from "effect/Tracer" |
| 20 | import * as Version from "effect/Version" |
| 21 | |
| 22 | describe("Logger", () => { |
| 23 | const makeTestLayer = (exporter: InMemoryLogRecordExporter) => |
| 24 | NodeSdk.layer(Effect.sync(() => ({ |
| 25 | resource: { serviceName: "test", serviceVersion: "service-version" }, |
| 26 | logRecordProcessor: [new SimpleLogRecordProcessor({ exporter })] |
| 27 | }))) |
| 28 | |
| 29 | it.effect("adds SDK resource defaults when the resource comes only from the environment", () => { |
| 30 | const exporter = new InMemoryLogRecordExporter() |
| 31 | return Effect.gen(function*() { |
| 32 | yield* Effect.log("test") |
| 33 | const record = exporter.getFinishedLogRecords()[0]! |
| 34 | assert.strictEqual(record.instrumentationScope.name, "effect") |
| 35 | assert.strictEqual(record.instrumentationScope.version, Version.getCurrentVersion()) |
| 36 | assert.deepStrictEqual(record.resource.attributes, { |
| 37 | "service.name": "env-service", |
| 38 | "telemetry.sdk.name": "@effect/opentelemetry", |
| 39 | "telemetry.sdk.language": "nodejs", |
| 40 | "telemetry.sdk.version": Version.getCurrentVersion() |
| 41 | }) |
| 42 | }).pipe( |
| 43 | Effect.provide(NodeSdk.layer(() => ({ logRecordProcessor: [new SimpleLogRecordProcessor({ exporter })] }))), |
| 44 | Effect.provideService( |
| 45 | ConfigProvider.ConfigProvider, |
| 46 | ConfigProvider.fromEnv({ env: { OTEL_SERVICE_NAME: "env-service" } }) |
| 47 | ) |
| 48 | ) |
| 49 | }) |
| 50 | |
| 51 | it.effect("namespaces generated attributes and lets them override annotations", () => { |
| 52 | const exporter = new InMemoryLogRecordExporter() |
| 53 | return Effect.gen(function*() { |
| 54 | const fiber = yield* Effect.fiber |
| 55 | yield* Effect.gen(function*() { |
| 56 | yield* TestClock.adjust("5 millis") |
| 57 | yield* Effect.logError( |
| 58 | "boom", |
| 59 | Cause.fail(new TypeError("cause message", { cause: new Error("nested failure") })) |
| 60 | ) |
| 61 | }).pipe( |
| 62 | Effect.withLogSpan("op"), |
| 63 | Effect.annotateLogs({ |
| 64 | "effect.fiberId": -1, |
| 65 | "effect.log_span.op": -1, |
| 66 | "exception.type": "annotated type", |
| 67 | "exception.message": "annotated message", |
| 68 | "exception.stacktrace": "annotated stack" |
| 69 | }) |
| 70 | ) |
| 71 | |
| 72 | const attributes = exporter.getFinishedLogRecords()[0]!.attributes |
| 73 | assert.strictEqual(attributes["effect.fiberId"], fiber.id) |
| 74 | assert.strictEqual(attributes["effect.log_span.op"], 5) |
| 75 | assert.strictEqual(attributes["exception.type"], "TypeError") |
| 76 | assert.strictEqual(attributes["exception.message"], "cause message") |
| 77 | assert.include(attributes["exception.stacktrace"], "nested failure") |
| 78 | }).pipe(Effect.provide(makeTestLayer(exporter))) |
| 79 | }) |
| 80 | |
| 81 | it.effect("shuts down the logger provider after forceFlush rejects", () => |
| 82 | Effect.gen(function*() { |
| 83 | let shutdowns = 0 |
| 84 | const processor: LogRecordProcessor = { |
| 85 | onEmit() {}, |
| 86 | forceFlush: () => Promise.reject(new Error("flush failed")), |
| 87 | shutdown: () => { |
| 88 | shutdowns++ |
| 89 | return Promise.resolve() |
| 90 | } |
| 91 | } |
| 92 | yield* Effect.exit( |
| 93 | Effect.scoped(Layer.build(OtelLogger.layerLoggerProvider(processor)).pipe(Effect.provide(Resource.layerEmpty))) |
| 94 | ) |
| 95 | assert.strictEqual(shutdowns, 1) |
| 96 | })) |
| 97 | |
| 98 | describe("provided", () => { |
| 99 | const exporter = new InMemoryLogRecordExporter() |
| 100 | |
| 101 | const TracingLayer = NodeSdk.layer(Effect.sync(() => ({ |
| 102 | resource: { |
| 103 | serviceName: "test" |
| 104 | }, |
| 105 | logRecordProcessor: [new SimpleLogRecordProcessor({ exporter })] |
| 106 | }))) |
| 107 | |
| 108 | it.effect("emits log records", () => |
| 109 | Effect.gen(function*() { |
| 110 | yield* Effect.log("test").pipe( |
| 111 | Effect.repeat({ times: 9 }) |
| 112 | ) |
| 113 | assert.lengthOf(exporter.getFinishedLogRecords(), 10) |
| 114 | const log = exporter.getFinishedLogRecords()[0]! |
| 115 | assert.isUndefined(log.spanContext) |
| 116 | assert.isUndefined(log.attributes.traceId) |
| 117 | assert.isUndefined(log.attributes.spanId) |
| 118 | }).pipe(Effect.provide(TracingLayer))) |
| 119 | |
| 120 | it.effect("maps Effect LogLevel to OTel SeverityNumber spec values", () => { |
| 121 | const severityExporter = new InMemoryLogRecordExporter() |
| 122 | const SeverityLayer = NodeSdk.layer(Effect.sync(() => ({ |
| 123 | resource: { serviceName: "test" }, |
| 124 | logRecordProcessor: [new SimpleLogRecordProcessor({ exporter: severityExporter })] |
| 125 | }))) |
| 126 | |
| 127 | return Effect.gen(function*() { |
| 128 | yield* Effect.logTrace("trace") |
| 129 | yield* Effect.logDebug("debug") |
| 130 | yield* Effect.logInfo("info") |
| 131 | yield* Effect.logWarning("warn") |
| 132 | yield* Effect.logError("error") |
| 133 | yield* Effect.logFatal("fatal") |
| 134 | |
| 135 | const records = severityExporter.getFinishedLogRecords() |
| 136 | const byText = Object.fromEntries(records.map((r) => [r.severityText, r.severityNumber])) |
| 137 | |
| 138 | assert.strictEqual(byText.Trace, SeverityNumber.TRACE) |
| 139 | assert.strictEqual(byText.Debug, SeverityNumber.DEBUG) |
| 140 | assert.strictEqual(byText.Info, SeverityNumber.INFO) |
| 141 | assert.strictEqual(byText.Warn, SeverityNumber.WARN) |
| 142 | assert.strictEqual(byText.Error, SeverityNumber.ERROR) |
| 143 | assert.strictEqual(byText.Fatal, SeverityNumber.FATAL) |
| 144 | }).pipe( |
| 145 | Effect.provide(SeverityLayer.pipe(Layer.provideMerge(Layer.succeed(References.MinimumLogLevel, "Trace")))) |
| 146 | ) |
| 147 | }) |
| 148 | |
| 149 | it.effect("keeps event time separate from observed time and uses wall-clock span time", () => { |
| 150 | const logExporter = new InMemoryLogRecordExporter() |
| 151 | const spanExporter = new InMemorySpanExporter() |
| 152 | const wallTimeNanos = 1_735_689_600_123_456_789n |
| 153 | const monotonicTimeNanos = 123_456_789n |
| 154 | const expectedTime: readonly [number, number] = [ |
| 155 | Number(wallTimeNanos / BigInt(1_000_000_000)), |
| 156 | Number(wallTimeNanos % BigInt(1_000_000_000)) |
| 157 | ] |
| 158 | const skewedClock: Clock.Clock = { |
| 159 | currentTimeMillisUnsafe: () => 1, |
| 160 | currentTimeMillis: Effect.succeed(1), |
| 161 | currentTimeNanosUnsafe: () => wallTimeNanos, |
| 162 | currentTimeNanos: Effect.succeed(wallTimeNanos), |
| 163 | monotonicTimeNanosUnsafe: () => monotonicTimeNanos, |
| 164 | monotonicTimeNanos: Effect.succeed(monotonicTimeNanos), |
| 165 | sleep: () => Effect.void |
| 166 | } |
| 167 | |
| 168 | const TracingLayer = NodeSdk.layer(Effect.sync(() => ({ |
| 169 | resource: { |
| 170 | serviceName: "test" |
| 171 | }, |
| 172 | spanProcessor: [new SimpleSpanProcessor(spanExporter)], |
| 173 | logRecordProcessor: [new SimpleLogRecordProcessor({ exporter: logExporter })] |
| 174 | }))) |
| 175 | |
| 176 | return Effect.gen(function*() { |
| 177 | yield* Effect.log("test").pipe( |
| 178 | Effect.withSpan("parent") |
| 179 | ) |
| 180 | |
| 181 | const logs = logExporter.getFinishedLogRecords() |
| 182 | const spans = spanExporter.getFinishedSpans() |
| 183 | assert.lengthOf(logs, 1) |
| 184 | assert.lengthOf(spans, 1) |
| 185 | const log = logs[0]! |
| 186 | const span = spans[0]! |
| 187 | |
| 188 | assert.deepStrictEqual(log.hrTime, [0, 1_000_000]) |
| 189 | assert.deepStrictEqual(log.hrTimeObserved, expectedTime) |
| 190 | assert.deepStrictEqual(log.hrTimeObserved, span.startTime) |
| 191 | assert.strictEqual(log.attributes.spanId, span.spanContext().spanId) |
| 192 | assert.strictEqual(log.attributes.traceId, span.spanContext().traceId) |
| 193 | }).pipe( |
| 194 | Effect.provide(TracingLayer), |
| 195 | Effect.provideService(Clock.Clock, skewedClock) |
| 196 | ) |
| 197 | }) |
| 198 | |
| 199 | it.effect("does not let annotations overwrite active span correlation", () => { |
| 200 | const logExporter = new InMemoryLogRecordExporter() |
| 201 | const spanExporter = new InMemorySpanExporter() |
| 202 | const TracingLayer = NodeSdk.layer(Effect.sync(() => ({ |
| 203 | resource: { serviceName: "test" }, |
| 204 | spanProcessor: [new SimpleSpanProcessor(spanExporter)], |
| 205 | logRecordProcessor: [new SimpleLogRecordProcessor({ exporter: logExporter })] |
| 206 | }))) |
| 207 | |
| 208 | return Effect.gen(function*() { |
| 209 | yield* Effect.log("test").pipe( |
| 210 | Effect.annotateLogs({ traceId: "spoof-trace", spanId: "spoof-span" }), |
| 211 | Effect.withSpan("parent") |
| 212 | ) |
| 213 | |
| 214 | const log = logExporter.getFinishedLogRecords()[0]! |
| 215 | const span = spanExporter.getFinishedSpans()[0]! |
| 216 | assert.strictEqual(log.attributes.traceId, span.spanContext().traceId) |
| 217 | assert.strictEqual(log.attributes.spanId, span.spanContext().spanId) |
| 218 | assert.deepStrictEqual(log.spanContext, span.spanContext()) |
| 219 | }).pipe(Effect.provide(TracingLayer)) |
| 220 | }) |
| 221 | |
| 222 | it.effect("correlates a log with an external parent span", () => { |
| 223 | const logExporter = new InMemoryLogRecordExporter() |
| 224 | const traceId = "ab".repeat(16) |
| 225 | const spanId = "cd".repeat(8) |
| 226 | |
| 227 | return Effect.gen(function*() { |
| 228 | yield* Effect.log("test").pipe( |
| 229 | Effect.withParentSpan(Tracer.externalSpan({ |
| 230 | traceId, |
| 231 | spanId, |
| 232 | sampled: false, |
| 233 | annotations: OtelTracer.OtelTraceFlags.context(0x02 as OtelApi.TraceFlags).pipe( |
| 234 | Context.add(OtelTracer.OtelTraceState, OtelApi.createTraceState("vendor=value")) |
| 235 | ) |
| 236 | })) |
| 237 | ) |
| 238 | const log = logExporter.getFinishedLogRecords()[0]! |
| 239 | assert.strictEqual(log.spanContext?.traceId, traceId) |
| 240 | assert.strictEqual(log.spanContext?.spanId, spanId) |
| 241 | assert.strictEqual(log.spanContext?.traceFlags, 0x02) |
| 242 | assert.strictEqual(log.spanContext?.traceState?.serialize(), "vendor=value") |
| 243 | assert.isTrue(log.spanContext?.isRemote) |
| 244 | }).pipe(Effect.provide(makeTestLayer(logExporter))) |
| 245 | }) |
| 246 | |
| 247 | it("uses ambient context only when there is no Effect parent span", async () => { |
| 248 | const contextManager = new AsyncHooksContextManager() |
| 249 | OtelApi.context.setGlobalContextManager(contextManager.enable()) |
| 250 | try { |
| 251 | const logExporter = new InMemoryLogRecordExporter() |
| 252 | const ambientSpan: OtelApi.SpanContext = { |
| 253 | traceId: "ab".repeat(16), |
| 254 | spanId: "cd".repeat(8), |
| 255 | traceFlags: OtelApi.TraceFlags.SAMPLED |
| 256 | } |
| 257 | const effectSpan = Tracer.externalSpan({ traceId: "ef".repeat(16), spanId: "12".repeat(8) }) |
| 258 | const active = OtelApi.trace.setSpanContext(OtelApi.ROOT_CONTEXT, ambientSpan) |
| 259 | await OtelApi.context.with(active, () => |
| 260 | Effect.runPromise( |
| 261 | Effect.gen(function*() { |
| 262 | yield* Effect.log("ambient") |
| 263 | yield* Effect.log("effect").pipe(Effect.withParentSpan(effectSpan)) |
| 264 | const logs = logExporter.getFinishedLogRecords() |
| 265 | assert.lengthOf(logs, 2) |
| 266 | assert.deepStrictEqual(logs[0]!.spanContext, ambientSpan) |
| 267 | assert.isUndefined(logs[0]!.attributes.traceId) |
| 268 | assert.isUndefined(logs[0]!.attributes.spanId) |
| 269 | assert.strictEqual(logs[1]!.spanContext?.traceId, effectSpan.traceId) |
| 270 | assert.strictEqual(logs[1]!.spanContext?.spanId, effectSpan.spanId) |
| 271 | }).pipe(Effect.provide(makeTestLayer(logExporter))) |
| 272 | )) |
| 273 | } finally { |
| 274 | OtelApi.context.disable() |
| 275 | contextManager.disable() |
| 276 | } |
| 277 | }) |
| 278 | }) |
| 279 | |
| 280 | describe("not provided", () => { |
| 281 | const exporter = new InMemoryLogRecordExporter() |
| 282 | |
| 283 | const TracingLayer = NodeSdk.layer(Effect.sync(() => ({ |
| 284 | resource: { |
| 285 | serviceName: "test" |
| 286 | } |
| 287 | }))) |
| 288 | |
| 289 | it.effect("withSpan", () => |
| 290 | Effect.gen(function*() { |
| 291 | yield* Effect.log("test") |
| 292 | assert.lengthOf(exporter.getFinishedLogRecords(), 0) |
| 293 | }).pipe(Effect.provide(TracingLayer))) |
| 294 | }) |
| 295 | }) |