Add an API to access the trace id. (#60)
Closes https://github.com/apache/skywalking/issues/7328
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 @@
/* 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 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 @@
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 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 @@
}
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 @@
const span = new spanClass({
id: context.spanId++,
parentId: this.finished ? -1 : parent?.id ?? -1,
- context: context,
+ context,
operation,
});
@@ -101,6 +101,7 @@
}
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 @@
}
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 @@
}
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 @@
ContextManager.restore(span);
}
+
+ traceId(): string {
+ if (!this.segment.relatedTraces) {
+ return 'N/A';
+ }
+ return this.segment.relatedTraces[0].toString();
+ }
}