Better heirarchy and option to do conversation tracing vs transaction.

This commit is contained in:
2026-01-09 23:11:07 -06:00
parent a9a8b14c73
commit 6b86c348fe
2 changed files with 158 additions and 112 deletions

View File

@@ -8,45 +8,44 @@ Use the _code block_ widget to start and end spans. Spans can be nested to form
```javascript ```javascript
(async () => { (async () => {
await latencySpan().endSpan(recognizedObject.conversationId); opentelemetry.startSpan("Global Flow")
const span = await opentelemetry().startSpan("Global Flow");
console.log(span);
})() })()
.catch((error) => { .catch((error) => {
console.log(error.message); console.error(error.message)
recognizedObject.answers.push(""); recognizedObject.answers.push('')
recognizedObject.errorInfo = { recognizedObject.errorInfo = {
...recognizedObject.errorInfo, ...recognizedObject.errorInfo,
label: { label: {
data: error.toJSON ? error.toJSON() : {}, data: error.toJSON ? error.toJSON() : {},
message: error.message, message: error.message,
}, },
}; }
}) })
.finally(() => { .finally(() => {
next(); next()
}); })
``` ```
### End Span ### End Span
```javascript ```javascript
(async () => { (async () => {
await opentelemetry().endSpan(); opentelemetry.endSpan("Global Flow")
})() })()
.catch((error) => { .catch((error) => {
console.log(error.message); console.error(error.message)
recognizedObject.answers.push(""); recognizedObject.answers.push('')
recognizedObject.errorInfo = { recognizedObject.errorInfo = {
...recognizedObject.errorInfo, ...recognizedObject.errorInfo,
label: { label: {
data: error.toJSON ? error.toJSON() : {}, data: error.toJSON ? error.toJSON() : {},
message: error.message, message: error.message,
}, },
}; }
}) })
.finally(() => { .finally(() => {
next(); next()
}); })
``` ```

View File

@@ -1,24 +1,45 @@
const traces = axios.create({ const {
recognizedObject: r = {},
traces = axios.create({
baseURL: opentelemetry_settings.baseUrl, baseURL: opentelemetry_settings.baseUrl,
timeout: 16000, timeout: 16000,
}) }),
} = this;
function generateIdHex(numBytes = 8) {
const bytes = crypto.getRandomValues(new Uint8Array(numBytes));
return Array.from(bytes)
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}
return { return {
async startSpan(name) { async startSpan(name) {
try { if (name == null) {
if (!conversationData.spans) { throw new Error("Span name is required");
conversationData.spans = []
} }
const spanName = name ? name : 'default'
// Initialize telemetry object if it doesn't exist
if ("telemetry" in r == false) {
r.telemetry = [];
if (opentelemetry_settings.trace == "conversation") {
r.telemetry.traceId = r.conversationId.replace(/-/gi, "");
} else {
r.telemetry.traceId = generateIdHex(16);
}
}
try {
const span = { const span = {
name: name,
resourceSpans: [ resourceSpans: [
{ {
resource: { resource: {
attributes: [ attributes: [
{ {
key: 'service.name', key: "service.name",
value: { value: {
stringValue: 'ivastudio.verint.live', stringValue: "ivastudio.verint.live",
}, },
}, },
], ],
@@ -26,43 +47,41 @@ return {
scopeSpans: [ scopeSpans: [
{ {
scope: { scope: {
name: recognizedObject.conversationId, name: r.conversationId,
version: '1.0.0', version: "1.0.0",
attributes: [ attributes: [
{ {
key: 'my.scope.attribute', key: "my.scope.attribute",
value: { value: {
stringValue: 'some scope attribute', stringValue: "some scope attribute",
}, },
}, },
], ],
}, },
spans: [ spans: [
{ {
// traceId: recognizedObject.req.headers['x-b3-traceid'], traceId: r.telemetry.traceId,
traceId: recognizedObject.conversationId.replace(/-/gi, ''), spanId: generateIdHex(),
spanId: recognizedObject.req.headers['x-b3-spanid'], name: name,
// parentSpanId: recognizedObject.req.headers['x-b3-parentspanid'], startTimeUnixNano: "" + Date.now() * 1000000,
name: spanName,
startTimeUnixNano: '' + Date.now() * 1000000,
kind: 2, kind: 2,
attributes: [ attributes: [
{ {
key: 'conversationId', key: "conversationId",
value: { value: {
stringValue: recognizedObject.conversationId, stringValue: r.conversationId,
}, },
}, },
{ {
key: 'workspaceId', key: "workspaceId",
value: { value: {
stringValue: recognizedObject.workspaceId, stringValue: r.workspaceId,
}, },
}, },
{ {
key: 'input', key: "input",
value: { value: {
stringValue: recognizedObject.input, stringValue: r.input,
}, },
}, },
], ],
@@ -72,34 +91,62 @@ return {
], ],
}, },
], ],
} };
conversationData.spans.push(span) r.telemetry.unshift(span);
return span return span;
} catch (e) { } catch (e) {
throw e throw e;
} }
}, },
async endSpan() { async endSpan(name) {
if (name == null) {
throw new Error("Span name is required");
}
if ("telemetry" in r == false) {
// nothing to end
return;
}
try { try {
const data = conversationData.spans.pop() const spanIndex = r.telemetry.findIndex((span) => span.name === name);
data.resourceSpans[0].scopeSpans[0].spans[0].endTimeUnixNano = '' + Date.now() * 1000000 if (spanIndex == -1) {
// nothing to end
return;
}
data.resourceSpans[0].scopeSpans[0].spans[0].attributes.push({ const span = r.telemetry.splice(spanIndex, 1)[0];
key: 'answers',
if (r.telemetry.length >= 1) {
// Assume span now at index is the parent
const parentSpan = r.telemetry[spanIndex];
if (parentSpan) {
span.resourceSpans[0].scopeSpans[0].spans[0].parentSpanId =
parentSpan.resourceSpans[0].scopeSpans[0].spans[0].spanId;
} else {
console.debug(name + " has no parentSpan");
}
}
span.resourceSpans[0].scopeSpans[0].spans[0].endTimeUnixNano =
"" + Date.now() * 1000000;
span.resourceSpans[0].scopeSpans[0].spans[0].attributes.push({
key: "answers",
value: { value: {
stringValue: JSON.stringify(recognizedObject.answers), stringValue: JSON.stringify(r.answers),
}, },
}) });
const response = await traces.post(`/v1/traces`, data, { const response = await traces.post(`/v1/traces`, span, {
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}) });
return response.data return response.data;
} catch (e) { } catch (e) {
throw e throw e;
} }
}, },
} };