Skip to content

Commit bef89cf

Browse files
committed
Refactor trace context tests; update trace ID handling and improve assertions
1 parent e9e7031 commit bef89cf

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

test/context/trace-context.test.js

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@ const RequestHeaderUtils = require('../../lib/instrumentation/request-header-uti
1414
const defaultPredefinedMethodDescriptorRegistry = require('../../lib/constant/default-predefined-method-descriptor-registry')
1515
const localStorage = require('../../lib/instrumentation/context/local-storage')
1616
const agent = require('../support/agent-singleton-mock')
17+
const TraceIdBuilder = require('../../lib/context/trace/trace-id-builder')
1718

1819
test('Should create continued trace and add span info', function (t) {
1920
t.plan(2)
2021

21-
const transactionId = fixture.getTransactionId()
22-
const traceId = fixture.getTraceId(transactionId)
23-
traceId.sampled = true
2422
const traceContext = new TraceContext(agent.agentInfo, dataSenderMock(), agent.config)
25-
26-
const trace = traceContext.continueTraceObject(traceId)
23+
const traceId = new TraceIdBuilder(agent.agentInfo.getAgentId(), agent.agentInfo.getAgentStartTime(), '9').build()
24+
const trace = traceContext.continueTraceObject2(traceId)
2725
localStorage.run(trace, () => {
28-
t.equal(traceContext.currentTraceObject().traceId.transactionId.toString(), transactionId.toString())
29-
26+
t.equal(traceContext.currentTraceObject().getTraceId(), traceId, `traceId is ${traceId}`)
27+
3028
trace.spanRecorder.recordServiceType(ServiceType.express)
3129
trace.spanRecorder.recordApi(defaultPredefinedMethodDescriptorRegistry.nodeServerMethodDescriptor)
32-
33-
t.equal(traceContext.currentTraceObject().span.serviceType, ServiceType.express)
30+
31+
t.equal(traceContext.currentTraceObject().spanBuilder.serviceType, ServiceType.express.getCode())
3432
traceContext.completeTraceObject(trace)
3533
})
3634
})
@@ -40,45 +38,41 @@ test('Should begin/end trace block asynchronously', async function (t) {
4038

4139
// start trace and write span info
4240
const traceContext = new TraceContext(agent.agentInfo, dataSenderMock(), agent.config)
43-
const startedTrace = traceContext.newTraceObject(true)
41+
const startedTrace = traceContext.newTraceObject2('/')
4442

4543
localStorage.run(startedTrace, () => {
4644
const spanRecorder = startedTrace.spanRecorder
4745
spanRecorder.recordServiceType(ServiceType.express)
48-
46+
4947
const currentTrace = traceContext.currentTraceObject()
5048
const spanEventRecorder = currentTrace.traceBlockBegin()
5149
spanEventRecorder.recordServiceType(ServiceType.express)
5250
spanEventRecorder.recordApi(defaultPredefinedMethodDescriptorRegistry.nodeServerMethodDescriptor)
53-
54-
t.equal(traceContext.currentTraceObject().callStack.length, 1)
55-
51+
52+
t.equal(traceContext.currentTraceObject().callStack.stack.length, 1)
53+
5654
const anotherContext = traceContext.currentTraceObject()
5755
t.equal(anotherContext.traceId, currentTrace.traceId)
58-
56+
5957
const spanEventRecorder2 = anotherContext.traceBlockBegin()
60-
t.equal(traceContext.currentTraceObject().callStack.length, 2)
61-
58+
t.equal(traceContext.currentTraceObject().callStack.stack.length, 2)
59+
6260
anotherContext.traceBlockEnd(spanEventRecorder2)
63-
61+
6462
currentTrace.traceBlockEnd(spanEventRecorder)
65-
t.equal(traceContext.currentTraceObject().callStack.length, 0, "traceBolckEnd callstack length is zero")
63+
t.equal(traceContext.currentTraceObject().callStack.stack.length, 0, "traceBolckEnd callstack length is zero")
6664
})
6765
})
6866

6967
test('Should complete trace ', async function (t) {
7068
t.plan(1)
71-
72-
const transactionId = fixture.getTransactionId()
73-
const traceId = fixture.getTraceId(transactionId)
7469
const traceContext = new TraceContext(agent.agentInfo, dataSenderMock(), agent.config)
75-
76-
const trace = traceContext.newTraceObject(traceId)
70+
const trace = traceContext.newTraceObject2('/')
7771

7872
await util.sleep(501)
7973

8074
traceContext.completeTraceObject(trace)
81-
t.ok(trace.spanRecorder.span.elapsedTime > 0)
75+
t.ok(trace.spanRecorder.spanBuilder.elapsedTime > 0)
8276
})
8377

8478
test('new Trace', (t) => {

test/fixture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const localStorage = require('../lib/instrumentation/context/local-storage')
1616
const getTransactionId = () => {
1717
const agentId = config.agentId
1818
const agentStartTime = Date.now()
19-
return new TransactionId(agentId, agentStartTime.toString(), "99")
19+
return new TransactionId(agentId, agentStartTime.toString(), '99')
2020
}
2121

2222
const getTraceId = (transactionId) => {

test/support/agent-singleton-mock.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ const Agent = require('../../lib/agent')
1313
const dataSenderMock = require('./data-sender-mock')
1414
const shimmer = require('@pinpoint-apm/shimmer')
1515
const httpShared = require('../../lib/instrumentation/http-shared')
16+
const TraceContext = require('../../lib/context/trace-context')
1617
const activeTrace = require('../../lib/metric/active-trace')
1718
const apiMetaService = require('../../lib/context/api-meta-service')
1819
const localStorage = require('../../lib/instrumentation/context/local-storage')
20+
const { cleanup } = require('../fixture')
1921
const sqlMetaDataService = require('../../lib/instrumentation/sql/sql-metadata-service')
2022
const SimpleCache = require('../../lib/utils/simple-cache')
2123
const sampler = require('../../lib/sampler/sampler')
2224
const transactionIdGenerator = require('../../lib/context/sequence-generators').transactionIdGenerator
2325
const closedTraceWrapped = Symbol('closedTraceWrapped')
24-
const TraceContext = require('../../lib/context/trace-context')
2526

2627
class MockAgent extends Agent {
2728
startSchedule(agentId, agentStartTime) {
@@ -72,12 +73,13 @@ class MockAgent extends Agent {
7273
})
7374

7475
this.dataSender = dataSender
75-
this.traceContext = new TraceContext(this.agentInfo, this.dataSender, this.config)
76+
this.traceContext.dataSender = dataSender
7677

7778
this.closedTraces = []
7879
}
7980

8081
cleanHttp() {
82+
cleanup()
8183
const http = require('http')
8284
shimmer.unwrap(http && http.Server && http.Server.prototype, 'emit')
8385
shimmer.unwrap(http, 'request')

0 commit comments

Comments
 (0)