forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.tap.js
170 lines (148 loc) · 5.08 KB
/
tools.tap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const tap = require('tap')
const helper = require('../../lib/agent_helper')
// load the assertSegments assertion
require('../../lib/metrics_helper')
const { version: pkgVersion } = require('@langchain/core/package.json')
const config = {
ai_monitoring: {
enabled: true
}
}
const baseUrl = 'http://httpbin.org'
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
tap.test('Langchain instrumentation - tools', (t) => {
t.beforeEach((t) => {
t.context.agent = helper.instrumentMockedAgent(config)
const TestTool = require('./helpers/custom-tool')
const tool = new TestTool({
baseUrl
})
t.context.tool = tool
t.context.input = 'langchain'
})
t.afterEach((t) => {
helper.unloadAgent(t.context.agent)
// bust the require-cache so it can re-instrument
Object.keys(require.cache).forEach((key) => {
if (key.includes('@langchain/core') || key.endsWith('/helpers/custom-tool.js')) {
delete require.cache[key]
}
})
})
t.test('should create span on successful tools create', (t) => {
const { agent, tool, input } = t.context
helper.runInTransaction(agent, async (tx) => {
const result = await tool.call(input)
t.ok(result)
t.assertSegments(tx.trace.root, ['Llm/tool/Langchain/node-agent-test-tool'], { exact: false })
tx.end()
t.end()
})
})
t.test('should increment tracking metric for each tool event', (t) => {
const { tool, agent, input } = t.context
helper.runInTransaction(agent, async (tx) => {
await tool.call(input)
const metrics = agent.metrics.getOrCreateMetric(
`Supportability/Nodejs/ML/Langchain/${pkgVersion}`
)
t.equal(metrics.callCount > 0, true)
tx.end()
t.end()
})
})
t.test('should create LlmTool event for every tool.call', (t) => {
const { agent, tool, input } = t.context
helper.runInTransaction(agent, async (tx) => {
tool.metadata = { key: 'instance-value', hello: 'world' }
tool.tags = ['tag1', 'tag2']
await tool.call(input, { metadata: { key: 'value' }, tags: ['tag2', 'tag3'] })
const events = agent.customEventAggregator.events.toArray()
t.equal(events.length, 1, 'should create a LlmTool event')
const [[{ type }, toolEvent]] = events
t.equal(type, 'LlmTool')
t.match(toolEvent, {
'id': /[a-f0-9]{36}/,
'appName': 'New Relic for Node.js tests',
'span_id': tx.trace.root.children[0].id,
'trace_id': tx.traceId,
'ingest_source': 'Node',
'vendor': 'langchain',
'metadata.key': 'value',
'metadata.hello': 'world',
'tags': 'tag1,tag2,tag3',
input,
'output': tool.fakeData[input],
'name': tool.name,
'description': tool.description,
'duration': tx.trace.root.children[0].getDurationInMillis(),
'run_id': undefined
})
tx.end()
t.end()
})
})
t.test('should add runId when a callback handler exists', (t) => {
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
let runId
const cbHandler = BaseCallbackHandler.fromMethods({
handleToolStart(...args) {
runId = args?.[2]
}
})
const { agent, tool, input } = t.context
tool.callbacks = [cbHandler]
helper.runInTransaction(agent, async (tx) => {
await tool.call(input)
const events = agent.customEventAggregator.events.toArray()
t.equal(events.length, 1, 'should create a LlmTool event')
const [[, toolEvent]] = events
t.equal(toolEvent.run_id, runId)
tx.end()
t.end()
})
})
t.test('should not create llm tool events when not in a transaction', async (t) => {
const { tool, agent, input } = t.context
await tool.call(input)
const events = agent.customEventAggregator.events.toArray()
t.equal(events.length, 0, 'should not create llm events')
})
t.test('should add llm attribute to transaction', (t) => {
const { tool, agent, input } = t.context
helper.runInTransaction(agent, async (tx) => {
await tool.call(input)
const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_EVENT)
t.equal(attributes.llm, true)
tx.end()
t.end()
})
})
t.test('should capture error events', (t) => {
const { agent, tool } = t.context
helper.runInTransaction(agent, async (tx) => {
try {
await tool.call('bad input')
} catch (error) {
t.ok(error)
}
const events = agent.customEventAggregator.events.toArray()
t.equal(events.length, 1)
const toolEvent = events.find((e) => e[0].type === 'LlmTool')?.[1]
t.equal(toolEvent.error, true)
const exceptions = tx.exceptions
t.equal(exceptions.length, 1)
const str = Object.prototype.toString.call(exceptions[0].customAttributes)
t.equal(str, '[object LlmErrorMessage]')
t.equal(exceptions[0].customAttributes.tool_id, toolEvent.id)
tx.end()
t.end()
})
})
t.end()
})