|
| 1 | +import { metrics, trace } from "@opentelemetry/api"; |
| 2 | +import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http"; |
| 3 | +import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; |
| 4 | +import { Resource } from "@opentelemetry/resources"; |
| 5 | +import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; |
| 6 | +import { NodeSDK } from "@opentelemetry/sdk-node"; |
| 7 | +import { ParentBasedSampler, TraceIdRatioBasedSampler } from "@opentelemetry/sdk-trace-base"; |
| 8 | +import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions"; |
| 9 | + |
| 10 | +import type { |
| 11 | + ClawdbotPluginService, |
| 12 | + DiagnosticUsageEvent, |
| 13 | +} from "clawdbot/plugin-sdk"; |
| 14 | +import { onDiagnosticEvent } from "clawdbot/plugin-sdk"; |
| 15 | + |
| 16 | +const DEFAULT_SERVICE_NAME = "clawdbot"; |
| 17 | + |
| 18 | +function normalizeEndpoint(endpoint?: string): string | undefined { |
| 19 | + const trimmed = endpoint?.trim(); |
| 20 | + return trimmed ? trimmed.replace(/\/+$/, "") : undefined; |
| 21 | +} |
| 22 | + |
| 23 | +function resolveOtelUrl(endpoint: string | undefined, path: string): string | undefined { |
| 24 | + if (!endpoint) return undefined; |
| 25 | + if (endpoint.includes("/v1/")) return endpoint; |
| 26 | + return `${endpoint}/${path}`; |
| 27 | +} |
| 28 | + |
| 29 | +function resolveSampleRate(value: number | undefined): number | undefined { |
| 30 | + if (typeof value !== "number" || !Number.isFinite(value)) return undefined; |
| 31 | + if (value < 0 || value > 1) return undefined; |
| 32 | + return value; |
| 33 | +} |
| 34 | + |
| 35 | +export function createDiagnosticsOtelService(): ClawdbotPluginService { |
| 36 | + let sdk: NodeSDK | null = null; |
| 37 | + let unsubscribe: (() => void) | null = null; |
| 38 | + |
| 39 | + return { |
| 40 | + id: "diagnostics-otel", |
| 41 | + async start(ctx) { |
| 42 | + const cfg = ctx.config.diagnostics; |
| 43 | + const otel = cfg?.otel; |
| 44 | + if (!cfg?.enabled || !otel?.enabled) return; |
| 45 | + |
| 46 | + const protocol = otel.protocol ?? process.env.OTEL_EXPORTER_OTLP_PROTOCOL ?? "http/protobuf"; |
| 47 | + if (protocol !== "http/protobuf") { |
| 48 | + ctx.logger.warn(`diagnostics-otel: unsupported protocol ${protocol}`); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const endpoint = normalizeEndpoint(otel.endpoint ?? process.env.OTEL_EXPORTER_OTLP_ENDPOINT); |
| 53 | + const headers = otel.headers ?? undefined; |
| 54 | + const serviceName = |
| 55 | + otel.serviceName?.trim() || process.env.OTEL_SERVICE_NAME || DEFAULT_SERVICE_NAME; |
| 56 | + const sampleRate = resolveSampleRate(otel.sampleRate); |
| 57 | + |
| 58 | + const tracesEnabled = otel.traces !== false; |
| 59 | + const metricsEnabled = otel.metrics !== false; |
| 60 | + if (!tracesEnabled && !metricsEnabled) return; |
| 61 | + |
| 62 | + const resource = new Resource({ |
| 63 | + [SemanticResourceAttributes.SERVICE_NAME]: serviceName, |
| 64 | + }); |
| 65 | + |
| 66 | + const traceUrl = resolveOtelUrl(endpoint, "v1/traces"); |
| 67 | + const metricUrl = resolveOtelUrl(endpoint, "v1/metrics"); |
| 68 | + const traceExporter = tracesEnabled |
| 69 | + ? new OTLPTraceExporter({ |
| 70 | + ...(traceUrl ? { url: traceUrl } : {}), |
| 71 | + ...(headers ? { headers } : {}), |
| 72 | + }) |
| 73 | + : undefined; |
| 74 | + |
| 75 | + const metricExporter = metricsEnabled |
| 76 | + ? new OTLPMetricExporter({ |
| 77 | + ...(metricUrl ? { url: metricUrl } : {}), |
| 78 | + ...(headers ? { headers } : {}), |
| 79 | + }) |
| 80 | + : undefined; |
| 81 | + |
| 82 | + const metricReader = metricExporter |
| 83 | + ? new PeriodicExportingMetricReader({ |
| 84 | + exporter: metricExporter, |
| 85 | + ...(typeof otel.flushIntervalMs === "number" |
| 86 | + ? { exportIntervalMillis: Math.max(1000, otel.flushIntervalMs) } |
| 87 | + : {}), |
| 88 | + }) |
| 89 | + : undefined; |
| 90 | + |
| 91 | + sdk = new NodeSDK({ |
| 92 | + resource, |
| 93 | + ...(traceExporter ? { traceExporter } : {}), |
| 94 | + ...(metricReader ? { metricReader } : {}), |
| 95 | + ...(sampleRate !== undefined |
| 96 | + ? { |
| 97 | + sampler: new ParentBasedSampler({ |
| 98 | + root: new TraceIdRatioBasedSampler(sampleRate), |
| 99 | + }), |
| 100 | + } |
| 101 | + : {}), |
| 102 | + }); |
| 103 | + |
| 104 | + await sdk.start(); |
| 105 | + |
| 106 | + const meter = metrics.getMeter("clawdbot"); |
| 107 | + const tracer = trace.getTracer("clawdbot"); |
| 108 | + |
| 109 | + const tokensCounter = meter.createCounter("clawdbot.tokens", { |
| 110 | + unit: "1", |
| 111 | + description: "Token usage by type", |
| 112 | + }); |
| 113 | + const costCounter = meter.createCounter("clawdbot.cost.usd", { |
| 114 | + unit: "1", |
| 115 | + description: "Estimated model cost (USD)", |
| 116 | + }); |
| 117 | + const durationHistogram = meter.createHistogram("clawdbot.run.duration_ms", { |
| 118 | + unit: "ms", |
| 119 | + description: "Agent run duration", |
| 120 | + }); |
| 121 | + const contextHistogram = meter.createHistogram("clawdbot.context.tokens", { |
| 122 | + unit: "1", |
| 123 | + description: "Context window size and usage", |
| 124 | + }); |
| 125 | + |
| 126 | + unsubscribe = onDiagnosticEvent((evt) => { |
| 127 | + if (evt.type !== "model.usage") return; |
| 128 | + const usageEvent = evt as DiagnosticUsageEvent; |
| 129 | + const attrs = { |
| 130 | + "clawdbot.channel": usageEvent.channel ?? "unknown", |
| 131 | + "clawdbot.provider": usageEvent.provider ?? "unknown", |
| 132 | + "clawdbot.model": usageEvent.model ?? "unknown", |
| 133 | + }; |
| 134 | + |
| 135 | + const usage = usageEvent.usage; |
| 136 | + if (usage.input) tokensCounter.add(usage.input, { ...attrs, "clawdbot.token": "input" }); |
| 137 | + if (usage.output) |
| 138 | + tokensCounter.add(usage.output, { ...attrs, "clawdbot.token": "output" }); |
| 139 | + if (usage.cacheRead) |
| 140 | + tokensCounter.add(usage.cacheRead, { ...attrs, "clawdbot.token": "cache_read" }); |
| 141 | + if (usage.cacheWrite) |
| 142 | + tokensCounter.add(usage.cacheWrite, { ...attrs, "clawdbot.token": "cache_write" }); |
| 143 | + if (usage.promptTokens) |
| 144 | + tokensCounter.add(usage.promptTokens, { ...attrs, "clawdbot.token": "prompt" }); |
| 145 | + if (usage.total) |
| 146 | + tokensCounter.add(usage.total, { ...attrs, "clawdbot.token": "total" }); |
| 147 | + |
| 148 | + if (usageEvent.costUsd) costCounter.add(usageEvent.costUsd, attrs); |
| 149 | + if (usageEvent.durationMs) durationHistogram.record(usageEvent.durationMs, attrs); |
| 150 | + if (usageEvent.context?.limit) |
| 151 | + contextHistogram.record(usageEvent.context.limit, { |
| 152 | + ...attrs, |
| 153 | + "clawdbot.context": "limit", |
| 154 | + }); |
| 155 | + if (usageEvent.context?.used) |
| 156 | + contextHistogram.record(usageEvent.context.used, { |
| 157 | + ...attrs, |
| 158 | + "clawdbot.context": "used", |
| 159 | + }); |
| 160 | + |
| 161 | + if (!tracesEnabled) return; |
| 162 | + const spanAttrs: Record<string, string | number> = { |
| 163 | + ...attrs, |
| 164 | + "clawdbot.sessionKey": usageEvent.sessionKey ?? "", |
| 165 | + "clawdbot.sessionId": usageEvent.sessionId ?? "", |
| 166 | + "clawdbot.tokens.input": usage.input ?? 0, |
| 167 | + "clawdbot.tokens.output": usage.output ?? 0, |
| 168 | + "clawdbot.tokens.cache_read": usage.cacheRead ?? 0, |
| 169 | + "clawdbot.tokens.cache_write": usage.cacheWrite ?? 0, |
| 170 | + "clawdbot.tokens.total": usage.total ?? 0, |
| 171 | + }; |
| 172 | + |
| 173 | + const startTime = usageEvent.durationMs |
| 174 | + ? Date.now() - Math.max(0, usageEvent.durationMs) |
| 175 | + : undefined; |
| 176 | + const span = tracer.startSpan("clawdbot.model.usage", { |
| 177 | + attributes: spanAttrs, |
| 178 | + ...(startTime ? { startTime } : {}), |
| 179 | + }); |
| 180 | + span.end(); |
| 181 | + }); |
| 182 | + |
| 183 | + if (otel.logs) { |
| 184 | + ctx.logger.warn("diagnostics-otel: logs exporter not wired yet"); |
| 185 | + } |
| 186 | + }, |
| 187 | + async stop() { |
| 188 | + unsubscribe?.(); |
| 189 | + unsubscribe = null; |
| 190 | + if (sdk) { |
| 191 | + await sdk.shutdown().catch(() => undefined); |
| 192 | + sdk = null; |
| 193 | + } |
| 194 | + }, |
| 195 | + } satisfies ClawdbotPluginService; |
| 196 | +} |
0 commit comments