|
| 1 | +/* eslint-disable @typescript-eslint/no-unnecessary-condition */ |
| 2 | +/* eslint-disable @typescript-eslint/no-deprecated */ |
| 3 | +import { Context } from '@opentelemetry/api' |
| 4 | +import { ExportResult, ExportResultCode, hrTimeToMicroseconds } from '@opentelemetry/core' |
| 5 | +import { ReadableSpan, SimpleSpanProcessor, Span, SpanExporter, SpanProcessor } from '@opentelemetry/sdk-trace-web' |
| 6 | +import { ATTR_HTTP_URL } from '@opentelemetry/semantic-conventions/incubating' |
| 7 | + |
| 8 | +// not present in the semantic conventions |
| 9 | +const ATTR_TARGET_XPATH = 'target_xpath' |
| 10 | +const ATTR_EVENT_TYPE = 'event_type' |
| 11 | + |
| 12 | +export const LevelLabels = { |
| 13 | + 1: 'trace', |
| 14 | + 5: 'debug', |
| 15 | + 9: 'info', |
| 16 | + 10: 'notice', |
| 17 | + 13: 'warning', |
| 18 | + 17: 'error', |
| 19 | + 21: 'fatal', |
| 20 | +} as const |
| 21 | + |
| 22 | +const Colors = { |
| 23 | + debug: '#E3E3E3', |
| 24 | + error: '#EA4335', |
| 25 | + fatal: '#EA4335', |
| 26 | + info: '#9EC1FB', |
| 27 | + notice: '#A5D490', |
| 28 | + 'on-debug': '#636262', |
| 29 | + 'on-error': '#FFEDE9', |
| 30 | + 'on-fatal': '#FFEDE9', |
| 31 | + 'on-info': '#063175', |
| 32 | + 'on-notice': '#222222', |
| 33 | + 'on-trace': '#636262', |
| 34 | + 'on-warning': '#613A0D', |
| 35 | + trace: '#E3E3E3', |
| 36 | + warning: '#EFB77A', |
| 37 | +} as const |
| 38 | + |
| 39 | +class LogfireConsoleSpanExporter implements SpanExporter { |
| 40 | + export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void { |
| 41 | + this.sendSpans(spans, resultCallback) |
| 42 | + } |
| 43 | + |
| 44 | + forceFlush(): Promise<void> { |
| 45 | + return Promise.resolve() |
| 46 | + } |
| 47 | + shutdown(): Promise<void> { |
| 48 | + this.sendSpans([]) |
| 49 | + return this.forceFlush() |
| 50 | + } |
| 51 | + /** |
| 52 | + * converts span info into more readable format |
| 53 | + * @param span |
| 54 | + */ |
| 55 | + private exportInfo(span: ReadableSpan) { |
| 56 | + return { |
| 57 | + attributes: span.attributes, |
| 58 | + duration: hrTimeToMicroseconds(span.duration), |
| 59 | + events: span.events, |
| 60 | + id: span.spanContext().spanId, |
| 61 | + instrumentationScope: span.instrumentationScope, |
| 62 | + kind: span.kind, |
| 63 | + links: span.links, |
| 64 | + name: span.name, |
| 65 | + parentSpanContext: span.parentSpanContext, |
| 66 | + resource: { |
| 67 | + attributes: span.resource.attributes, |
| 68 | + }, |
| 69 | + status: span.status, |
| 70 | + timestamp: hrTimeToMicroseconds(span.startTime), |
| 71 | + traceId: span.spanContext().traceId, |
| 72 | + traceState: span.spanContext().traceState?.serialize(), |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private sendSpans(spans: ReadableSpan[], done?: (result: ExportResult) => void): void { |
| 77 | + for (const span of spans) { |
| 78 | + const type = LevelLabels[span.attributes['logfire.level_num'] as keyof typeof LevelLabels] ?? 'info' |
| 79 | + |
| 80 | + const { attributes, name, ...rest } = this.exportInfo(span) |
| 81 | + console.log( |
| 82 | + `%cLogfire %c${type}`, |
| 83 | + 'background-color: #E520E9; color: #FFFFFF', |
| 84 | + `background-color: ${Colors[`on-${type}`]}; color: ${Colors[type]}`, |
| 85 | + name, |
| 86 | + attributes, |
| 87 | + rest |
| 88 | + ) |
| 89 | + } |
| 90 | + if (done) { |
| 91 | + done({ code: ExportResultCode.SUCCESS }) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export class LogfireSpanProcessor implements SpanProcessor { |
| 97 | + private console?: SpanProcessor |
| 98 | + private wrapped: SpanProcessor |
| 99 | + |
| 100 | + constructor(wrapped: SpanProcessor, enableConsole: boolean) { |
| 101 | + if (enableConsole) { |
| 102 | + this.console = new SimpleSpanProcessor(new LogfireConsoleSpanExporter()) |
| 103 | + } |
| 104 | + this.wrapped = wrapped |
| 105 | + } |
| 106 | + |
| 107 | + async forceFlush(): Promise<void> { |
| 108 | + await this.console?.forceFlush() |
| 109 | + return this.wrapped.forceFlush() |
| 110 | + } |
| 111 | + |
| 112 | + onEnd(span: ReadableSpan): void { |
| 113 | + this.console?.onEnd(span) |
| 114 | + // Note: this is too late for the regular node instrumentation. The opentelemetry API rejects the non-primitive attribute values. |
| 115 | + // Instead, the serialization happens at the `logfire.span, logfire.startSpan`, etc. |
| 116 | + // Object.assign(span.attributes, serializeAttributes(span.attributes)) |
| 117 | + this.wrapped.onEnd(span) |
| 118 | + } |
| 119 | + |
| 120 | + onStart(span: Span, parentContext: Context): void { |
| 121 | + // make the fetch spans more descriptive |
| 122 | + if (ATTR_HTTP_URL in span.attributes) { |
| 123 | + const url = new URL(span.attributes[ATTR_HTTP_URL] as string) |
| 124 | + Reflect.set(span, 'name', `${span.name} ${url.pathname}`) |
| 125 | + } |
| 126 | + |
| 127 | + // same for the interaction spans |
| 128 | + if (ATTR_TARGET_XPATH in span.attributes) { |
| 129 | + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 130 | + Reflect.set(span, 'name', `${span.attributes[ATTR_EVENT_TYPE] ?? 'unknown'} ${span.attributes[ATTR_TARGET_XPATH] ?? ''}`) |
| 131 | + } |
| 132 | + this.console?.onStart(span, parentContext) |
| 133 | + this.wrapped.onStart(span, parentContext) |
| 134 | + } |
| 135 | + |
| 136 | + async shutdown(): Promise<void> { |
| 137 | + await this.console?.shutdown() |
| 138 | + return this.wrapped.shutdown() |
| 139 | + } |
| 140 | +} |
0 commit comments