Skip to content

Commit 1662072

Browse files
authored
chore(llmobs): add span size telemetry metrics (#5468)
* Add raw span size metric * Add processed span event size metrics * Refactor * Remove unnecessary check
1 parent 288d38b commit 1662072

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

packages/dd-trace/src/llmobs/telemetry.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ const LLMObsTagger = require('./tagger')
1818

1919
const llmobsMetrics = telemetryMetrics.manager.namespace('mlobs')
2020

21+
function extractIntegrationFromTags (tags) {
22+
if (!Array.isArray(tags)) return null
23+
const integrationTag = tags.find(tag => tag.startsWith('integration:'))
24+
if (!integrationTag) return null
25+
return integrationTag.split(':')[1] || null
26+
}
27+
28+
function extractTagsFromSpanEvent (event) {
29+
const spanKind = event.meta?.['span.kind'] || ''
30+
const integration = extractIntegrationFromTags(event.tags)
31+
const error = event.status === 'error'
32+
const autoinstrumented = integration != null
33+
34+
return {
35+
span_kind: spanKind,
36+
autoinstrumented: Number(autoinstrumented),
37+
error: error ? 1 : 0,
38+
integration: integration || 'N/A'
39+
}
40+
}
41+
2142
function incrementLLMObsSpanStartCount (tags, value = 1) {
2243
llmobsMetrics.count('span.start', tags).inc(value)
2344
}
@@ -53,7 +74,20 @@ function incrementLLMObsSpanFinishedCount (span, value = 1) {
5374
llmobsMetrics.count('span.finished', tags).inc(value)
5475
}
5576

77+
function recordLLMObsRawSpanSize (event, rawEventSize) {
78+
const tags = extractTagsFromSpanEvent(event)
79+
llmobsMetrics.distribution('span.raw_size', tags).track(rawEventSize)
80+
}
81+
82+
function recordLLMObsSpanSize (event, eventSize, shouldTruncate) {
83+
const tags = extractTagsFromSpanEvent(event)
84+
tags.truncated = Number(shouldTruncate)
85+
llmobsMetrics.distribution('span.size', tags).track(eventSize)
86+
}
87+
5688
module.exports = {
5789
incrementLLMObsSpanStartCount,
58-
incrementLLMObsSpanFinishedCount
90+
incrementLLMObsSpanFinishedCount,
91+
recordLLMObsRawSpanSize,
92+
recordLLMObsSpanSize
5993
}

packages/dd-trace/src/llmobs/writers/spans/base.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { EVP_EVENT_SIZE_LIMIT, EVP_PAYLOAD_SIZE_LIMIT } = require('../../constant
44
const { DROPPED_VALUE_TEXT } = require('../../constants/text')
55
const { DROPPED_IO_COLLECTION_ERROR } = require('../../constants/tags')
66
const BaseWriter = require('../base')
7+
const telemetry = require('../../telemetry')
78
const logger = require('../../../log')
89

910
const tracerVersion = require('../../../../../../package.json').version
@@ -18,11 +19,19 @@ class LLMObsSpanWriter extends BaseWriter {
1819

1920
append (event) {
2021
const eventSizeBytes = Buffer.from(JSON.stringify(event)).byteLength
21-
if (eventSizeBytes > EVP_EVENT_SIZE_LIMIT) {
22+
telemetry.recordLLMObsRawSpanSize(event, eventSizeBytes)
23+
24+
const shouldTruncate = eventSizeBytes > EVP_EVENT_SIZE_LIMIT
25+
let processedEventSizeBytes = eventSizeBytes
26+
27+
if (shouldTruncate) {
2228
logger.warn(`Dropping event input/output because its size (${eventSizeBytes}) exceeds the 1MB event size limit`)
2329
event = this._truncateSpanEvent(event)
30+
processedEventSizeBytes = Buffer.from(JSON.stringify(event)).byteLength
2431
}
2532

33+
telemetry.recordLLMObsSpanSize(event, processedEventSizeBytes, shouldTruncate)
34+
2635
if (this._bufferSize + eventSizeBytes > EVP_PAYLOAD_SIZE_LIMIT) {
2736
logger.debug('Flusing queue because queing next event will exceed EvP payload limit')
2837
this.flush()

0 commit comments

Comments
 (0)