|
1 | 1 | import * as OtelApi from "@opentelemetry/api" |
2 | 2 | import * as OtelSemConv from "@opentelemetry/semantic-conventions" |
3 | 3 | import * as Cause from "effect/Cause" |
| 4 | +import type * as Clock from "effect/Clock" |
4 | 5 | import * as Context from "effect/Context" |
5 | 6 | import * as Effect from "effect/Effect" |
6 | | -import type { Exit } from "effect/Exit" |
7 | | -import { dual } from "effect/Function" |
| 7 | +import * as Exit from "effect/Exit" |
| 8 | +import { constTrue, dual } from "effect/Function" |
8 | 9 | import * as Layer from "effect/Layer" |
9 | 10 | import * as Option from "effect/Option" |
10 | 11 | import * as EffectTracer from "effect/Tracer" |
@@ -109,7 +110,7 @@ export class OtelSpan implements EffectTracer.Span { |
109 | 110 | }))) |
110 | 111 | } |
111 | 112 |
|
112 | | - end(endTime: bigint, exit: Exit<unknown, unknown>) { |
| 113 | + end(endTime: bigint, exit: Exit.Exit<unknown, unknown>) { |
113 | 114 | const hrTime = nanosToHrTime(endTime) |
114 | 115 | this.status = { |
115 | 116 | _tag: "Ended", |
@@ -237,15 +238,106 @@ export const makeExternalSpan = (options: { |
237 | 238 | } |
238 | 239 | } |
239 | 240 |
|
| 241 | +const makeOtelSpan = (span: EffectTracer.Span, clock: Clock.Clock): OtelApi.Span => { |
| 242 | + const spanContext: OtelApi.SpanContext = { |
| 243 | + traceId: span.traceId, |
| 244 | + spanId: span.spanId, |
| 245 | + traceFlags: span.sampled ? OtelApi.TraceFlags.SAMPLED : OtelApi.TraceFlags.NONE, |
| 246 | + isRemote: false |
| 247 | + } |
| 248 | + |
| 249 | + let exit = Exit.void |
| 250 | + |
| 251 | + const self: OtelApi.Span = { |
| 252 | + spanContext: () => spanContext, |
| 253 | + setAttribute(key, value) { |
| 254 | + span.attribute(key, value) |
| 255 | + return self |
| 256 | + }, |
| 257 | + setAttributes(attributes) { |
| 258 | + for (const [key, value] of Object.entries(attributes)) { |
| 259 | + span.attribute(key, value) |
| 260 | + } |
| 261 | + return self |
| 262 | + }, |
| 263 | + addEvent(name) { |
| 264 | + let attributes: OtelApi.Attributes | undefined = undefined |
| 265 | + let startTime: OtelApi.TimeInput | undefined = undefined |
| 266 | + if (arguments.length === 3) { |
| 267 | + attributes = arguments[1] |
| 268 | + startTime = arguments[2] |
| 269 | + } else { |
| 270 | + startTime = arguments[1] |
| 271 | + } |
| 272 | + span.event(name, convertOtelTimeInput(startTime, clock), attributes) |
| 273 | + return self |
| 274 | + }, |
| 275 | + addLink(link) { |
| 276 | + span.addLinks([{ |
| 277 | + _tag: "SpanLink", |
| 278 | + span: makeExternalSpan(link.context), |
| 279 | + attributes: link.attributes ?? {} |
| 280 | + }]) |
| 281 | + return self |
| 282 | + }, |
| 283 | + addLinks(links) { |
| 284 | + span.addLinks(links.map((link) => ({ |
| 285 | + _tag: "SpanLink", |
| 286 | + span: makeExternalSpan(link.context), |
| 287 | + attributes: link.attributes ?? {} |
| 288 | + }))) |
| 289 | + return self |
| 290 | + }, |
| 291 | + setStatus(status) { |
| 292 | + exit = OtelApi.SpanStatusCode.ERROR |
| 293 | + ? Exit.die(status.message ?? "Unknown error") |
| 294 | + : Exit.void |
| 295 | + return self |
| 296 | + }, |
| 297 | + updateName: () => self, |
| 298 | + end(endTime) { |
| 299 | + const time = convertOtelTimeInput(endTime, clock) |
| 300 | + span.end(time, exit) |
| 301 | + return self |
| 302 | + }, |
| 303 | + isRecording: constTrue, |
| 304 | + recordException(exception, timeInput) { |
| 305 | + const time = convertOtelTimeInput(timeInput, clock) |
| 306 | + const cause = Cause.fail(exception) |
| 307 | + const error = Cause.prettyErrors(cause)[0] |
| 308 | + span.event(error.message, time, { |
| 309 | + "exception.type": error.name, |
| 310 | + "exception.message": error.message, |
| 311 | + "exception.stacktrace": error.stack ?? "" |
| 312 | + }) |
| 313 | + } |
| 314 | + } |
| 315 | + return self |
| 316 | +} |
| 317 | + |
| 318 | +const bigint1e6 = BigInt(1_000_000) |
| 319 | +const bigint1e9 = BigInt(1_000_000_000) |
| 320 | + |
| 321 | +const convertOtelTimeInput = (input: OtelApi.TimeInput | undefined, clock: Clock.Clock): bigint => { |
| 322 | + if (input === undefined) { |
| 323 | + return clock.unsafeCurrentTimeNanos() |
| 324 | + } else if (typeof input === "number") { |
| 325 | + return BigInt(Math.round(input * 1_000_000)) |
| 326 | + } else if (input instanceof Date) { |
| 327 | + return BigInt(input.getTime()) * bigint1e6 |
| 328 | + } |
| 329 | + const [seconds, nanos] = input |
| 330 | + return BigInt(seconds) * bigint1e9 + BigInt(nanos) |
| 331 | +} |
| 332 | + |
240 | 333 | /** @internal */ |
241 | | -export const currentOtelSpan = Effect.flatMap( |
242 | | - Effect.currentSpan, |
243 | | - (span) => { |
| 334 | +export const currentOtelSpan: Effect.Effect<OtelApi.Span, Cause.NoSuchElementException> = Effect.clockWith((clock) => |
| 335 | + Effect.map(Effect.currentSpan, (span): OtelApi.Span => { |
244 | 336 | if (OtelSpanTypeId in span) { |
245 | | - return Effect.succeed((span as OtelSpan).span) |
| 337 | + return (span as OtelSpan).span |
246 | 338 | } |
247 | | - return Effect.fail(new Cause.NoSuchElementException()) |
248 | | - } |
| 339 | + return makeOtelSpan(span, clock) |
| 340 | + }) |
249 | 341 | ) |
250 | 342 |
|
251 | 343 | /** @internal */ |
|
0 commit comments