forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.js
86 lines (77 loc) · 2.4 KB
/
event.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
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const BaseEvent = require('../event')
const { makeId } = require('../../util/hashes')
const { isSimpleObject } = require('../../util/objects')
/**
* @typedef {object} LangChainEventParams
* @property {object} agent A New Relic agent instance.
* @property {object} segment A New Relic segment instance.
* @property {string} runId The identifier LangChain has assigned to the "run."
* @property {Object<string, string>} metadata The metadata, if any, associated with the
* LangChain run.
* @property {boolean|undefined} [error] Indicates if an error took place during
* the conversation that resulted in the event.
* @property {boolean|undefined} [virtual=true] Indicates that this event is a
* LangChain specific event (`true`). LangChain is not itself an LLM, but an
* interface to many LLMs. Any LLMs LangChain interacts with that we have
* instrumented will have their own traces that are not "virtual."
*/
/**
* @type {LangChainEventParams}
*/
const defaultParams = {
agent: {},
segment: {
transaction: {}
},
tags: [],
runId: '',
metadata: {},
error: undefined,
virtual: undefined
}
/**
* Baseline object representing an event in a LangChain conversation.
*/
class LangChainEvent extends BaseEvent {
id = makeId(36)
appName
span_id
request_id
trace_id
ingest_source = 'Node'
vendor = 'langchain'
virtual_llm = true
constructor(params = defaultParams) {
params = Object.assign({}, defaultParams, params)
super(params)
const { agent, segment } = params
this.appName = agent.config.applications()[0]
this.span_id = segment?.id
this.request_id = params.runId
this.trace_id = segment?.transaction?.traceId
this.langchainMeta = params.metadata
this.metadata = agent
this.tags = Array.isArray(params.tags) ? params.tags.join(',') : params.tags
this.error = params.error ?? null
if (params.virtual !== undefined) {
if (params.virtual !== true && params.virtual !== false) {
throw Error('params.virtual must be a primitive boolean')
}
this.virtual_llm = params.virtual
}
}
set langchainMeta(value) {
if (isSimpleObject(value) === false) {
return
}
for (const [key, val] of Object.entries(value)) {
this[`metadata.${key}`] = val
}
}
}
module.exports = LangChainEvent