Skip to content

Commit 90317db

Browse files
authored
feat: Document OpenAI integration (#14511)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR Documents OpenA integration added with getsentry/sentry-javascript#17022 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [x] Other deadline: <!-- ENTER DATE HERE --> - [ ] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## LEGAL BOILERPLATE <!-- Sentry employees and contractors can delete or ignore this section. --> Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/)
1 parent eb2b69a commit 90317db

15 files changed

+157
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: OpenAI
3+
description: "Adds instrumentation for OpenAI API."
4+
supported:
5+
- javascript.node
6+
- javascript.aws-lambda
7+
- javascript.azure-functions
8+
- javascript.connect
9+
- javascript.express
10+
- javascript.fastify
11+
- javascript.gcp-functions
12+
- javascript.hapi
13+
- javascript.hono
14+
- javascript.koa
15+
- javascript.nestjs
16+
- javascript.electron
17+
- javascript.nextjs
18+
- javascript.nuxt
19+
- javascript.solidstart
20+
- javascript.sveltekit
21+
- javascript.react-router
22+
- javascript.remix
23+
- javascript.astro
24+
- javascript.bun
25+
- javascript.tanstackstart-react
26+
- javascript.cloudflare
27+
---
28+
29+
<Alert>
30+
31+
This integration works in the Node.js, Cloudflare Workers, Vercel Edge Functions runtimes. It requires SDK version `10.2.0` or higher.
32+
33+
</Alert>
34+
35+
_Import name: `Sentry.openAIIntegration`_
36+
37+
The `openAIIntegration` adds instrumentation for the `openai` API to capture spans by automatically wrapping OpenAI client calls and recording LLM interactions with configurable input/output recording.
38+
39+
<PlatformSection notSupported={["javascript.cloudflare", "javascript.nextjs"]}>
40+
It is enabled by default and will automatically capture spans for OpenAI API method calls. You can opt-in to capture inputs and outputs by setting `recordInputs` and `recordOutputs` in the integration config:
41+
42+
```javascript
43+
Sentry.init({
44+
dsn: "____PUBLIC_DSN____",
45+
tracesSampleRate: 1.0,
46+
integrations: [
47+
Sentry.openAIIntegration({
48+
recordInputs: true,
49+
recordOutputs: true,
50+
}),
51+
],
52+
});
53+
```
54+
55+
</PlatformSection>
56+
57+
<PlatformSection supported={["javascript.cloudflare"]}>
58+
For Cloudflare Workers, you need to manually instrument the OpenAI client using the `instrumentOpenAiClient` helper:
59+
60+
```javascript
61+
import * as Sentry from "@sentry/cloudflare";
62+
import OpenAI from "openai";
63+
64+
const openai = new OpenAI();
65+
const client = Sentry.instrumentOpenAiClient(openai, {
66+
recordInputs: true,
67+
recordOutputs: true,
68+
});
69+
70+
// Use the wrapped client instead of the original openai instance
71+
const response = await client.chat.completions.create({
72+
model: "gpt-4o",
73+
messages: [{ role: "user", content: "Hello!" }],
74+
});
75+
```
76+
77+
</PlatformSection>
78+
79+
<PlatformSection supported={['javascript.nextjs']}>
80+
81+
This integration is automatically instrumented in the Node.js runtime. For Next.js applications using the Edge runtime, you need to manually instrument the OpenAI client:
82+
83+
```javascript
84+
import * as Sentry from "@sentry/nextjs";
85+
import OpenAI from "openai";
86+
87+
const openai = new OpenAI();
88+
const client = Sentry.instrumentOpenAiClient(openai, {
89+
recordInputs: true,
90+
recordOutputs: true,
91+
});
92+
93+
// Use the wrapped client instead of the original openai instance
94+
const response = await client.chat.completions.create({
95+
model: "gpt-4o",
96+
messages: [{ role: "user", content: "Hello!" }],
97+
});
98+
```
99+
100+
</PlatformSection>
101+
102+
## Options
103+
104+
### `recordInputs`
105+
106+
_Type: `boolean`_
107+
108+
Records inputs to OpenAI API method calls (such as prompts and messages).
109+
110+
Defaults to `true` if `sendDefaultPii` is `true`.
111+
112+
```javascript
113+
Sentry.init({
114+
integrations: [Sentry.openAIIntegration({ recordInputs: true })],
115+
});
116+
```
117+
118+
### `recordOutputs`
119+
120+
_Type: `boolean`_
121+
122+
Records outputs from OpenAI API method calls (such as generated text and responses).
123+
124+
Defaults to `true` if `sendDefaultPii` is `true`.
125+
126+
```javascript
127+
Sentry.init({
128+
integrations: [Sentry.openAIIntegration({ recordOutputs: true })],
129+
});
130+
```
131+
132+
## Configuration
133+
134+
By default this integration adds tracing support to OpenAI API method calls including:
135+
136+
- `chat.completions.create()` - Chat completion requests
137+
- `responses.create()` - Response API requests
138+
139+
The integration will automatically detect streaming vs non-streaming requests and handle them appropriately.
140+
141+
## Supported Versions
142+
143+
- `openai`: `>=4.0.0 <6`

platform-includes/configuration/integrations/javascript.astro.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,4 @@ Depending on whether an integration enhances the functionality of a particular r
8181
| [`localVariablesIntegration`](./localvariables) | || | |
8282
| [`nodeProfilingIntegration`](./nodeprofiling) | | || |
8383
| [`trpcMiddleware`](./trpc) | ||||
84+
| [`openAIIntegration`](./openai) || || |

platform-includes/configuration/integrations/javascript.aws-lambda.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| [`onUnhandledRejectionIntegration`](./unhandledrejection) ||| | |
1919
| [`childProcessIntegration`](./childProcess) || | ||
2020
| [`vercelAiIntegration`](./vercelai) || || |
21+
| [`openAIIntegration`](./openai) || || |
2122
| [`amqplibIntegration`](./amqplib) | | || |
2223
| [`anrIntegration`](./anr) | || | |
2324
| [`captureConsoleIntegration`](./captureconsole) | | | ||

platform-includes/configuration/integrations/javascript.bun.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
| [`tediousIntegration`](./tedious) || || |
3131
| [`prismaIntegration`](./prisma) || || |
3232
| [`vercelAiIntegration`](./vercelai) || || |
33+
| [`openAIIntegration`](./openai) || || |
3334
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3435
| [`dataloaderIntegration`](./dataloader) | | || |
3536
| [`extraErrorDataIntegration`](./extraerrordata) | | | ||

platform-includes/configuration/integrations/javascript.connect.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| [`childProcessIntegration`](./childProcess) || | ||
3232
| [`rewriteFramesIntegration`](./rewriteframes) ||| | |
3333
| [`vercelAiIntegration`](./vercelai) || || |
34+
| [`openAIIntegration`](./openai) || || |
3435
| [`anrIntegration`](./anr) | || | |
3536
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3637
| [`eventLoopBlockIntegration`](./event-loop-block) | || | |

platform-includes/configuration/integrations/javascript.fastify.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| [`childProcessIntegration`](./childProcess) || | ||
3232
| [`prismaIntegration`](./prisma) || || |
3333
| [`vercelAiIntegration`](./vercelai) || || |
34+
| [`openAIIntegration`](./openai) || || |
3435
| [`anrIntegration`](./anr) | || | |
3536
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3637
| [`eventLoopBlockIntegration`](./event-loop-block) | || | |

platform-includes/configuration/integrations/javascript.gcp-functions.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| [`onUnhandledRejectionIntegration`](./unhandledrejection) ||| | |
1919
| [`childProcessIntegration`](./childProcess) || | ||
2020
| [`vercelAiIntegration`](./vercelai) || || |
21+
| [`openAIIntegration`](./openai) || || |
2122
| [`amqplibIntegration`](./amqplib) | | || |
2223
| [`anrIntegration`](./anr) | || | |
2324
| [`captureConsoleIntegration`](./captureconsole) | | | ||

platform-includes/configuration/integrations/javascript.hapi.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| [`childProcessIntegration`](./childProcess) || | ||
3232
| [`prismaIntegration`](./prisma) || || |
3333
| [`vercelAiIntegration`](./vercelai) || || |
34+
| [`openAIIntegration`](./openai) || || |
3435
| [`anrIntegration`](./anr) | || | |
3536
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3637
| [`eventLoopBlockIntegration`](./event-loop-block) | || | |

platform-includes/configuration/integrations/javascript.nestjs.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444
| [`supabaseIntegration`](./supabase) | ||| |
4545
| [`trpcMiddleware`](./trpc) | ||||
4646
| [`unleashIntegration`](./unleash) | | | ||
47+
| [`openAIIntegration`](./openai) || || |

platform-includes/configuration/integrations/javascript.nextjs.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Depending on whether an integration enhances the functionality of a particular r
8787
| [`nodeProfilingIntegration`](./nodeprofiling) | | || |
8888
| [`trpcMiddleware`](./trpc) | ||||
8989
| [`vercelAiIntegration`](./vercelai) || |||
90+
| [`openAIIntegration`](./openai) || || |
9091

9192
### Edge Integrations
9293

0 commit comments

Comments
 (0)