Repository navigation
Replies: 1 comment
|
import { SpanStatusCode, trace } from '@opentelemetry/api';
const tracer = trace.getTracer('my-service');
async function tracedOperation() {
return await tracer.startActiveSpan('operation', async (span) => {
try {
const result = await doAsyncWork();
return result;
} catch (error) {
span.recordException(error as Error);
span.setStatus({ code: SpanStatusCode.ERROR });
throw error;
} finally {
span.end();
}
});
}The important distinction is that The current API type returns
On Node.js, Initialize and register the SDK before loading the application modules that create async work. If using |
Uh oh!
There was an error while loading. Please reload this page.
All the examples for node.js just use a synchronous "die roller" application.
Most real applications are based on async functions and promises, and the "scoped" callback-based tracer.startActiveSpan() function will end the span too early.
There are some discussion about how (synchronous) callbacks are bad style in Node.js (which is true within the context of promises based functions) but the answers are all missing the point, saying "functions aren't bad style."
So, are there really no async-based helpers in the opentelemetry SDK?
What is the recommended way to trace async functions?
All reactions