Skip to content

Commit e5b19a6

Browse files
authored
chore: Enhance AI integration guidelines with runtime-specific placem… (#19296)
Enhances the AI integration guidelines with: - Runtime-specific placement rules (Node.js, Cloudflare Workers, Browser only SDKs should stay in their respective packages) - Mandatory auto-detection requirement for all AI integrations - E2E testing references for Cloudflare Workers and Browser runtimes - Attribute restriction rule (only use attributes from https://getsentry.github.io/sentry-conventions/attributes/gen_ai/) Closes #19297 (added automatically)
1 parent bc09cf3 commit e5b19a6

File tree

1 file changed

+69
-17
lines changed

1 file changed

+69
-17
lines changed

.cursor/rules/adding-a-new-ai-integration.mdc

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ Multi-runtime considerations:
2525
- Edge (Cloudflare/Vercel): No OTel, processors only or manual wrapping
2626
```
2727

28+
**IMPORTANT - Runtime-Specific Placement:**
29+
30+
If an AI SDK only works in a specific runtime, the integration code should live exclusively in that runtime's package. Do NOT add it to `packages/core/` or attempt to make it work in other runtimes where it cannot function.
31+
32+
**Runtime-specific integration structures:**
33+
34+
**Node.js-only SDKs** → `packages/node/`
35+
36+
- Core logic: `packages/node/src/integrations/tracing/{provider}/index.ts`
37+
- OTel instrumentation: `packages/node/src/integrations/tracing/{provider}/instrumentation.ts`
38+
- Use when SDK only work with Node.js-specific APIs
39+
40+
**Cloudflare Workers-only SDKs** → `packages/cloudflare/`
41+
42+
- Single file: `packages/cloudflare/src/integrations/tracing/{provider}.ts`
43+
- Use when SDK only works with Cloudflare Workers APIs or Cloudflare AI
44+
45+
**Browser-only SDKs** → `packages/browser/`
46+
47+
- Core logic: `packages/browser/src/integrations/tracing/{provider}/index.ts`
48+
- Use when SDK requires browser-specific APIs (DOM, WebAPIs, etc.)
49+
50+
**For all runtime-specific SDKs:** DO NOT create `packages/core/src/tracing/{provider}/` - keep everything in the runtime package.
51+
52+
**Multi-runtime SDKs:** If the SDK works across multiple runtimes (Node.js, browser, edge), follow the standard pattern with shared core logic in `packages/core/` and runtime-specific wrappers/instrumentation in each package where needed.
53+
2854
---
2955

3056
## Span Hierarchy
@@ -139,6 +165,10 @@ OpenTelemetry Semantic Convention attribute names. **Always use these constants!
139165
- `GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE` - Token counts
140166
- `GEN_AI_OPERATION_NAME_ATTRIBUTE` - 'chat', 'embeddings', etc.
141167

168+
**CRITICAL - Attribute Usage:**
169+
170+
Only use attributes explicitly listed in the [Sentry Gen AI Conventions](https://getsentry.github.io/sentry-conventions/attributes/gen_ai/). Do NOT create custom attributes or use undocumented ones. If you need a new attribute, it MUST be documented in the conventions first before implementation.
171+
142172
### `utils.ts`
143173

144174
- `setTokenUsageAttributes()` - Set token usage on span
@@ -213,6 +243,8 @@ OpenTelemetry Semantic Convention attribute names. **Always use these constants!
213243

214244
## Auto-Instrumentation (Out-of-the-Box Support)
215245

246+
**MANDATORY**
247+
216248
**RULE:** AI SDKs should be auto-enabled in Node.js runtime if possible.
217249

218250
✅ **Auto-enable if:**
@@ -269,12 +301,26 @@ export type { {Provider}Options } from './integrations/tracing/{provider}';
269301
export { {provider}Integration } from './integrations/tracing/{provider}';
270302
```
271303

272-
**4. Add E2E test** in `packages/node-integration-tests/suites/{provider}/`
304+
**4. Add E2E tests**
305+
306+
For Node.js integrations, add tests in `dev-packages/node-integration-tests/suites/tracing/{provider}/`:
273307

274308
- Verify spans created automatically (no manual setup)
275309
- Test `recordInputs` and `recordOutputs` options
276310
- Test integration can be disabled
277311

312+
For Cloudflare Workers integrations, add tests in `dev-packages/cloudflare-integration-tests/suites/tracing/{provider}`:
313+
314+
- Create a new worker test app with the AI SDK
315+
- Verify manual instrumentation creates spans correctly
316+
- Test in actual Cloudflare Workers runtime (use `wrangler dev` or `miniflare`)
317+
318+
For Browser integrations, add tests in `dev-packages/browser-integration-tests/suites/tracing/ai-providers/{provider}/`:
319+
320+
- Create a new test suite with Playwright
321+
- Verify manual instrumentation creates spans correctly in the browser
322+
- Test with actual browser runtime
323+
278324
---
279325

280326
## Directory Structure
@@ -307,15 +353,17 @@ packages/
307353

308354
## Key Best Practices
309355

310-
1. **Respect `sendDefaultPii`** for recordInputs/recordOutputs
311-
2. **Use semantic attributes** from `gen-ai-attributes.ts` (never hardcode)
312-
3. **Set Sentry origin**: `SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN = 'auto.ai.openai'` (use provider name: `openai`, `anthropic`, `vercelai`, etc. - only alphanumerics, `_`, and `.` allowed)
313-
4. **Truncate large data**: Use helper functions from `utils.ts`
314-
5. **Correct span operations**: `gen_ai.invoke_agent` for parent, `gen_ai.chat` for children
315-
6. **Streaming**: Use `startSpanManual()`, accumulate state, call `span.end()`
316-
7. **Token accumulation**: Direct on child spans, accumulate on parent from children
317-
8. **Performance**: Use `callWhenPatched()` for Pattern 1
318-
9. **LangChain**: Check `_INTERNAL_shouldSkipAiProviderWrapping()` in Pattern 2
356+
1. **Auto-instrumentation is mandatory** - All integrations MUST auto-detect and instrument automatically in Node.js runtime
357+
2. **Runtime-specific placement** - If SDK only works in one runtime, code lives only in that package
358+
3. **Respect `sendDefaultPii`** for recordInputs/recordOutputs
359+
4. **Use semantic attributes** from `gen-ai-attributes.ts` (never hardcode) - Only use attributes from [Sentry Gen AI Conventions](https://getsentry.github.io/sentry-conventions/attributes/gen_ai/)
360+
5. **Set Sentry origin**: `SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN = 'auto.ai.openai'` (use provider name: `openai`, `anthropic`, `vercelai`, etc. - only alphanumerics, `_`, and `.` allowed)
361+
6. **Truncate large data**: Use helper functions from `utils.ts`
362+
7. **Correct span operations**: `gen_ai.invoke_agent` for parent, `gen_ai.chat` for children
363+
8. **Streaming**: Use `startSpanManual()`, accumulate state, call `span.end()`
364+
9. **Token accumulation**: Direct on child spans, accumulate on parent from children
365+
10. **Performance**: Use `callWhenPatched()` for Pattern 1
366+
11. **LangChain**: Check `_INTERNAL_shouldSkipAiProviderWrapping()` in Pattern 2
319367

320368
---
321369

@@ -329,16 +377,20 @@ packages/
329377

330378
## Auto-Instrumentation Checklist
331379

332-
- [ ] Added to `getAutoPerformanceIntegrations()` in correct order
333-
- [ ] Added to `getOpenTelemetryInstrumentationToPreload()`
334-
- [ ] Exported from `packages/node/src/index.ts`
335-
- [ ] **If browser-compatible:** Exported from `packages/browser/src/index.ts`
336-
- [ ] Added E2E test in `packages/node-integration-tests/suites/{provider}/`
337-
- [ ] E2E test verifies auto-instrumentation
380+
- [ ] If runtime-specific, placed code only in that runtime's package
381+
- [ ] Added to `getAutoPerformanceIntegrations()` in correct order (Node.js)
382+
- [ ] Added to `getOpenTelemetryInstrumentationToPreload()` (Node.js with OTel)
383+
- [ ] Exported from appropriate package index (`packages/node/src/index.ts`, `packages/cloudflare/src/index.ts`, etc.)
384+
- [ ] Added E2E tests:
385+
- [ ] Node.js: `dev-packages/node-integration-tests/suites/tracing/{provider}/`
386+
- [ ] Cloudflare: `dev-packages/cloudflare-integration-tests/suites/tracing/{provider}/`
387+
- [ ] Browser: `dev-packages/browser-integration-tests/suites/tracing/ai-providers/{provider}/`
388+
- [ ] E2E test verifies auto-instrumentation (no manual setup required)
389+
- [ ] Only used attributes from [Sentry Gen AI Conventions](https://getsentry.github.io/sentry-conventions/attributes/gen_ai/)
338390
- [ ] JSDoc says "enabled by default" or "not enabled by default"
339391
- [ ] Documented how to disable (if auto-enabled)
340392
- [ ] Documented limitations clearly
341-
- [ ] Verified OTel only patches when package imported
393+
- [ ] Verified OTel only patches when package imported (Node.js)
342394

343395
---
344396

0 commit comments

Comments
 (0)