Updated readme to match latest changes.

Not writes process/resource and additional attribute information
This commit is contained in:
2026-01-10 13:53:53 -06:00
parent ce23405757
commit 7371260442
2 changed files with 171 additions and 153 deletions

View File

@@ -1,57 +1,67 @@
# Open Telemetery Example # Open Telemetry Example
Writes Telemetry Tracing information to the endpoint provided using the [OLTP JSON over HTTP specification](https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#json-protobuf-encoding)
## Example Usage ## Example Usage
Use the _code block_ widget to start and end spans. Spans can be nested to form a stack, ending a span will always end the last span added to the stack. Use the _code block_ widget to start and end spans.
### Visualization Spans can be nested to form a stack, with the parent span being the most recent
active span added to the stack. You can have multiple spans using the same name,
and ending a span by name will always end the most recent span.
Spans will only be written to the opentelemetry endpoint once the _endSpan_
method is called.
## Visualization
For example, using Jaeger UI: For example, using Jaeger UI:
![Tracing Screenshot](screenshots/IVA%20Tracing.png) ![Tracing Screenshot](screenshots/IVA%20Tracing.png)
## Code Snippets
### Start Span ### Start Span
```javascript ```javascript
(async () => { (async () => {
opentelemetry.startSpan("Global Flow") opentelemetry.startSpan("Global Flow");
})() })()
.catch((error) => { .catch((error) => {
console.error(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 () => {
opentelemetry.endSpan("Global Flow") opentelemetry.endSpan("Global Flow");
})() })()
.catch((error) => { .catch((error) => {
console.error(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

@@ -9,8 +9,8 @@ const {
function generateIdHex(numBytes = 8) { function generateIdHex(numBytes = 8) {
const bytes = crypto.getRandomValues(new Uint8Array(numBytes)); const bytes = crypto.getRandomValues(new Uint8Array(numBytes));
return Array.from(bytes) return Array.from(bytes)
.map(b => b.toString(16).padStart(2, '0')) .map((b) => b.toString(16).padStart(2, "0"))
.join(''); .join("");
} }
return { return {
@@ -21,7 +21,7 @@ return {
// Initialize telemetry object if it doesn't exist // Initialize telemetry object if it doesn't exist
if ("telemetry" in r == false) { if ("telemetry" in r == false) {
r.telemetry = []; r.telemetry = { spans: [] };
if (opentelemetry_settings.trace == "conversation") { if (opentelemetry_settings.trace == "conversation") {
r.telemetry.traceId = r.conversationId.replace(/-/gi, ""); r.telemetry.traceId = r.conversationId.replace(/-/gi, "");
} else { } else {
@@ -42,30 +42,6 @@ return {
stringValue: "ivastudio.verint.live", stringValue: "ivastudio.verint.live",
}, },
}, },
],
},
scopeSpans: [
{
scope: {
name: r.conversationId,
version: "1.0.0",
attributes: [
{
key: "my.scope.attribute",
value: {
stringValue: "some scope attribute",
},
},
],
},
spans: [
{
traceId: r.telemetry.traceId,
spanId: generateIdHex(),
name: name,
startTimeUnixNano: "" + Date.now() * 1000000,
kind: 2,
attributes: [
{ {
key: "conversationId", key: "conversationId",
value: { value: {
@@ -78,12 +54,42 @@ return {
stringValue: r.workspaceId, stringValue: r.workspaceId,
}, },
}, },
],
},
scopeSpans: [
{
spans: [
{
traceId: r.telemetry.traceId,
spanId: generateIdHex(),
name: name,
startTimeUnixNano: "" + Date.now() * 1000000,
kind: 2,
attributes: [
{ {
key: "input", key: "input",
value: { value: {
stringValue: r.input, stringValue: r.input,
}, },
}, },
{
key: "channel",
value: {
stringValue: (
req.body?.metadata?.channel ??
req.body?.channel ??
"web"
).toLowerCase(),
},
},
{
key: "sessionId",
value: {
stringValue: (
req.body?.metadata?.sessionId ?? "undefined"
),
},
},
], ],
}, },
], ],
@@ -92,7 +98,7 @@ return {
}, },
], ],
}; };
r.telemetry.unshift(span); r.telemetry.spans.unshift(span);
return span; return span;
} catch (e) { } catch (e) {
throw e; throw e;
@@ -109,18 +115,20 @@ return {
} }
try { try {
const spanIndex = r.telemetry.findIndex((span) => span.name === name); const spanIndex = r.telemetry.spans.findIndex(
(span) => span.name === name
);
if (spanIndex == -1) { if (spanIndex == -1) {
// nothing to end // nothing to end
return; return;
} }
const span = r.telemetry.splice(spanIndex, 1)[0]; const span = r.telemetry.spans.splice(spanIndex, 1)[0];
if (r.telemetry.length >= 1) { if (r.telemetry.spans.length >= 1) {
// Assume span now at index is the parent // Assume span now at index is the parent
const parentSpan = r.telemetry[spanIndex]; const parentSpan = r.telemetry.spans[spanIndex];
if (parentSpan) { if (parentSpan) {
span.resourceSpans[0].scopeSpans[0].spans[0].parentSpanId = span.resourceSpans[0].scopeSpans[0].spans[0].parentSpanId =
parentSpan.resourceSpans[0].scopeSpans[0].spans[0].spanId; parentSpan.resourceSpans[0].scopeSpans[0].spans[0].spanId;