| 335 | * @since 4.0.0 |
| 336 | */ |
| 337 | export const addSpanAttributes = ( |
| 338 | /** |
| 339 | * The prefix to add to all attribute keys. |
| 340 | */ |
| 341 | keyPrefix: string, |
| 342 | /** |
| 343 | * Function to transform attribute keys (e.g., camelCase to snake_case). |
| 344 | */ |
| 345 | transformKey: (key: string) => string |
| 346 | ) => |
| 347 | <Attributes extends Record<string, any>>( |
| 348 | /** |
| 349 | * The OpenTelemetry span to add attributes to. |
| 350 | */ |
| 351 | span: Span, |
| 352 | /** |
| 353 | * The attributes to add to the span. |
| 354 | */ |
| 355 | attributes: Attributes |
| 356 | ): void => { |
| 357 | for (const [key, value] of Object.entries(attributes)) { |
| 358 | if (Predicate.isNotNullish(value)) { |
| 359 | span.attribute(`${keyPrefix}.${transformKey(key)}`, value) |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | const addSpanBaseAttributes = addSpanAttributes("gen_ai", String.camelToSnake)<BaseAttributes> |
| 365 | const addSpanOperationAttributes = addSpanAttributes("gen_ai.operation", String.camelToSnake)<OperationAttributes> |