I am making a wrapper over opentelemetry JS. I want that I don't need to pass the parent span in the the methods, just call the function and declare the wrapper library methodlibrary.createSpan("span name") and end when the method ends with just simple call library.endSpan()
So I am using this code to do provide this functionality but failing.
createChildSpan(name, tags = {}) {
if (typeof tags != 'object') {
console.log("Span not generated, tags should be of object type");
return;
}
if(typeof name != 'string'){
console.log("Span not generated, name should be of string type");
return;
}
// Get the tracer
const tracer = opentelemetry.trace.getTracerProvider().getTracer("serviceName");
// Start the span
const span = tracer.startSpan(name);
// Set it as active context
opentelemetry.context.with(opentelemetry.trace.setSpan(opentelemetry.context.active(), span), () => {
const currentSpan = opentelemetry.trace.getSpan(opentelemetry.context.active());
if(currentSpan){
currentSpan.setAttribute("check", "data");
}
});
return new spanClass(span);
}
My problem is spans are making child parent connection
I have a functions and methods in service
app.get('/test', async (req, res) => {
add1();
return res.status(200).send('OK test');
});
const add1 = () => {
// start the span
const span = tracer.createChildSpan("this is add");
//Operation to perform
multi();
// end the span
span.end();
}
const multi = () => {
const span = tracer.createChildSpan("this is mutli");
span.end();
}
And my result on lightstep is spans
I want that multi is the child of the add1 but it's not like this. I think it's because of it's unable to update the current context. May be I am doing something wrong.