withSpanAwait(), HttpPlugin errorer on abort/error (#4)
diff --git a/src/plugins/HttpPlugin.ts b/src/plugins/HttpPlugin.ts
index 87c0891..3b5b171 100644
--- a/src/plugins/HttpPlugin.ts
+++ b/src/plugins/HttpPlugin.ts
@@ -80,10 +80,10 @@
span.async();
let stopped = 0; // compensating if request aborted right after creation 'close' is not emitted
- const stopIfNotStopped = () => !stopped++ ? span.stop() : null;
- request.on('abort', stopIfNotStopped); // make sure we stop only once
+ const stopIfNotStopped = () => !stopped++ ? span.stop() : null; // make sure we stop only once
request.on('close', stopIfNotStopped);
- request.on('error', stopIfNotStopped);
+ request.on('abort', () => (span.errored = true, stopIfNotStopped()));
+ request.on('error', (err) => (span.error(err), stopIfNotStopped()));
request.prependListener('response', (res) => {
span.resync();
diff --git a/src/trace/context/ContextManager.ts b/src/trace/context/ContextManager.ts
index b22ab60..7f373a6 100644
--- a/src/trace/context/ContextManager.ts
+++ b/src/trace/context/ContextManager.ts
@@ -77,6 +77,20 @@
}
}
+ async withSpanAwait(span: Span, callback: (...args: any[]) => any, ...args: any[]): Promise<any> {
+ if(!span.startTime)
+ span.start();
+
+ try {
+ return await callback(span, ...args);
+ } catch (e) {
+ span.error(e);
+ throw e;
+ } finally {
+ span.stop();
+ }
+ }
+
withSpanNoStop(span: Span, callback: (...args: any[]) => any, ...args: any[]): any {
if(!span.startTime)
span.start();