|
| 1 | +import { v4 as uuidv4 } from 'uuid' |
| 2 | +import { PostHog } from 'posthog-node' |
| 3 | +import { sendEventToPosthog, sendEventWithErrorToPosthog } from '../utils' |
| 4 | +import { defaultSpanMappers } from './mappers' |
| 5 | +import type { ReadableSpan } from '@opentelemetry/sdk-trace-base' |
| 6 | +import type { PostHogTelemetryOptions, PostHogSpanMapper, UsageData } from './types' |
| 7 | + |
| 8 | +function pickMapper(span: ReadableSpan, mappers: PostHogSpanMapper[]): PostHogSpanMapper | undefined { |
| 9 | + return mappers.find((mapper) => { |
| 10 | + try { |
| 11 | + return mapper.canMap(span) |
| 12 | + } catch { |
| 13 | + return false |
| 14 | + } |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +function getTraceId(span: ReadableSpan, options: PostHogTelemetryOptions, mapperTraceId?: string): string { |
| 19 | + if (mapperTraceId) { |
| 20 | + return mapperTraceId |
| 21 | + } |
| 22 | + if (options.posthogTraceId) { |
| 23 | + return options.posthogTraceId |
| 24 | + } |
| 25 | + const spanTraceId = span.spanContext?.().traceId |
| 26 | + return spanTraceId || uuidv4() |
| 27 | +} |
| 28 | + |
| 29 | +function buildPosthogParams( |
| 30 | + options: PostHogTelemetryOptions, |
| 31 | + traceId: string, |
| 32 | + distinctId: string | undefined, |
| 33 | + modelParams: Record<string, unknown>, |
| 34 | + posthogProperties: Record<string, unknown> |
| 35 | +): Record<string, unknown> { |
| 36 | + return { |
| 37 | + ...modelParams, |
| 38 | + posthogDistinctId: distinctId, |
| 39 | + posthogTraceId: traceId, |
| 40 | + posthogProperties, |
| 41 | + posthogPrivacyMode: options.posthogPrivacyMode, |
| 42 | + posthogGroups: options.posthogGroups, |
| 43 | + posthogModelOverride: options.posthogModelOverride, |
| 44 | + posthogProviderOverride: options.posthogProviderOverride, |
| 45 | + posthogCostOverride: options.posthogCostOverride, |
| 46 | + posthogCaptureImmediate: options.posthogCaptureImmediate, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export async function captureSpan( |
| 51 | + span: ReadableSpan, |
| 52 | + phClient: PostHog, |
| 53 | + options: PostHogTelemetryOptions = {} |
| 54 | +): Promise<void> { |
| 55 | + if (options.shouldExportSpan && options.shouldExportSpan({ otelSpan: span }) === false) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + const mappers = options.mappers ?? defaultSpanMappers |
| 60 | + const mapper = pickMapper(span, mappers) |
| 61 | + if (!mapper) { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + const mapped = mapper.map(span, { options }) |
| 66 | + if (!mapped) { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + const traceId = getTraceId(span, options, mapped.traceId) |
| 71 | + const distinctId = mapped.distinctId ?? options.posthogDistinctId |
| 72 | + const posthogProperties = { |
| 73 | + ...options.posthogProperties, |
| 74 | + ...mapped.posthogProperties, |
| 75 | + } |
| 76 | + |
| 77 | + const params = buildPosthogParams(options, traceId, distinctId, mapped.modelParams ?? {}, posthogProperties) |
| 78 | + const baseURL = mapped.baseURL ?? '' |
| 79 | + const usage: UsageData = mapped.usage ?? {} |
| 80 | + |
| 81 | + if (mapped.error !== undefined) { |
| 82 | + await sendEventWithErrorToPosthog({ |
| 83 | + eventType: mapped.eventType, |
| 84 | + client: phClient, |
| 85 | + distinctId, |
| 86 | + traceId, |
| 87 | + model: mapped.model, |
| 88 | + provider: mapped.provider, |
| 89 | + input: mapped.input, |
| 90 | + output: mapped.output, |
| 91 | + latency: mapped.latency, |
| 92 | + baseURL, |
| 93 | + params: params as any, |
| 94 | + usage, |
| 95 | + tools: mapped.tools, |
| 96 | + error: mapped.error, |
| 97 | + captureImmediate: options.posthogCaptureImmediate, |
| 98 | + }) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + await sendEventToPosthog({ |
| 103 | + eventType: mapped.eventType, |
| 104 | + client: phClient, |
| 105 | + distinctId, |
| 106 | + traceId, |
| 107 | + model: mapped.model, |
| 108 | + provider: mapped.provider, |
| 109 | + input: mapped.input, |
| 110 | + output: mapped.output, |
| 111 | + latency: mapped.latency, |
| 112 | + timeToFirstToken: mapped.timeToFirstToken, |
| 113 | + baseURL, |
| 114 | + params: params as any, |
| 115 | + httpStatus: mapped.httpStatus ?? 200, |
| 116 | + usage, |
| 117 | + tools: mapped.tools, |
| 118 | + captureImmediate: options.posthogCaptureImmediate, |
| 119 | + }) |
| 120 | +} |
0 commit comments