You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
Copy file name to clipboardExpand all lines: .cursor/rules/adding-a-new-ai-integration.mdc
+69-17Lines changed: 69 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,32 @@ Multi-runtime considerations:
25
25
- Edge (Cloudflare/Vercel): No OTel, processors only or manual wrapping
26
26
```
27
27
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.
- 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
+
28
54
---
29
55
30
56
## Span Hierarchy
@@ -139,6 +165,10 @@ OpenTelemetry Semantic Convention attribute names. **Always use these constants!
- `GEN_AI_OPERATION_NAME_ATTRIBUTE` - 'chat', 'embeddings', etc.
141
167
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
+
142
172
### `utils.ts`
143
173
144
174
- `setTokenUsageAttributes()` - Set token usage on span
@@ -213,6 +243,8 @@ OpenTelemetry Semantic Convention attribute names. **Always use these constants!
213
243
214
244
## Auto-Instrumentation (Out-of-the-Box Support)
215
245
246
+
**MANDATORY**
247
+
216
248
**RULE:** AI SDKs should be auto-enabled in Node.js runtime if possible.
217
249
218
250
✅ **Auto-enable if:**
@@ -269,12 +301,26 @@ export type { {Provider}Options } from './integrations/tracing/{provider}';
269
301
export { {provider}Integration } from './integrations/tracing/{provider}';
270
302
```
271
303
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}/`:
273
307
274
308
- Verify spans created automatically (no manual setup)
275
309
- Test `recordInputs` and `recordOutputs` options
276
310
- Test integration can be disabled
277
311
312
+
For Cloudflare Workers integrations, add tests in `dev-packages/cloudflare-integration-tests/suites/tracing/{provider}`:
- 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
+
278
324
---
279
325
280
326
## Directory Structure
@@ -307,15 +353,17 @@ packages/
307
353
308
354
## Key Best Practices
309
355
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
319
367
320
368
---
321
369
@@ -329,16 +377,20 @@ packages/
329
377
330
378
## Auto-Instrumentation Checklist
331
379
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.)
0 commit comments