You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/12/02 13:51:37 UTC

[skywalking-nodejs] branch master updated: withSpanAwait(), HttpPlugin errorer on abort/error (#4)

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-nodejs.git


The following commit(s) were added to refs/heads/master by this push:
     new aae33f5  withSpanAwait(), HttpPlugin errorer on abort/error (#4)
aae33f5 is described below

commit aae33f532ecd63dc1acee776b1008909d96e1d20
Author: Tomasz Pytel <to...@gmail.com>
AuthorDate: Wed Dec 2 10:51:27 2020 -0300

    withSpanAwait(), HttpPlugin errorer on abort/error (#4)
---
 src/plugins/HttpPlugin.ts           |  6 +++---
 src/trace/context/ContextManager.ts | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

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 @@ class HttpPlugin implements SwPlugin {
         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 @@ class ContextManager {
     }
   }
 
+  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();