Ensure spans are properly created, attached to async operations, and reported at appropriate times to maintain accurate tracing data. Spans that are not properly managed will result in incorrect durations, missing trace data, or unreliable observability metrics.
Key practices:
context.attachSpans()
to ensure spans remain active for the duration of the async operationExample of correct span attachment:
jsg::Promise<jsg::JsRef<jsg::JsValue>> DurableObjectStorageOperations::get(jsg::Lock& js,
kj::OneOf<kj::String, kj::Array<kj::String>> keys,
jsg::Optional<GetOptions> maybeOptions) {
auto& context = IoContext::current();
auto userSpan = context.makeUserTraceSpan("durable_object_storage.get"_kjc);
// Correct: attach span to promise to maintain lifecycle
return context.attachSpans(js, getOne(js, kj::mv(s), options), kj::mv(userSpan));
}
Avoid patterns where spans immediately fall out of scope or where trace events are reported with stale timing information, as this compromises the accuracy and usefulness of observability data.
Enter the URL of a public GitHub repository