Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// <reference lib="deno.ns" />

import { tracingChannel } from 'node:diagnostics_channel';
import type { TransactionEvent } from '@sentry/core';
import type { DenoClient } from '@sentry/deno';
import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno';
import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts';
import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts';
import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts';

function resetGlobals(): void {
getCurrentScope().clear();
getCurrentScope().setClient(undefined);
getIsolationScope().clear();
getGlobalScope().clear();
}

/** See deno-redis.test.ts — same sink shape, deduped for clarity. */
function transactionSink(): {
beforeSendTransaction: (event: TransactionEvent) => null;
waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise<TransactionEvent>;
} {
const transactions: TransactionEvent[] = [];
const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = [];
return {
beforeSendTransaction(event) {
transactions.push(event);
for (let i = waiters.length - 1; i >= 0; i--) {
const w = waiters[i]!;
if (w.predicate(event)) {
waiters.splice(i, 1);
w.resolve(event);
}
}
return null;
},
waitFor(predicate) {
const already = transactions.find(predicate);
if (already) return Promise.resolve(already);
return new Promise<TransactionEvent>(resolve => {
waiters.push({ predicate, resolve });
});
},
};
}

function withTimeout<T>(p: Promise<T>, ms: number, what: string): Promise<T> {
let timer: ReturnType<typeof setTimeout> | undefined;
const timeout = new Promise<T>((_, reject) => {
timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms);
});
return Promise.race([p, timeout]).finally(() => {
if (timer !== undefined) clearTimeout(timer);
});
}

Deno.test('google-genai instrumentation: included in default integrations (Deno 2.8.0+)', () => {
resetGlobals();
const client = init({ dsn: 'https://username@domain/123' }) as DenoClient;
const names = client.getOptions().integrations.map(i => i.name);
assert(names.includes('Google_GenAI'), `Google_GenAI should be in defaults, got ${names.join(', ')}`);
});
Comment on lines +57 to +62

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The test unconditionally asserts Google_GenAI is a default integration, but it's only included on Deno 2.8.0+. This will cause test failures on older Deno versions.
Severity: MEDIUM

Suggested Fix

Conditionally skip the test assertion if the Deno version is less than 2.8.0. This can be achieved by checking the MODULE_REGISTER_HOOKS_SUPPORTED flag within the test file and only running the assertion if it's true. This will align the test's logic with the SDK's conditional inclusion logic.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
dev-packages/deno-integration-tests/suites/orchestrion-google-genai/test.ts#L57-L62

Potential issue: The `googleGenAIChannelIntegration()` is conditionally added to the
default integrations only on Deno versions 2.8.0 and newer, controlled by the
`MODULE_REGISTER_HOOKS_SUPPORTED` flag. However, the corresponding integration test
unconditionally asserts that the `'Google_GenAI'` integration is present in the
defaults. This discrepancy will cause the test to fail when run in an environment with a
Deno version older than 2.8.0, as the test suite does not enforce a minimum Deno
version. This pattern is reportedly present in over 20 other integration tests,
indicating a systemic issue.

Also affects:

  • packages/deno/src/sdk.ts:96~102


Deno.test('google-genai instrumentation: orchestrion @google/genai:generate-content channel produces a nested gen_ai span', async () => {
resetGlobals();
const sink = transactionSink();
init({
Comment on lines +64 to +67

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: A module-level installedIntegrations array is not cleared between tests, causing subsequent init() calls to skip setup and leading to test failures.
Severity: MEDIUM

Suggested Fix

The installedIntegrations array should be cleared between tests. This can be done by exporting a reset function from @sentry/core that clears the array and then calling this new function within resetGlobals(). This will ensure each test runs in a clean state.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
dev-packages/deno-integration-tests/suites/orchestrion-google-genai/test.ts#L69-L72

Potential issue: A module-level array, `installedIntegrations`, is not cleared between
tests, even when `resetGlobals()` is called. When a second test in the same file calls
`init()`, the integration's `setupOnce()` function is skipped because the integration
name is already present in the array from the first test's execution. This prevents
diagnostic channel subscriptions from being set up for the second test. Consequently,
the test times out waiting for a transaction that is never created, causing a test
failure.

Also affects:

  • packages/core/src/integration.ts:10
  • packages/core/src/integration.ts:117~119

Did we get this right? 👍 / 👎 to inform future reviews.

dsn: 'https://username@domain/123',
tracesSampleRate: 1,
beforeSendTransaction: sink.beforeSendTransaction,
});

Comment thread
sentry[bot] marked this conversation as resolved.
Comment on lines +64 to +72

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The module-level subscribed flag in the google-genai integration is not reset between tests, causing subsequent init() calls in the same file to fail to set up tracing.
Severity: MEDIUM

Suggested Fix

Export a reset function from the google-genai.ts integration module that sets the subscribed flag back to false. Call this new reset function from within the resetGlobals() helper in the test suite to ensure a clean state between test runs.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
dev-packages/deno-integration-tests/suites/orchestrion-google-genai/test.ts#L64-L72

Potential issue: The `google-genai.ts` integration uses a module-level `subscribed` flag
to prevent re-subscribing to diagnostics channels. In the Deno test environment, tests
within the same file share module scope. The first test sets `subscribed` to `true`. A
`resetGlobals()` function is called between tests, but it fails to reset this
module-level flag. Consequently, when the second test calls `init()`, the integration's
`setupOnce()` function sees that `subscribed` is already `true` and exits prematurely,
skipping the channel subscription logic. This causes the second test to fail its
assertion, as no AI span is created.

const channel = tracingChannel('orchestrion:@google/genai:generate-content');

// `arguments[0]` is the request params passed to `generateContent(params)`.
const params = { model: 'gemini-1.5-flash', contents: 'hi' };
const ctx: Record<string, unknown> = { arguments: [params] };

startSpan({ name: 'parent', op: 'test' }, () => {
channel.start.runStores(ctx, () => undefined);
channel.end.publish(ctx);
ctx.result = {
modelVersion: 'gemini-1.5-flash-002',
usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
};
channel.asyncEnd.publish(ctx);
});

const parent = await withTimeout(
sink.waitFor(t => t.transaction === 'parent'),
5000,
"'parent' transaction",
);

const aiSpan = parent.spans?.find(s => s.op === 'gen_ai.generate_content');
assertExists(
aiSpan,
`expected a gen_ai.generate_content child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`,
);
assertEquals(aiSpan!.description, 'generate_content gemini-1.5-flash');
assertEquals(aiSpan!.data?.['gen_ai.system'], 'google_genai');
assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'generate_content');
assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'gemini-1.5-flash');
assertEquals(aiSpan!.data?.['gen_ai.response.model'], 'gemini-1.5-flash-002');
assertEquals(aiSpan!.data?.['gen_ai.usage.total_tokens'], 15);
assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.ai.orchestrion.google_genai');
});
1 change: 1 addition & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export {
expressChannelIntegration,
firebaseChannelIntegration,
genericPoolChannelIntegration,
googleGenAIChannelIntegration,
graphqlDiagnosticsChannelIntegration,
hapiChannelIntegration,
kafkajsChannelIntegration,
Expand Down
2 changes: 2 additions & 0 deletions packages/deno/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
expressChannelIntegration,
firebaseChannelIntegration,
genericPoolChannelIntegration,
googleGenAIChannelIntegration,
graphqlDiagnosticsChannelIntegration,
hapiChannelIntegration,
kafkajsChannelIntegration,
Expand Down Expand Up @@ -95,6 +96,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
expressChannelIntegration(),
firebaseChannelIntegration(),
genericPoolChannelIntegration(),
googleGenAIChannelIntegration(),
hapiChannelIntegration(),
kafkajsChannelIntegration(),
koaChannelIntegration(),
Expand Down
4 changes: 4 additions & 0 deletions packages/deno/test/__snapshots__/mod.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ snapshot[`captureException 1`] = `
"Express",
"Firebase",
"GenericPool",
"Google_GenAI",
"Hapi",
"Kafka",
"Koa",
Expand Down Expand Up @@ -216,6 +217,7 @@ snapshot[`captureMessage 1`] = `
"Express",
"Firebase",
"GenericPool",
"Google_GenAI",
"Hapi",
"Kafka",
"Koa",
Expand Down Expand Up @@ -317,6 +319,7 @@ snapshot[`captureMessage twice 1`] = `
"Express",
"Firebase",
"GenericPool",
"Google_GenAI",
"Hapi",
"Kafka",
"Koa",
Expand Down Expand Up @@ -425,6 +428,7 @@ snapshot[`captureMessage twice 2`] = `
"Express",
"Firebase",
"GenericPool",
"Google_GenAI",
"Hapi",
"Kafka",
"Koa",
Expand Down
Loading