(init: SpanInit)
| 232 | * The span_id is generated here so callers don't have to reach for randomUUID. |
| 233 | */ |
| 234 | export async function writeSpan(init: SpanInit): Promise<string> { |
| 235 | const now = Date.now(); |
| 236 | const span: CognitionSpan = { |
| 237 | span_id: randomUUID(), |
| 238 | trace_id: init.trace_id, |
| 239 | parent_span_id: init.parent_span_id ?? null, |
| 240 | workflow: init.workflow, |
| 241 | step: init.step, |
| 242 | kind: init.kind, |
| 243 | prompt: init.prompt ?? null, |
| 244 | model: init.model ?? null, |
| 245 | provider: init.provider ?? null, |
| 246 | input_redacted: init.input === undefined ? null : redactValue(init.input, init.input_entity_hint), |
| 247 | output_redacted: init.output === undefined ? null : redactValue(init.output, init.output_entity_hint), |
| 248 | validate_result: init.validate_result ?? null, |
| 249 | confidence: init.confidence ?? null, |
| 250 | prompt_tokens: init.prompt_tokens ?? null, |
| 251 | completion_tokens: init.completion_tokens ?? null, |
| 252 | cost_usd: init.cost_usd ?? null, |
| 253 | latency_ms: init.latency_ms, |
| 254 | cache_hit: init.cache_hit ?? false, |
| 255 | status: init.status, |
| 256 | error_code: init.error_code ?? null, |
| 257 | metadata: init.metadata ?? {}, |
| 258 | started_at: now - init.latency_ms, |
| 259 | finished_at: now, |
| 260 | }; |
| 261 | try { |
| 262 | await __backend.write(span); |
| 263 | counter("cognition.span_written", { kind: init.kind, status: init.status }); |
| 264 | } catch (e) { |
| 265 | counter("cognition.span_write_failed"); |
| 266 | const msg = e instanceof Error ? e.message : String(e); |
| 267 | logger.warn("cognition_span_write_failed", { event: "traces", status: "rejected", trace_id: span.trace_id, metadata: { error: msg.slice(0, 256) } }); |
| 268 | } |
| 269 | return span.span_id; |
| 270 | } |
| 271 | |
| 272 | /** Load all spans for a trace, sorted by start time. */ |
| 273 | export async function loadTrace(trace_id: string): Promise<CognitionSpan[]> { |
nothing calls this directly
no test coverage detected