(traceSpan: TraceSpan, parentSpan?: Span)
| 222 | * Creates a proper OTel span with all the metadata from the trace span |
| 223 | */ |
| 224 | export function createOTelSpanFromTraceSpan(traceSpan: TraceSpan, parentSpan?: Span): Span | null { |
| 225 | try { |
| 226 | const tracer = getTracer() |
| 227 | |
| 228 | const blockMapping = BLOCK_TYPE_MAPPING[traceSpan.type] || { |
| 229 | spanName: `block.${traceSpan.type}`, |
| 230 | spanKind: 'internal', |
| 231 | getAttributes: (span: TraceSpan) => ({ |
| 232 | 'block.type': span.type, |
| 233 | 'block.id': span.blockId, |
| 234 | 'block.name': span.name, |
| 235 | }), |
| 236 | } |
| 237 | |
| 238 | const attributes = { |
| 239 | ...blockMapping.getAttributes(traceSpan), |
| 240 | 'span.type': traceSpan.type, |
| 241 | 'span.duration_ms': traceSpan.duration, |
| 242 | 'span.status': traceSpan.status, |
| 243 | } |
| 244 | |
| 245 | const ctx = parentSpan ? trace.setSpan(context.active(), parentSpan) : context.active() |
| 246 | |
| 247 | const span = tracer.startSpan( |
| 248 | blockMapping.spanName, |
| 249 | { |
| 250 | attributes, |
| 251 | startTime: new Date(traceSpan.startTime), |
| 252 | }, |
| 253 | ctx |
| 254 | ) |
| 255 | |
| 256 | if (traceSpan.status === 'error') { |
| 257 | const errorMessage = |
| 258 | typeof traceSpan.output?.error === 'string' |
| 259 | ? traceSpan.output.error |
| 260 | : 'Block execution failed' |
| 261 | |
| 262 | span.setStatus({ |
| 263 | code: SpanStatusCode.ERROR, |
| 264 | message: errorMessage, |
| 265 | }) |
| 266 | |
| 267 | if (errorMessage && errorMessage !== 'Block execution failed') { |
| 268 | span.recordException(new Error(errorMessage)) |
| 269 | } |
| 270 | } else { |
| 271 | span.setStatus({ code: SpanStatusCode.OK }) |
| 272 | } |
| 273 | |
| 274 | if (traceSpan.children && traceSpan.children.length > 0) { |
| 275 | for (const childTraceSpan of traceSpan.children) { |
| 276 | createOTelSpanFromTraceSpan(childTraceSpan, span) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (traceSpan.toolCalls && traceSpan.toolCalls.length > 0) { |
| 281 | for (const toolCall of traceSpan.toolCalls) { |
no test coverage detected