|
| 1 | +/** |
| 2 | + * Effect-shape integration for `@arcp/middleware-otel`. |
| 3 | + * |
| 4 | + * Slice #49 of the Effect migration. This module sits alongside the legacy |
| 5 | + * Transport-level {@link withTracing} wrapper (preserved unchanged in |
| 6 | + * `./index.ts`) and exposes a small, compositional bridge from |
| 7 | + * `@effect/opentelemetry` into an ARCP runtime built with |
| 8 | + * {@link makeARCPServerRuntime} / {@link ARCPRuntimeLayer}. |
| 9 | + * |
| 10 | + * Scope of this module |
| 11 | + * -------------------- |
| 12 | + * Two distinct concerns are at play: |
| 13 | + * |
| 14 | + * 1. **Wire-level propagation.** Stamping the W3C `traceparent` carrier |
| 15 | + * into `envelope.extensions["x-vendor.opentelemetry.tracecontext"]` so |
| 16 | + * that ARCP §11 spans link across hops. That contract is owned by |
| 17 | + * {@link withTracing} (Transport wrapper) and is unchanged. |
| 18 | + * |
| 19 | + * 2. **Effect-workflow tracing.** Letting spans opened by `Effect.withSpan` |
| 20 | + * and `Effect.useSpan` inside an ARCP runtime appear in the same OTel |
| 21 | + * trace as the spans emitted by (1). That is what this module adds. |
| 22 | + * |
| 23 | + * Why we don't expose a `NodeSdk.layer` here |
| 24 | + * ------------------------------------------ |
| 25 | + * `@effect/opentelemetry`'s `NodeSdk.layer` constructs a full Node tracer |
| 26 | + * provider from `@opentelemetry/sdk-trace-node`, `@opentelemetry/resources`, |
| 27 | + * and a user-supplied `SpanProcessor`. That decision (which exporter, which |
| 28 | + * batching strategy, which propagator, etc.) belongs to the consumer's |
| 29 | + * observability stack, not to a middleware package. Forcing a particular |
| 30 | + * SDK shape would also force a hard dependency on `@opentelemetry/sdk-node` |
| 31 | + * — which the issue explicitly forbids ("No new dependency on |
| 32 | + * `@opentelemetry/sdk-node`; consumer brings their own SDK; we only bridge"). |
| 33 | + * |
| 34 | + * Instead we expose {@link OtelTracerLayer}, a thin bridge that takes an |
| 35 | + * already-constructed OTel `TracerProvider` (whatever the consumer wired up |
| 36 | + * — `NodeTracerProvider`, `WebTracerProvider`, `@vercel/otel`, a no-op |
| 37 | + * provider for tests, or `trace.getTracerProvider()` after global registration) |
| 38 | + * and lifts it into an Effect {@link Layer} that satisfies the |
| 39 | + * `OtelTracer.OtelTracer` and `OtelTracer.OtelTracerProvider` requirements |
| 40 | + * for any downstream effect that calls `Effect.withSpan(...)`. |
| 41 | + * |
| 42 | + * Usage |
| 43 | + * ----- |
| 44 | + * ```ts |
| 45 | + * import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"; |
| 46 | + * import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; |
| 47 | + * import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; |
| 48 | + * import { Layer } from "effect"; |
| 49 | + * import { ARCPRuntimeLayer } from "@arcp/runtime"; |
| 50 | + * import { OtelTracerLayer } from "@arcp/middleware-otel"; |
| 51 | + * |
| 52 | + * const provider = new NodeTracerProvider(); |
| 53 | + * provider.addSpanProcessor(new BatchSpanProcessor(new OTLPTraceExporter())); |
| 54 | + * provider.register(); // also makes withTracing happy for §11 propagation |
| 55 | + * |
| 56 | + * const MainLayer = Layer.provideMerge( |
| 57 | + * ARCPRuntimeLayer(serverOptions), |
| 58 | + * OtelTracerLayer({ |
| 59 | + * tracerProvider: provider, |
| 60 | + * resource: { serviceName: "arcp-runtime" }, |
| 61 | + * }), |
| 62 | + * ); |
| 63 | + * ``` |
| 64 | + * |
| 65 | + * The legacy {@link withTracing} wrapper continues to work side-by-side: it |
| 66 | + * pulls the active OTel context from `@opentelemetry/api` (which the global |
| 67 | + * provider above satisfies), so a single registered provider feeds both the |
| 68 | + * Transport-level `arcp.send` / `arcp.recv` spans and the Effect-workflow |
| 69 | + * spans opened inside the runtime. |
| 70 | + */ |
| 71 | +import * as OtelResource from "@effect/opentelemetry/Resource"; |
| 72 | +import * as OtelTracer from "@effect/opentelemetry/Tracer"; |
| 73 | +import type * as OtelApi from "@opentelemetry/api"; |
| 74 | +import { Layer } from "effect"; |
| 75 | + |
| 76 | +/** |
| 77 | + * Configuration for {@link OtelTracerLayer}. |
| 78 | + * |
| 79 | + * Both fields are required: the bridge needs a real OTel `TracerProvider` |
| 80 | + * to delegate span creation to, and a `resource` (service name / version / |
| 81 | + * attributes) so that `provider.getTracer(serviceName, serviceVersion)` |
| 82 | + * resolves to a stable tracer per the OTel spec. |
| 83 | + */ |
| 84 | +export interface OtelTracerLayerOptions { |
| 85 | + /** |
| 86 | + * The OpenTelemetry `TracerProvider` to bridge into Effect. Typically the |
| 87 | + * same provider passed to `provider.register()` so that the legacy |
| 88 | + * {@link withTracing} Transport wrapper (which reads from the global API) |
| 89 | + * and Effect-workflow spans share a single export pipeline. |
| 90 | + */ |
| 91 | + readonly tracerProvider: OtelApi.TracerProvider; |
| 92 | + |
| 93 | + /** |
| 94 | + * Resource attributes used to name the bridged tracer. `serviceName` is |
| 95 | + * required; `serviceVersion` and `attributes` are optional and forwarded |
| 96 | + * to `@effect/opentelemetry`'s `Resource.layer` unchanged. |
| 97 | + */ |
| 98 | + readonly resource: { |
| 99 | + readonly serviceName: string; |
| 100 | + readonly serviceVersion?: string; |
| 101 | + readonly attributes?: OtelApi.Attributes; |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Build an Effect {@link Layer} that bridges Effect's tracer to a |
| 107 | + * consumer-supplied OTel `TracerProvider`. |
| 108 | + * |
| 109 | + * The resulting layer eliminates the `OtelTracer.OtelTracer`, |
| 110 | + * `OtelTracer.OtelTracerProvider`, and `OtelResource.Resource` |
| 111 | + * requirements for downstream effects, and replaces the default |
| 112 | + * Effect tracer with one that emits OTel spans through the supplied |
| 113 | + * provider. Compose it via `Layer.provideMerge` into an |
| 114 | + * `ARCPRuntimeLayer`-built program; no other plumbing is required. |
| 115 | + * |
| 116 | + * This is purely an Effect-shape addition. The Transport-level |
| 117 | + * `withTracing` wrapper exported from `./index.ts` is unchanged and |
| 118 | + * continues to be the right tool for §11 wire propagation. |
| 119 | + */ |
| 120 | +export const OtelTracerLayer = ( |
| 121 | + options: OtelTracerLayerOptions, |
| 122 | +): Layer.Layer<never> => { |
| 123 | + const providerLayer = Layer.succeed( |
| 124 | + OtelTracer.OtelTracerProvider, |
| 125 | + options.tracerProvider, |
| 126 | + ); |
| 127 | + const resourceLayer = OtelResource.layer(options.resource); |
| 128 | + const tracerLayer = OtelTracer.layer; |
| 129 | + return Layer.provide( |
| 130 | + tracerLayer, |
| 131 | + Layer.merge(providerLayer, resourceLayer), |
| 132 | + ); |
| 133 | +}; |
0 commit comments