105 lines
3.0 KiB
JavaScript

const traces = axios.create({
baseURL: opentelemetry_settings.baseUrl,
timeout: 16000,
})
return {
async startSpan(name) {
try {
if (!conversationData.spans) {
conversationData.spans = []
}
const spanName = name ? name : 'default'
const span = {
resourceSpans: [
{
resource: {
attributes: [
{
key: 'service.name',
value: {
stringValue: 'ivastudio.verint.live',
},
},
],
},
scopeSpans: [
{
scope: {
name: recognizedObject.conversationId,
version: '1.0.0',
attributes: [
{
key: 'my.scope.attribute',
value: {
stringValue: 'some scope attribute',
},
},
],
},
spans: [
{
// traceId: recognizedObject.req.headers['x-b3-traceid'],
traceId: recognizedObject.conversationId.replace(/-/gi, ''),
spanId: recognizedObject.req.headers['x-b3-spanid'],
// parentSpanId: recognizedObject.req.headers['x-b3-parentspanid'],
name: spanName,
startTimeUnixNano: '' + Date.now() * 1000000,
kind: 2,
attributes: [
{
key: 'conversationId',
value: {
stringValue: recognizedObject.conversationId,
},
},
{
key: 'workspaceId',
value: {
stringValue: recognizedObject.workspaceId,
},
},
{
key: 'input',
value: {
stringValue: recognizedObject.input,
},
},
],
},
],
},
],
},
],
}
conversationData.spans.push(span)
return span
} catch (e) {
throw e
}
},
async endSpan() {
try {
const data = conversationData.spans.pop()
data.resourceSpans[0].scopeSpans[0].spans[0].endTimeUnixNano = '' + Date.now() * 1000000
data.resourceSpans[0].scopeSpans[0].spans[0].attributes.push({
key: 'answers',
value: {
stringValue: JSON.stringify(recognizedObject.answers),
},
})
const response = await traces.post(`/v1/traces`, data, {
headers: {
'Content-Type': 'application/json',
},
})
return response.data
} catch (e) {
throw e
}
},
}