Skip to content

feat(otel): add option for custom span attributes#14975

Merged
aayush-kapoor merged 6 commits intomainfrom
aayush/enrich-otel-spans
May 8, 2026
Merged

feat(otel): add option for custom span attributes#14975
aayush-kapoor merged 6 commits intomainfrom
aayush/enrich-otel-spans

Conversation

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

@aayush-kapoor aayush-kapoor commented May 4, 2026

Background

#14727

@ai-sdk/otel currently does not expose a supported way to add attributes at span creation time. this is for external observability systems that sometimes need to add additional span attributes that are specific to their ingestion pipeline

Summary

Added a new enrichSpanAttributes option to OpenTelemetry so callers can append custom attributes when spans are created

Manual Verification

ran the example examples/ai-functions/src/telemetry/otel/generate-text-custom-attributes.ts

Checklist

  • All commits are signed (PRs with unsigned commits cannot be merged)
  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

fixes #14727

sdk.start();
registerTelemetry(
new OpenTelemetry({
enrichSpanAttributes: ({ spanType, operationId, runtimeContext }) => ({
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am working on restricting how the runtimeContext is exposed to telemetry for security so this might conflict somewhat

Comment thread packages/otel/src/supplemental-attributes.ts Outdated
Comment on lines +20 to +26
export type OpenTelemetrySpanType =
| 'operation'
| 'step'
| 'languageModel'
| 'tool'
| 'embedding'
| 'reranking';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these mix language model and other span -- i am wondering if there are better names, e.g. hierarchical

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a span type but

Comment thread packages/otel/src/supplemental-attributes.ts Outdated
@aayush-kapoor aayush-kapoor merged commit c025d60 into main May 8, 2026
19 checks passed
@aayush-kapoor aayush-kapoor deleted the aayush/enrich-otel-spans branch May 8, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add span attribute enrichment hook to @ai-sdk/otel

2 participants