|
| 1 | +/* |
| 2 | + * Copyright 2025 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | +const LangchainSubscriber = require('./base') |
| 8 | +const { |
| 9 | + AI: { LANGCHAIN } |
| 10 | +} = require('../../metrics/names') |
| 11 | +const { langchainRunId } = require('../../symbols') |
| 12 | +const { LangChainTool } = require('../../llm-events/langchain') |
| 13 | +const LlmErrorMessage = require('../../llm-events/error-message') |
| 14 | +const { DESTINATIONS } = require('../../config/attribute-filter') |
| 15 | + |
| 16 | +class LangchainToolSubscriber extends LangchainSubscriber { |
| 17 | + constructor({ agent, logger }) { |
| 18 | + super({ agent, logger, channelName: 'nr_call' }) |
| 19 | + this.events = ['asyncEnd'] |
| 20 | + } |
| 21 | + |
| 22 | + handler(data, ctx) { |
| 23 | + if (!this.enabled) { |
| 24 | + // We need this check inside the wrapper because it is possible for monitoring |
| 25 | + // to be disabled at the account level. In such a case, the value is set |
| 26 | + // after the instrumentation has been initialized. |
| 27 | + return ctx |
| 28 | + } |
| 29 | + const tool = data?.self |
| 30 | + |
| 31 | + const segment = this.agent.tracer.createSegment({ |
| 32 | + name: `${LANGCHAIN.TOOL}/${tool?.name}`, |
| 33 | + parent: ctx.segment, |
| 34 | + transaction: ctx.transaction |
| 35 | + }) |
| 36 | + return ctx.enterSegment({ segment }) |
| 37 | + } |
| 38 | + |
| 39 | + asyncEnd(data) { |
| 40 | + const { moduleVersion: pkgVersion, result, error: err } = data |
| 41 | + const { name, metadata: instanceMeta, description, tags: instanceTags } = data?.self |
| 42 | + const request = data?.arguments?.[0] |
| 43 | + const params = data?.arguments?.[1] || {} |
| 44 | + const { metadata: paramsMeta, tags: paramsTags } = params |
| 45 | + const metadata = this.mergeMetadata(instanceMeta, paramsMeta) |
| 46 | + const tags = this.mergeTags(instanceTags, paramsTags) |
| 47 | + |
| 48 | + const { agent } = this |
| 49 | + const ctx = agent.tracer.getContext() |
| 50 | + const { segment, transaction } = ctx |
| 51 | + if (transaction?.isActive() !== true) { |
| 52 | + return |
| 53 | + } |
| 54 | + segment.end() |
| 55 | + |
| 56 | + if (!this.enabled) { |
| 57 | + // We need this check inside the wrapper because it is possible for monitoring |
| 58 | + // to be disabled at the account level. In such a case, the value is set |
| 59 | + // after the instrumentation has been initialized. |
| 60 | + this.logger.debug('skipping sending of ai data') |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + const toolEvent = new LangChainTool({ |
| 65 | + agent, |
| 66 | + description, |
| 67 | + name, |
| 68 | + runId: segment[langchainRunId], |
| 69 | + metadata, |
| 70 | + transaction, |
| 71 | + tags, |
| 72 | + input: request?.input, |
| 73 | + output: result, |
| 74 | + segment, |
| 75 | + error: err != null |
| 76 | + }) |
| 77 | + this.recordEvent({ type: 'LlmTool', pkgVersion, msg: toolEvent }) |
| 78 | + |
| 79 | + if (err) { |
| 80 | + agent.errors.add( |
| 81 | + transaction, |
| 82 | + err, |
| 83 | + new LlmErrorMessage({ |
| 84 | + response: {}, |
| 85 | + cause: err, |
| 86 | + tool: toolEvent |
| 87 | + }) |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_EVENT, 'llm', true) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +module.exports = LangchainToolSubscriber |
0 commit comments