Skip to content

Commit

Permalink
refactor: adds a serialize method to LlmEvent to remove the complex o…
Browse files Browse the repository at this point in the history
…bjects before enqueuing to the custom event aggregator (newrelic#241)
  • Loading branch information
bizob2828 authored Jan 10, 2024
1 parent 0bb4ffc commit 993673e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/llm/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class LlmEvent {
const attrs = tx?.trace?.custom.get(0x01 | 0x02 | 0x04 | 0x08)
return attrs?.['llm.conversation_id']
}

/**
* Removes the complex objects from the event
* This will be called right before the event is enqueued to the custom event aggregator
*/
serialize() {
delete this.bedrockCommand
delete this.bedrockResponse
delete this.constructionParams
}
}

module.exports = LlmEvent
1 change: 1 addition & 0 deletions lib/v3/bedrock.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function shouldSkipInstrumentation(config) {
* @param {object} params.msg LLM event
*/
function recordEvent({ agent, type, msg }) {
msg.serialize()
agent.customEventAggregator.add([{ type, timestamp: Date.now() }, msg])
}

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/llm/event.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ tap.test('create creates a new instance', async (t) => {
t.equal(event['request.max_tokens'], null)
t.equal(event.error, false)
})

tap.test('serializes the event', (t) => {
const event = new LlmEvent(t.context)
event.serialize()
t.notOk(event.bedrockCommand)
t.notOk(event.bedrockResponse)
t.notOk(event.constructionParams)
t.end()
})

0 comments on commit 993673e

Please sign in to comment.