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 2021/08/11 12:13:16 UTC

[skywalking-nodejs] branch master updated: Add an API to access the trace id. (#60)

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 e755659  Add an API to access the trace id. (#60)
e755659 is described below

commit e755659c7f308d3b5589619778c8360308cb14f8
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Wed Aug 11 20:13:12 2021 +0800

    Add an API to access the trace id. (#60)
    
    Closes https://github.com/apache/skywalking/issues/7328
---
 src/trace/context/Context.ts      |  8 +++++++-
 src/trace/context/DummyContext.ts |  9 ++++++++-
 src/trace/context/SpanContext.ts  | 17 +++++++++++++----
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/trace/context/Context.ts b/src/trace/context/Context.ts
index 9928895..c8ab2d4 100644
--- a/src/trace/context/Context.ts
+++ b/src/trace/context/Context.ts
@@ -19,7 +19,7 @@
 
 import Span from '../../trace/span/Span';
 import Segment from '../../trace/context/Segment';
-import { Component } from '../../trace/Component';
+import { Component } from '../Component';
 import { ContextCarrier } from './ContextCarrier';
 
 export default interface Context {
@@ -51,4 +51,10 @@ export default interface Context {
   /* This should be called upon entering the new async context for a span that has previously executed .async(), it
      should be the first thing the callback function belonging to the span does. */
   resync(span: Span): void;
+
+  /**
+   * Returns the global trace id of the current trace, if there is no trace when invoking this method,
+   * "N/A" is returned.
+   */
+  traceId(): string;
 }
diff --git a/src/trace/context/DummyContext.ts b/src/trace/context/DummyContext.ts
index 2acf661..509d3e2 100644
--- a/src/trace/context/DummyContext.ts
+++ b/src/trace/context/DummyContext.ts
@@ -21,7 +21,7 @@ import Context from '../../trace/context/Context';
 import Span from '../../trace/span/Span';
 import DummySpan from '../../trace/span/DummySpan';
 import Segment from '../../trace/context/Segment';
-import { Component } from '../../trace/Component';
+import { Component } from '../Component';
 import { ContextCarrier } from './ContextCarrier';
 import ContextManager from './ContextManager';
 
@@ -71,4 +71,11 @@ export default class DummyContext implements Context {
   resync(span: DummySpan) {
     ContextManager.restore(span);
   }
+
+  traceId(): string {
+    if (!this.segment.relatedTraces) {
+      return 'N/A';
+    }
+    return this.segment.relatedTraces[0].toString();
+  }
 }
diff --git a/src/trace/context/SpanContext.ts b/src/trace/context/SpanContext.ts
index 79d39e0..a02a699 100644
--- a/src/trace/context/SpanContext.ts
+++ b/src/trace/context/SpanContext.ts
@@ -28,7 +28,7 @@ import LocalSpan from '../../trace/span/LocalSpan';
 import SegmentRef from './SegmentRef';
 import ContextManager from './ContextManager';
 import Tag from '../../Tag';
-import { Component } from '../../trace/Component';
+import { Component } from '../Component';
 import { createLogger } from '../../logging';
 import { ContextCarrier } from './ContextCarrier';
 import { SpanType } from '../../proto/language-agent/Tracing_pb';
@@ -55,7 +55,7 @@ export default class SpanContext implements Context {
   }
 
   spanCheck(spanType: SpanType, operation: string, carrier?: ContextCarrier): [Span | null, Span?] {
-    let span = this.ignoreCheck(operation, SpanType.ENTRY, carrier);
+    const span = this.ignoreCheck(operation, SpanType.ENTRY, carrier);
 
     if (span)
       return [span];
@@ -75,7 +75,7 @@ export default class SpanContext implements Context {
     const span = new spanClass({
       id: context.spanId++,
       parentId: this.finished ? -1 : parent?.id ?? -1,
-      context: context,
+      context,
       operation,
     });
 
@@ -101,6 +101,7 @@ export default class SpanContext implements Context {
   }
 
   newEntrySpan(operation: string, carrier?: ContextCarrier, inherit?: Component): Span {
+    // tslint:disable-next-line:prefer-const
     let [span, parent] = this.spanCheck(SpanType.ENTRY, operation, carrier);
 
     if (span)
@@ -127,6 +128,7 @@ export default class SpanContext implements Context {
   }
 
   newExitSpan(operation: string, component: Component, inherit?: Component): Span {
+    // tslint:disable-next-line:prefer-const
     let [span, parent] = this.spanCheck(SpanType.EXIT, operation);
 
     if (span)
@@ -151,7 +153,7 @@ export default class SpanContext implements Context {
   }
 
   newLocalSpan(operation: string): Span {
-    let [span, parent] = this.spanCheck(SpanType.LOCAL, operation);
+    const [span, parent] = this.spanCheck(SpanType.LOCAL, operation);
 
     if (span)
       return span;
@@ -228,4 +230,11 @@ export default class SpanContext implements Context {
 
     ContextManager.restore(span);
   }
+
+  traceId(): string {
+    if (!this.segment.relatedTraces) {
+      return 'N/A';
+    }
+    return this.segment.relatedTraces[0].toString();
+  }
 }