-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(deno): Add OpenTelemetry support and vercelAI integration #17445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sergical
wants to merge
8
commits into
develop
Choose a base branch
from
feat/deno-opentelemetry-vercelai
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4269b46
feat(deno): Add OpenTelemetry support and vercelAI integration
sergical 1427b8b
lint
sergical 814a1e3
better tests
sergical 446f901
Merge branch 'develop' into feat/deno-opentelemetry-vercelai
sergical 170d300
fixes for 🤖
sergical 3b38271
Merge branch 'develop' into feat/deno-opentelemetry-vercelai
sergical a84b0d9
Merge branch 'develop' into feat/deno-opentelemetry-vercelai
sergical f98559f
Merge branch 'develop' into feat/deno-opentelemetry-vercelai
sergical File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* This is a copy of the Vercel AI integration from the cloudflare SDK. | ||
* | ||
* The only difference is that it does not use `@opentelemetry/instrumentation` | ||
* because Deno Workers do not support it in the same way. | ||
* | ||
* Therefore, we cannot automatically patch setting `experimental_telemetry: { isEnabled: true }` | ||
* and users have to manually set this to get spans. | ||
*/ | ||
|
||
import type { IntegrationFn } from '@sentry/core'; | ||
import { addVercelAiProcessors, defineIntegration } from '@sentry/core'; | ||
|
||
const INTEGRATION_NAME = 'VercelAI'; | ||
|
||
const _vercelAIIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setup(client) { | ||
addVercelAiProcessors(client); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* Adds Sentry tracing instrumentation for the [ai](https://www.npmjs.com/package/ai) library. | ||
* This integration is not enabled by default, you need to manually add it. | ||
* | ||
* For more information, see the [`ai` documentation](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry). | ||
* | ||
* You need to enable collecting spans for a specific call by setting | ||
* `experimental_telemetry.isEnabled` to `true` in the first argument of the function call. | ||
* | ||
* ```javascript | ||
* const result = await generateText({ | ||
* model: openai('gpt-4-turbo'), | ||
* experimental_telemetry: { isEnabled: true }, | ||
* }); | ||
* ``` | ||
* | ||
* If you want to collect inputs and outputs for a specific call, you must specifically opt-in to each | ||
* function call by setting `experimental_telemetry.recordInputs` and `experimental_telemetry.recordOutputs` | ||
* to `true`. | ||
* | ||
* ```javascript | ||
* const result = await generateText({ | ||
* model: openai('gpt-4-turbo'), | ||
* experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true }, | ||
* }); | ||
*/ | ||
export const vercelAIIntegration = defineIntegration(_vercelAIIntegration); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { Context, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api'; | ||
import { trace } from '@opentelemetry/api'; | ||
import { startInactiveSpan, startSpanManual } from '@sentry/core'; | ||
|
||
/** | ||
* Set up a mock OTEL tracer to allow inter-op with OpenTelemetry emitted spans. | ||
* This is not perfect but handles easy/common use cases. | ||
*/ | ||
export function setupOpenTelemetryTracer(): void { | ||
trace.setGlobalTracerProvider(new SentryDenoTraceProvider()); | ||
} | ||
|
||
class SentryDenoTraceProvider implements TracerProvider { | ||
private readonly _tracers: Map<string, Tracer> = new Map(); | ||
|
||
public getTracer(name: string, version?: string, options?: { schemaUrl?: string }): Tracer { | ||
const key = `${name}@${version || ''}:${options?.schemaUrl || ''}`; | ||
if (!this._tracers.has(key)) { | ||
this._tracers.set(key, new SentryDenoTracer()); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this._tracers.get(key)!; | ||
} | ||
} | ||
|
||
class SentryDenoTracer implements Tracer { | ||
public startSpan(name: string, options?: SpanOptions): Span { | ||
return startInactiveSpan({ | ||
name, | ||
...options, | ||
attributes: { | ||
...options?.attributes, | ||
'sentry.deno_tracer': true, | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* NOTE: This does not handle `context` being passed in. It will always put spans on the current scope. | ||
*/ | ||
public startActiveSpan<F extends (span: Span) => unknown>(name: string, fn: F): ReturnType<F>; | ||
public startActiveSpan<F extends (span: Span) => unknown>(name: string, options: SpanOptions, fn: F): ReturnType<F>; | ||
public startActiveSpan<F extends (span: Span) => unknown>( | ||
name: string, | ||
options: SpanOptions, | ||
context: Context, | ||
fn: F, | ||
): ReturnType<F>; | ||
public startActiveSpan<F extends (span: Span) => unknown>( | ||
name: string, | ||
options: unknown, | ||
context?: unknown, | ||
fn?: F, | ||
): ReturnType<F> { | ||
const opts = (typeof options === 'object' && options !== null ? options : {}) as SpanOptions; | ||
|
||
const spanOpts = { | ||
name, | ||
...opts, | ||
attributes: { | ||
...opts.attributes, | ||
'sentry.deno_tracer': true, | ||
}, | ||
}; | ||
|
||
const callback = ( | ||
typeof options === 'function' | ||
? options | ||
: typeof context === 'function' | ||
? context | ||
: typeof fn === 'function' | ||
? fn | ||
: () => {} | ||
) as F; | ||
|
||
// In OTEL the semantic matches `startSpanManual` because spans are not auto-ended | ||
return startSpanManual(spanOpts, callback) as ReturnType<F>; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { assertEquals } from 'https://deno.land/[email protected]/assert/mod.ts'; | ||
import { context, propagation, trace } from 'npm:@opentelemetry/api@1'; | ||
import type { DenoClient } from '../build/esm/index.js'; | ||
import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '../build/esm/index.js'; | ||
|
||
function resetGlobals(): void { | ||
getCurrentScope().clear(); | ||
getCurrentScope().setClient(undefined); | ||
getIsolationScope().clear(); | ||
getGlobalScope().clear(); | ||
} | ||
|
||
function cleanupOtel(): void { | ||
// Disable all globally registered APIs | ||
trace.disable(); | ||
context.disable(); | ||
propagation.disable(); | ||
} | ||
|
||
function resetSdk(): void { | ||
resetGlobals(); | ||
cleanupOtel(); | ||
} | ||
|
||
Deno.test('should not capture spans emitted via @opentelemetry/api when skipOpenTelemetrySetup is true', async () => { | ||
resetSdk(); | ||
const transactionEvents: any[] = []; | ||
|
||
const client = init({ | ||
dsn: 'https://username@domain/123', | ||
tracesSampleRate: 1, | ||
skipOpenTelemetrySetup: true, | ||
beforeSendTransaction: event => { | ||
transactionEvents.push(event); | ||
return null; | ||
}, | ||
}) as DenoClient; | ||
|
||
const tracer = trace.getTracer('test'); | ||
const span = tracer.startSpan('test'); | ||
span.end(); | ||
|
||
await client.flush(); | ||
|
||
tracer.startActiveSpan('test 2', { attributes: { 'test.attribute': 'test' } }, span2 => { | ||
const span = tracer.startSpan('test 3', { attributes: { 'test.attribute': 'test2' } }); | ||
span.end(); | ||
span2.end(); | ||
}); | ||
|
||
await client.flush(); | ||
|
||
assertEquals(transactionEvents.length, 0); | ||
}); | ||
|
||
Deno.test('should capture spans emitted via @opentelemetry/api', async () => { | ||
resetSdk(); | ||
const transactionEvents: any[] = []; | ||
|
||
const client = init({ | ||
dsn: 'https://username@domain/123', | ||
tracesSampleRate: 1, | ||
beforeSendTransaction: event => { | ||
transactionEvents.push(event); | ||
return null; | ||
}, | ||
}) as DenoClient; | ||
|
||
const tracer = trace.getTracer('test'); | ||
const span = tracer.startSpan('test'); | ||
span.end(); | ||
|
||
await client.flush(); | ||
|
||
tracer.startActiveSpan('test 2', { attributes: { 'test.attribute': 'test' } }, span2 => { | ||
const span = tracer.startSpan('test 3', { attributes: { 'test.attribute': 'test2' } }); | ||
span.end(); | ||
span2.end(); | ||
}); | ||
|
||
await client.flush(); | ||
|
||
assertEquals(transactionEvents.length, 2); | ||
const [transactionEvent, transactionEvent2] = transactionEvents; | ||
|
||
assertEquals(transactionEvent?.spans?.length, 0); | ||
assertEquals(transactionEvent?.transaction, 'test'); | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.deno_tracer'], true); | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.origin'], 'manual'); | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.sample_rate'], 1); | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.source'], 'custom'); | ||
|
||
assertEquals(transactionEvent2?.spans?.length, 1); | ||
assertEquals(transactionEvent2?.transaction, 'test 2'); | ||
assertEquals(transactionEvent2?.contexts?.trace?.data?.['sentry.deno_tracer'], true); | ||
assertEquals(transactionEvent2?.contexts?.trace?.data?.['sentry.origin'], 'manual'); | ||
assertEquals(transactionEvent2?.contexts?.trace?.data?.['sentry.sample_rate'], 1); | ||
assertEquals(transactionEvent2?.contexts?.trace?.data?.['sentry.source'], 'custom'); | ||
assertEquals(transactionEvent2?.contexts?.trace?.data?.['test.attribute'], 'test'); | ||
|
||
const childSpan = transactionEvent2?.spans?.[0]; | ||
assertEquals(childSpan?.description, 'test 3'); | ||
assertEquals(childSpan?.data?.['sentry.deno_tracer'], true); | ||
assertEquals(childSpan?.data?.['sentry.origin'], 'manual'); | ||
assertEquals(childSpan?.data?.['test.attribute'], 'test2'); | ||
}); | ||
|
||
Deno.test('opentelemetry spans should interop with Sentry spans', async () => { | ||
resetSdk(); | ||
const transactionEvents: any[] = []; | ||
|
||
const client = init({ | ||
dsn: 'https://username@domain/123', | ||
tracesSampleRate: 1, | ||
beforeSendTransaction: event => { | ||
transactionEvents.push(event); | ||
return null; | ||
}, | ||
}) as DenoClient; | ||
|
||
const tracer = trace.getTracer('test'); | ||
|
||
startSpan({ name: 'sentry span' }, () => { | ||
const span = tracer.startSpan('otel span'); | ||
span.end(); | ||
}); | ||
|
||
await client.flush(); | ||
|
||
assertEquals(transactionEvents.length, 1); | ||
const [transactionEvent] = transactionEvents; | ||
|
||
assertEquals(transactionEvent?.spans?.length, 1); | ||
assertEquals(transactionEvent?.transaction, 'sentry span'); | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.origin'], 'manual'); | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.sample_rate'], 1); | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.source'], 'custom'); | ||
// Note: Sentry-created spans don't have the deno_tracer marker | ||
assertEquals(transactionEvent?.contexts?.trace?.data?.['sentry.deno_tracer'], undefined); | ||
|
||
const otelSpan = transactionEvent?.spans?.[0]; | ||
assertEquals(otelSpan?.description, 'otel span'); | ||
assertEquals(otelSpan?.data?.['sentry.deno_tracer'], true); | ||
assertEquals(otelSpan?.data?.['sentry.origin'], 'manual'); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.