|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { addHook } = require('./helpers/instrument') |
| 4 | +const shimmer = require('../../datadog-shimmer') |
| 5 | +const tracingChannel = require('dc-polyfill').tracingChannel |
| 6 | +const channel = require('dc-polyfill').channel |
| 7 | + |
| 8 | +const genaiTracingChannel = tracingChannel('apm:google:genai:request') |
| 9 | +const onStreamedChunkCh = channel('apm:google:genai:request:chunk') |
| 10 | + |
| 11 | +function wrapGenerateContent (method) { |
| 12 | + return function wrappedGenerateContent (original) { |
| 13 | + return function (...args) { |
| 14 | + if (!genaiTracingChannel.start.hasSubscribers) { |
| 15 | + return original.apply(this, args) |
| 16 | + } |
| 17 | + |
| 18 | + const normalizedName = normalizeMethodName(method) |
| 19 | + |
| 20 | + const ctx = { args, methodName: normalizedName } |
| 21 | + |
| 22 | + return genaiTracingChannel.start.runStores(ctx, () => { |
| 23 | + let result |
| 24 | + try { |
| 25 | + result = original.apply(this, arguments) |
| 26 | + } catch (error) { |
| 27 | + finish(ctx, null, error) |
| 28 | + throw error |
| 29 | + } finally { |
| 30 | + genaiTracingChannel.end.publish(ctx) |
| 31 | + } |
| 32 | + return result.then(response => { |
| 33 | + if (response[Symbol.asyncIterator]) { |
| 34 | + shimmer.wrap(response, Symbol.asyncIterator, iterator => wrapStreamIterator(iterator, ctx)) |
| 35 | + } else { |
| 36 | + finish(ctx, response, null) |
| 37 | + } |
| 38 | + return response |
| 39 | + }).catch(error => { |
| 40 | + finish(ctx, null, error) |
| 41 | + throw error |
| 42 | + }) |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function wrapStreamIterator (iterator, ctx) { |
| 49 | + return function () { |
| 50 | + const itr = iterator.apply(this, arguments) |
| 51 | + shimmer.wrap(itr, 'next', next => function () { |
| 52 | + return next.apply(this, arguments) |
| 53 | + .then(res => { |
| 54 | + const { done, value: chunk } = res |
| 55 | + onStreamedChunkCh.publish({ ctx, chunk, done }) |
| 56 | + |
| 57 | + if (done) { |
| 58 | + finish(ctx) |
| 59 | + } |
| 60 | + |
| 61 | + return res |
| 62 | + }) |
| 63 | + .catch(error => { |
| 64 | + finish(ctx, null, error) |
| 65 | + throw error |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + return itr |
| 70 | + } |
| 71 | +} |
| 72 | +function finish (ctx, result, error) { |
| 73 | + if (error) { |
| 74 | + ctx.error = error |
| 75 | + genaiTracingChannel.error.publish(ctx) |
| 76 | + } |
| 77 | + |
| 78 | + // streamed responses are handled and set separately |
| 79 | + ctx.result ??= result |
| 80 | + |
| 81 | + genaiTracingChannel.asyncEnd.publish(ctx) |
| 82 | +} |
| 83 | +// Hook the main package entry point |
| 84 | +addHook({ |
| 85 | + name: '@google/genai', |
| 86 | + versions: ['>=1.19.0'] |
| 87 | +}, exports => { |
| 88 | + // Wrap GoogleGenAI to intercept when it creates Models instances |
| 89 | + if (!exports.GoogleGenAI) return exports |
| 90 | + |
| 91 | + shimmer.wrap(exports, 'GoogleGenAI', GoogleGenAI => { |
| 92 | + return class extends GoogleGenAI { |
| 93 | + constructor (...args) { |
| 94 | + super(...args) |
| 95 | + |
| 96 | + // We are patching the instance instead of the prototype because when it is compiled from |
| 97 | + // typescript, the models property is not available on the prototype. |
| 98 | + if (this.models) { |
| 99 | + if (this.models.generateContent) { |
| 100 | + shimmer.wrap(this.models, 'generateContent', wrapGenerateContent('generateContent')) |
| 101 | + } |
| 102 | + if (this.models.generateContentStream) { |
| 103 | + shimmer.wrap(this.models, 'generateContentStream', wrapGenerateContent('generateContentStream')) |
| 104 | + } |
| 105 | + if (this.models.embedContent) { |
| 106 | + shimmer.wrap(this.models, 'embedContent', wrapGenerateContent('embedContent')) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + return exports |
| 113 | +}) |
| 114 | + |
| 115 | +function normalizeMethodName (methodName) { |
| 116 | + // Convert camelCase to snake_case and add Models prefix |
| 117 | + return 'Models.' + methodName |
| 118 | + .replaceAll(/([a-z0-9])([A-Z])/g, '$1_$2') |
| 119 | + .toLowerCase() |
| 120 | +} |
0 commit comments