File tree 2 files changed +29
-5
lines changed
2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change @@ -1537,18 +1537,24 @@ API.prototype.getTraceMetadata = function getTraceMetadata() {
1537
1537
NAMES . SUPPORTABILITY . API + '/getTraceMetadata'
1538
1538
)
1539
1539
metric . incrementCallCount ( )
1540
-
1541
1540
const metadata = { }
1542
1541
1543
- const segment = this . agent . tracer . getSegment ( )
1542
+ if ( this . agent . config . distributed_tracing . enabled === false ) {
1543
+ return metadata
1544
+ }
1545
+
1544
1546
const transaction = this . agent . tracer . getTransaction ( )
1545
- if ( ! ( segment || transaction ) ) {
1547
+ if ( ! transaction ) {
1546
1548
logger . debug ( 'No transaction found when calling API#getTraceMetadata' )
1547
- } else if ( ! this . agent . config . distributed_tracing . enabled ) {
1548
- logger . debug ( 'Distributed tracing disabled when calling API#getTraceMetadata' )
1549
1549
} else {
1550
1550
metadata . traceId = transaction . traceId
1551
+ }
1552
+
1553
+ const segment = this . agent . tracer . getSegment ( )
1551
1554
1555
+ if ( ! segment ) {
1556
+ logger . debug ( 'No segment found when calling API#getTraceMetadata' )
1557
+ } else {
1552
1558
const spanId = segment . getSpanId ( )
1553
1559
if ( spanId ) {
1554
1560
metadata . spanId = spanId
Original file line number Diff line number Diff line change @@ -71,4 +71,22 @@ test('Agent API - trace metadata', async (t) => {
71
71
end ( )
72
72
} )
73
73
} )
74
+
75
+ await t . test ( 'should not include transaction when not in active transaction' , ( t ) => {
76
+ const { api } = t . nr
77
+ const metadata = api . getTraceMetadata ( )
78
+ assert . deepEqual ( metadata , { } )
79
+ } )
80
+
81
+ await t . test ( 'should not include spanId when in active active transaction but not active segment' , ( t , end ) => {
82
+ const { api, agent } = t . nr
83
+
84
+ helper . runInTransaction ( agent , function ( txn ) {
85
+ agent . tracer . setSegment ( { segment : null } )
86
+ const metadata = api . getTraceMetadata ( )
87
+ assert . equal ( metadata . traceId , txn . traceId )
88
+ assert . ok ( ! metadata . spanId )
89
+ end ( )
90
+ } )
91
+ } )
74
92
} )
You can’t perform that action at this time.
0 commit comments