|
| 1 | +// Inline to avoid peer dependency issues |
| 2 | +// https://github.com/Effect-Deprecated/otel/blob/ee7aa57e42e1bcdf9af98815bc601279f54e26ec/packages/otel-exporter-trace-otlp-grpc/src/index.ts |
| 3 | +import * as T from '@effect-ts/core/Effect' |
| 4 | +import * as L from '@effect-ts/core/Effect/Layer' |
| 5 | +import * as M from '@effect-ts/core/Effect/Managed' |
| 6 | +import { pipe } from '@effect-ts/core/Function' |
| 7 | +import { tag } from '@effect-ts/core/Has' |
| 8 | +import { SimpleProcessor } from '@effect-ts/otel' |
| 9 | +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc' |
| 10 | +import type { OTLPGRPCExporterConfigNode } from '@opentelemetry/otlp-grpc-exporter-base' |
| 11 | + |
| 12 | +export const OTLPTraceExporterConfigSymbol = Symbol() |
| 13 | + |
| 14 | +export class OTLPTraceExporterConfig { |
| 15 | + readonly [OTLPTraceExporterConfigSymbol] = OTLPTraceExporterConfigSymbol |
| 16 | + constructor(readonly config: OTLPGRPCExporterConfigNode) {} |
| 17 | +} |
| 18 | + |
| 19 | +export const OTLPTraceExporterConfigTag = tag<OTLPTraceExporterConfig>(OTLPTraceExporterConfigSymbol) |
| 20 | + |
| 21 | +export const makeOTLPTraceExporterConfigLayer = (config: OTLPGRPCExporterConfigNode) => |
| 22 | + L.fromEffect(OTLPTraceExporterConfigTag)(T.succeedWith(() => new OTLPTraceExporterConfig(config))).setKey( |
| 23 | + OTLPTraceExporterConfigTag.key, |
| 24 | + ) |
| 25 | + |
| 26 | +export const makeOTLPTraceExporterConfigLayerM = <R, E>(config: T.Effect<R, E, OTLPGRPCExporterConfigNode>) => |
| 27 | + L.fromEffect(OTLPTraceExporterConfigTag)(T.map_(config, (_) => new OTLPTraceExporterConfig(_))).setKey( |
| 28 | + OTLPTraceExporterConfigTag.key, |
| 29 | + ) |
| 30 | + |
| 31 | +export const makeTracingSpanExporter = M.gen(function* (_) { |
| 32 | + const { config } = yield* _(OTLPTraceExporterConfigTag) |
| 33 | + |
| 34 | + const spanExporter = yield* _( |
| 35 | + pipe( |
| 36 | + T.succeedWith(() => new OTLPTraceExporter(config)), |
| 37 | + // NOTE Unfortunately this workaround/"hack" is currently needed since Otel doesn't yet provide a graceful |
| 38 | + // way to shutdown. |
| 39 | + // |
| 40 | + // Related issue: https://github.com/open-telemetry/opentelemetry-js/issues/987 |
| 41 | + M.make((p) => |
| 42 | + T.gen(function* (_) { |
| 43 | + while (1) { |
| 44 | + yield* _(T.sleep(0)) |
| 45 | + const promises = p['_sendingPromises'] as any[] |
| 46 | + if (promises.length > 0) { |
| 47 | + yield* _(T.result(T.promise(() => Promise.all(promises)))) |
| 48 | + } else { |
| 49 | + break |
| 50 | + } |
| 51 | + } |
| 52 | + }), |
| 53 | + ), |
| 54 | + ), |
| 55 | + ) |
| 56 | + |
| 57 | + return spanExporter |
| 58 | +}) |
| 59 | + |
| 60 | +export const LiveSimpleProcessor = SimpleProcessor(makeTracingSpanExporter) |
0 commit comments