Skip to content

feat(ai): correlate ai-gateway calls with functionPath/traceId - #181

Merged
prisis merged 1 commit into
alphafrom
feat/ai-gateway-metadata
Jul 23, 2026
Merged

prisis merged 1 commit into
alphafrom
feat/ai-gateway-metadata

Conversation

@prisis

@prisis prisis commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Auto-threads functionPath + traceId into the AI-Gateway call so gateway logs correlate to the Lunora trace (the #165 follow-up).

  • Both ids are reachable at ai-facade construction in the generated buildCtx; the emit fragment threads them under the same anchor-guard the tracer uses (a deferred subscription re-run can't borrow another dispatch's trace).
  • Uses Cloudflare's native gateway.metadata for Workers-AI/binding calls and the cf-aig-metadata header for BYO providers. Additive/opt-in — attached only when a gateway resolves, never overrides a caller's own metadata.
  • resolveGatewayOption folds it into both the AI-SDK model path and raw ai.run().

ai 119 / codegen 863 tests pass. (Follow-up commit clears pre-existing prefer-expect-assertions debt in ai test files that the affected-lint surfaces.)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added optional metadata support for AI Gateway requests.
    • AI calls can now include function path and trace ID details for improved correlation in gateway logs and tracing.
    • Added a public helper for constructing AI Gateway metadata fields.
    • Gateway metadata is applied consistently when a gateway is configured.

The generated `ctx.ai` facade now threads the dispatch's `functionPath`
and `traceId` into `createAi`, which folds them into the active AI
Gateway's native `metadata` (and `resolveAiGateway`'s `cf-aig-metadata`
header) so gateway log entries tie back to the Lunora trace.

Additive and opt-in: metadata is attached only when a gateway is
configured (explicit or env-derived) and never overrides a caller's own
gateway metadata; unset gateways are unchanged. The emitter reads the
trace under the same anchor guard the tracer uses so a deferred
subscription re-run does not borrow a concurrent dispatch's trace.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
@netlify

netlify Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for lunorash ready!

Name Link
🔨 Latest commit e3e91cb
🔍 Latest deploy log https://app.netlify.com/projects/lunorash/deploys/6a61509e537aa900087bb6d0
😎 Deploy Preview https://deploy-preview-181--lunorash.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b4787f28-f537-4269-928e-793592ac4dae

📥 Commits

Reviewing files that changed from the base of the PR and between 32f70f2 and e3e91cb.

⛔ Files ignored due to path filters (4)
  • packages/ai/__tests__/build-provider.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/ai/__tests__/create-ai.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/ai/__tests__/gateway.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/codegen/__tests__/run-codegen.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
📒 Files selected for processing (5)
  • packages/ai/src/create-ai.ts
  • packages/ai/src/gateway.ts
  • packages/ai/src/index.ts
  • packages/ai/src/types.ts
  • packages/codegen/src/emit.ts

Walkthrough

AI Gateway metadata types and encoding are extended for correlation fields. Gateway resolution merges metadata into active gateway options, while generated AI calls pass function paths and trace IDs into createAi.

Changes

AI Gateway correlation metadata

Layer / File(s) Summary
Metadata contracts and encoding
packages/ai/src/types.ts, packages/ai/src/gateway.ts, packages/ai/src/index.ts
Adds gateway metadata options, projects non-empty correlation fields, encodes them for cf-aig-metadata, and exports the helper.
Metadata-aware gateway resolution
packages/ai/src/create-ai.ts
Passes metadata through createAi and merges correlation fields into resolved gateway configuration when applicable.
Generated tracing integration
packages/codegen/src/emit.ts
Generated ctx.ai calls pass functionPath and the current traceId to createAi when an AI binding is present.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Generated ctx.ai
  participant createAi
  participant resolveGatewayOption
  participant AI Gateway
  Generated ctx.ai->>Generated ctx.ai: Derive functionPath and traceId
  Generated ctx.ai->>createAi: Pass metadata
  createAi->>resolveGatewayOption: Resolve gateway with metadata
  resolveGatewayOption->>AI Gateway: Forward gateway metadata
Loading

Possibly related PRs

  • anolilab/lunora#165: Updates related AI Gateway routing and telemetry handling in the same AI package.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the core change but misses most required template sections, including linked issues, test plan, checklist, notes, and the CLA text. Add the missing headings from the template, especially Linked issues, Test plan, Checklist, Notes for reviewers, and the exact CLA statement.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, conventional, and accurately describes the main change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/ai-gateway-metadata

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for confirming the Contributor License Agreement! 🙏

@codspeed

codspeed Bot commented Jul 22, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 4 untouched benchmarks
⏩ 157 skipped benchmarks1


Comparing feat/ai-gateway-metadata (e3e91cb) with alpha (c62d862)2

Open in CodSpeed

Footnotes

  1. 157 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on alpha (32f70f2) during the generation of this report, so c62d862 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@prisis
prisis merged commit 065832f into alpha Jul 23, 2026
44 checks passed
@prisis
prisis deleted the feat/ai-gateway-metadata branch July 23, 2026 05:23
prisis added a commit that referenced this pull request Jul 24, 2026
Sync the cloud observability + security-hardening branch with alpha (OTLP
resource attributes #182, notify observability #183, do perf #184, workerd
custom-spans test #180, ai-gateway correlation #181, and release bumps).
Clean auto-merge — no conflicts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
prisis added a commit that referenced this pull request Jul 24, 2026
Codegen picks up alpha's ai-gateway trace correlation (#181): the emitted
ShardDO now threads functionPath + traceId into createAi. Matches the merged
runtime; tsc validated against this output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
prisis added a commit that referenced this pull request Aug 2, 2026
Codegen picks up alpha's ai-gateway trace correlation (#181): the emitted
ShardDO now threads functionPath + traceId into createAi. Matches the merged
runtime; tsc validated against this output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
prisis added a commit that referenced this pull request Aug 7, 2026
Codegen picks up alpha's ai-gateway trace correlation (#181): the emitted
ShardDO now threads functionPath + traceId into createAi. Matches the merged
runtime; tsc validated against this output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
prisis added a commit that referenced this pull request Aug 19, 2026
Codegen picks up alpha's ai-gateway trace correlation (#181): the emitted
ShardDO now threads functionPath + traceId into createAi. Matches the merged
runtime; tsc validated against this output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
prisis added a commit that referenced this pull request Aug 20, 2026
Codegen picks up alpha's ai-gateway trace correlation (#181): the emitted
ShardDO now threads functionPath + traceId into createAi. Matches the merged
runtime; tsc validated against this output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
prisis added a commit that referenced this pull request Aug 20, 2026
Codegen picks up alpha's ai-gateway trace correlation (#181): the emitted
ShardDO now threads functionPath + traceId into createAi. Matches the merged
runtime; tsc validated against this output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
prisis added a commit that referenced this pull request Aug 24, 2026
Codegen picks up alpha's ai-gateway trace correlation (#181): the emitted
ShardDO now threads functionPath + traceId into createAi. Matches the merged
runtime; tsc validated against this output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using our GitHub Discussions tab for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 8, 2026

This branch was previously deployed

1 inactive deployment
benchmarks — e3e91cbc Deployed Jul 22, 2026 by prisis via Benchmarks #887
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant