Skip to content

Conversation

RulaKhaled
Copy link
Member

@RulaKhaled RulaKhaled commented Sep 12, 2025

This PR implements instrumentation for the Google GenerativeAI SDK, adding automatic tracing for both direct model calls (models.generateContent) and conversational chat sessions (chats.create + sendMessage). The implementation follows Sentry's AI Agents Manual Instrumentation conventions and includes integration tests.

Usage:

import * as Sentry from '@sentry/node';

Sentry.init({
  integrations: [
    Sentry.googleGenAIIntegration({
      recordInputs: true,   // Record prompts/messages
      recordOutputs: true,  // Record AI responses
    }),
  ],
});

// Chat Google GenAI calls are now automatically instrumented

Copy link
Contributor

github-actions bot commented Sep 12, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.22 kB - -
@sentry/browser - with treeshaking flags 22.74 kB - -
@sentry/browser (incl. Tracing) 40.26 kB - -
@sentry/browser (incl. Tracing, Replay) 78.63 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 68.29 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 83.29 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 95.52 kB - -
@sentry/browser (incl. Feedback) 40.94 kB - -
@sentry/browser (incl. sendFeedback) 28.88 kB - -
@sentry/browser (incl. FeedbackAsync) 33.78 kB - -
@sentry/react 25.93 kB - -
@sentry/react (incl. Tracing) 42.26 kB - -
@sentry/vue 28.71 kB - -
@sentry/vue (incl. Tracing) 42.07 kB - -
@sentry/svelte 24.24 kB - -
CDN Bundle 25.75 kB - -
CDN Bundle (incl. Tracing) 40.07 kB - -
CDN Bundle (incl. Tracing, Replay) 76.3 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 81.8 kB - -
CDN Bundle - uncompressed 75.23 kB - -
CDN Bundle (incl. Tracing) - uncompressed 118.65 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 233.78 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 246.54 kB - -
@sentry/nextjs (client) 44.26 kB - -
@sentry/sveltekit (client) 40.69 kB - -
@sentry/node-core 49.96 kB -0.01% -1 B 🔽
@sentry/node 152.25 kB +0.66% +993 B 🔺
@sentry/node - without tracing 91.86 kB -0.01% -2 B 🔽
@sentry/aws-serverless 105.31 kB - -

View base workflow run

Copy link
Contributor

github-actions bot commented Sep 12, 2025

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 8,636 - 9,143 -6%
GET With Sentry 1,373 16% 1,394 -2%
GET With Sentry (error only) 5,927 69% 6,056 -2%
POST Baseline 1,181 - 1,199 -2%
POST With Sentry 549 46% 531 +3%
POST With Sentry (error only) 1,048 89% 1,065 -2%
MYSQL Baseline 3,270 - 3,318 -1%
MYSQL With Sentry 486 15% 453 +7%
MYSQL With Sentry (error only) 2,674 82% 2,670 +0%

View base workflow run

@RulaKhaled RulaKhaled force-pushed the instrument-genai-google branch from 52f49f5 to 6ef5ce0 Compare September 15, 2025 08:36
@RulaKhaled RulaKhaled marked this pull request as ready for review September 15, 2025 08:41
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@RulaKhaled RulaKhaled force-pushed the instrument-genai-google branch from 224c1a3 to 60eb2d1 Compare September 18, 2025 16:34
@RulaKhaled RulaKhaled force-pushed the instrument-genai-google branch from 60eb2d1 to 275a9a7 Compare September 18, 2025 16:40
cursor[bot]

This comment was marked as outdated.

tracesSampleRate: 1.0,
sendDefaultPii: true,
transport: loggingTransport,
integrations: [Sentry.googleGenAIIntegration()],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
integrations: [Sentry.googleGenAIIntegration()],

this is added by default, right? So no need to add this here :)

tracesSampleRate: 1.0,
sendDefaultPii: false,
transport: loggingTransport,
integrations: [Sentry.googleGenAIIntegration()],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
integrations: [Sentry.googleGenAIIntegration()],

/**
* Google GenAI Integration interface for type safety
*/
export interface GoogleGenAIIntegration {
Copy link
Member

Choose a reason for hiding this comment

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

do we actually need this, when we do not lookup the integration on the client anymore? :)

* @param wrappedConstructor The wrapped constructor function to replace the original with
* @returns void
*/
export function replaceExports(
Copy link
Member

Choose a reason for hiding this comment

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

👍 nice util!

exports => this._patch(exports),
exports => exports,
[
new InstrumentationNodeModuleFile(
Copy link
Member

Choose a reason for hiding this comment

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

let's add a comment here why this is necessary, for future reference!

const config = this.getConfig();

if (typeof Original !== 'function') {
return;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return;
return exports;

Do we not need to return the exports here if we do not patch it? 🤔 or is this handled if this is undefined and we do nothing then? just double checking 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah i think you're correct

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

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

left some small comments but overall this looks very good now 🚀

@RulaKhaled RulaKhaled merged commit 97ff2f7 into develop Sep 19, 2025
188 checks passed
@RulaKhaled RulaKhaled deleted the instrument-genai-google branch September 19, 2025 09:21
@RulaKhaled RulaKhaled restored the instrument-genai-google branch September 19, 2025 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants