* Persist a cognition span. Never throws — failures are logged and counted. * The span_id is generated here so callers don't have to reach for randomUUID.
(init)
| 144 | * The span_id is generated here so callers don't have to reach for randomUUID. |
| 145 | */ |
| 146 | async function writeSpan(init) { |
| 147 | const now = Date.now(); |
| 148 | const span = { |
| 149 | span_id: (0, crypto_1.randomUUID)(), |
| 150 | trace_id: init.trace_id, |
| 151 | parent_span_id: init.parent_span_id ?? null, |
| 152 | workflow: init.workflow, |
| 153 | step: init.step, |
| 154 | kind: init.kind, |
| 155 | prompt: init.prompt ?? null, |
| 156 | model: init.model ?? null, |
| 157 | provider: init.provider ?? null, |
| 158 | input_redacted: init.input === undefined ? null : redactValue(init.input, init.input_entity_hint), |
| 159 | output_redacted: init.output === undefined ? null : redactValue(init.output, init.output_entity_hint), |
| 160 | validate_result: init.validate_result ?? null, |
| 161 | confidence: init.confidence ?? null, |
| 162 | prompt_tokens: init.prompt_tokens ?? null, |
| 163 | completion_tokens: init.completion_tokens ?? null, |
| 164 | cost_usd: init.cost_usd ?? null, |
| 165 | latency_ms: init.latency_ms, |
| 166 | cache_hit: init.cache_hit ?? false, |
| 167 | status: init.status, |
| 168 | error_code: init.error_code ?? null, |
| 169 | metadata: init.metadata ?? {}, |
| 170 | started_at: now - init.latency_ms, |
| 171 | finished_at: now, |
| 172 | }; |
| 173 | try { |
| 174 | await __backend.write(span); |
| 175 | (0, metrics_1.counter)("cognition.span_written", { kind: init.kind, status: init.status }); |
| 176 | } |
| 177 | catch (e) { |
| 178 | (0, metrics_1.counter)("cognition.span_write_failed"); |
| 179 | const msg = e instanceof Error ? e.message : String(e); |
| 180 | logger_1.logger.warn("cognition_span_write_failed", { event: "traces", status: "rejected", trace_id: span.trace_id, metadata: { error: msg.slice(0, 256) } }); |
| 181 | } |
| 182 | return span.span_id; |
| 183 | } |
| 184 | /** Load all spans for a trace, sorted by start time. */ |
| 185 | async function loadTrace(trace_id) { |
| 186 | return __backend.loadTrace(trace_id); |
no test coverage detected