|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { addHook } = require('./helpers/instrument') |
| 4 | +const shimmer = require('../../datadog-shimmer') |
| 5 | +const { channel, tracingChannel } = require('dc-polyfill') |
| 6 | + |
| 7 | +const anthropicTracingChannel = tracingChannel('apm:anthropic:request') |
| 8 | +const onStreamedChunkCh = channel('apm:anthropic:request:chunk') |
| 9 | + |
| 10 | +function wrapStreamIterator (iterator, ctx) { |
| 11 | + return function () { |
| 12 | + const itr = iterator.apply(this, arguments) |
| 13 | + shimmer.wrap(itr, 'next', next => function () { |
| 14 | + return next.apply(this, arguments) |
| 15 | + .then(res => { |
| 16 | + const { done, value: chunk } = res |
| 17 | + onStreamedChunkCh.publish({ ctx, chunk, done }) |
| 18 | + |
| 19 | + if (done) { |
| 20 | + finish(ctx) |
| 21 | + } |
| 22 | + |
| 23 | + return res |
| 24 | + }) |
| 25 | + .catch(error => { |
| 26 | + finish(ctx, null, error) |
| 27 | + throw error |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + return itr |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function wrapCreate (create) { |
| 36 | + return function () { |
| 37 | + if (!anthropicTracingChannel.start.hasSubscribers) { |
| 38 | + return create.apply(this, arguments) |
| 39 | + } |
| 40 | + |
| 41 | + const options = arguments[0] |
| 42 | + const stream = options.stream |
| 43 | + |
| 44 | + const ctx = { options, resource: 'create' } |
| 45 | + |
| 46 | + return anthropicTracingChannel.start.runStores(ctx, () => { |
| 47 | + let apiPromise |
| 48 | + try { |
| 49 | + apiPromise = create.apply(this, arguments) |
| 50 | + } catch (error) { |
| 51 | + finish(ctx, null, error) |
| 52 | + throw error |
| 53 | + } |
| 54 | + |
| 55 | + shimmer.wrap(apiPromise, 'parse', parse => function () { |
| 56 | + return parse.apply(this, arguments) |
| 57 | + .then(response => { |
| 58 | + if (stream) { |
| 59 | + shimmer.wrap(response, Symbol.asyncIterator, iterator => wrapStreamIterator(iterator, ctx)) |
| 60 | + } else { |
| 61 | + finish(ctx, response, null) |
| 62 | + } |
| 63 | + |
| 64 | + return response |
| 65 | + }).catch(error => { |
| 66 | + finish(ctx, null, error) |
| 67 | + throw error |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + anthropicTracingChannel.end.publish(ctx) |
| 72 | + |
| 73 | + return apiPromise |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function finish (ctx, result, error) { |
| 79 | + if (error) { |
| 80 | + ctx.error = error |
| 81 | + anthropicTracingChannel.error.publish(ctx) |
| 82 | + } |
| 83 | + |
| 84 | + // streamed responses are handled and set separately |
| 85 | + ctx.result ??= result |
| 86 | + |
| 87 | + anthropicTracingChannel.asyncEnd.publish(ctx) |
| 88 | +} |
| 89 | + |
| 90 | +const extensions = ['js', 'mjs'] |
| 91 | +for (const extension of extensions) { |
| 92 | + addHook({ |
| 93 | + name: '@anthropic-ai/sdk', |
| 94 | + file: `resources/messages.${extension}`, |
| 95 | + versions: ['>=0.14.0 <0.33.0'] |
| 96 | + }, exports => { |
| 97 | + const Messages = exports.Messages |
| 98 | + |
| 99 | + shimmer.wrap(Messages.prototype, 'create', wrapCreate) |
| 100 | + |
| 101 | + return exports |
| 102 | + }) |
| 103 | + |
| 104 | + addHook({ |
| 105 | + name: '@anthropic-ai/sdk', |
| 106 | + file: `resources/messages/messages.${extension}`, |
| 107 | + versions: ['>=0.33.0'] |
| 108 | + }, exports => { |
| 109 | + const Messages = exports.Messages |
| 110 | + |
| 111 | + shimmer.wrap(Messages.prototype, 'create', wrapCreate) |
| 112 | + |
| 113 | + return exports |
| 114 | + }) |
| 115 | +} |
0 commit comments