This repository was archived by the owner on Nov 10, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ import { Tracer } from '../trace/tracer';
28
28
import { TracerProvider } from '../trace/tracer_provider' ;
29
29
import {
30
30
deleteSpan ,
31
+ getActiveSpan ,
31
32
getSpan ,
32
33
getSpanContext ,
33
34
setSpan ,
@@ -102,6 +103,8 @@ export class TraceAPI {
102
103
103
104
public getSpan = getSpan ;
104
105
106
+ public getActiveSpan = getActiveSpan ;
107
+
105
108
public getSpanContext = getSpanContext ;
106
109
107
110
public setSpan = setSpan ;
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import { Context } from '../context/types';
19
19
import { Span } from './span' ;
20
20
import { SpanContext } from './span_context' ;
21
21
import { NonRecordingSpan } from './NonRecordingSpan' ;
22
+ import { ContextAPI } from '../api/context' ;
22
23
23
24
/**
24
25
* span key
@@ -34,6 +35,13 @@ export function getSpan(context: Context): Span | undefined {
34
35
return ( context . getValue ( SPAN_KEY ) as Span ) || undefined ;
35
36
}
36
37
38
+ /**
39
+ * Gets the span from the current context, if one exists.
40
+ */
41
+ export function getActiveSpan ( ) : Span | undefined {
42
+ return getSpan ( ContextAPI . getInstance ( ) . active ( ) ) ;
43
+ }
44
+
37
45
/**
38
46
* Set the span on a context
39
47
*
Original file line number Diff line number Diff line change @@ -52,6 +52,17 @@ describe('API', () => {
52
52
assert . strictEqual ( typeof tracer , 'object' ) ;
53
53
} ) ;
54
54
55
+ it ( 'getActiveSpan should get the current span' , ( ) => {
56
+ const span = new NonRecordingSpan ( ) ;
57
+ const ctx = trace . setSpan ( ROOT_CONTEXT , span ) ;
58
+ context . setGlobalContextManager ( { active : ( ) => ctx , disable : ( ) => { } } as any ) ;
59
+
60
+ const active = trace . getActiveSpan ( ) ;
61
+ assert . strictEqual ( active , span ) ;
62
+
63
+ context . disable ( ) ;
64
+ } ) ;
65
+
55
66
describe ( 'Context' , ( ) => {
56
67
it ( 'with should forward this, arguments and return value' , ( ) => {
57
68
function fnWithThis ( this : string , a : string , b : number ) : string {
You can’t perform that action at this time.
0 commit comments