Skip to content

Commit 4abdc45

Browse files
committed
[#242] Extract Class refactoring from ActiveTrace to ActiveRequestRepository
1 parent cf42f1c commit 4abdc45

File tree

4 files changed

+11
-50
lines changed

4 files changed

+11
-50
lines changed

lib/context/trace-context.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const AsyncSpanChunkBuilder = require('./trace/async-span-chunk-builder')
2323
const ChildTraceBuilder = require('./trace/child-trace-builder')
2424
const DisableChildTrace = require('./trace/disable-child-trace')
2525
const disableAsyncId = require('./trace/disable-async-id')
26-
const ActiveTraceRepository = require('../metric/active-trace-repository')
26+
const activeRequestRepository = require('../metric/active-request-repository')
2727

2828
class TraceContext {
2929
constructor(agentInfo, dataSender, config) {
@@ -35,7 +35,6 @@ class TraceContext {
3535
this.enableSampling = config.sampling
3636
}
3737
this.traceSampler = new TraceSampler(agentInfo, config)
38-
this.activeRequestRepository = new ActiveTraceRepository()
3938
}
4039

4140
getAgentInfo() {
@@ -83,7 +82,7 @@ class TraceContext {
8382
}
8483
try {
8584
trace.close()
86-
this.activeRequestRepository.remove(trace.getTraceRoot())
85+
activeRequestRepository.remove(trace.getTraceRoot())
8786
// activeTrace.remove(trace)
8887
} catch (e) {
8988
log.error('Fail to complete trace object', e)
@@ -118,7 +117,7 @@ class TraceContext {
118117
}
119118

120119
newLocalTrace(traceRoot) {
121-
this.activeRequestRepository.register(traceRoot)
120+
activeRequestRepository.register(traceRoot)
122121
return new DisableTrace(traceRoot)
123122
}
124123

@@ -137,7 +136,7 @@ class TraceContext {
137136
const spanBuilder = new SpanBuilder(traceRoot)
138137
const spanChunkBuilder = new SpanChunkBuilder(traceRoot)
139138
const repository = new SpanRepository(spanChunkBuilder, this.dataSender, this.agentInfo)
140-
this.activeRequestRepository.register(traceRoot)
139+
activeRequestRepository.register(traceRoot)
141140
return new Trace2(spanBuilder, repository)
142141
}
143142

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
const SimpleCache = require('../utils/simple-cache')
1010

1111
// DefaultActiveTraceRepository.java
12-
class ActiveTraceRepository {
12+
class ActiveRequestRepository {
1313
constructor() {
1414
this.activeTraceCache = new SimpleCache()
1515
}
@@ -29,4 +29,4 @@ class ActiveTraceRepository {
2929
}
3030
}
3131

32-
module.exports = ActiveTraceRepository
32+
module.exports = new ActiveRequestRepository()

test/stats/active-trace.test.js

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,20 @@
66

77
const test = require('tape')
88
const axios = require('axios')
9-
const { fixture, util } = require('../test-helper')
10-
const activeTrace = require('../../lib/metric/active-trace')
119
const agent = require('../support/agent-singleton-mock')
1210
const express = require('express')
1311
const http = require('http')
1412
const grpc = require('@grpc/grpc-js')
1513
const services = require('../../lib/data/v1/Service_grpc_pb')
1614
const spanMessages = require('../../lib/data/v1/Span_pb')
15+
const activeRequestRepository = require('../../lib/metric/active-request-repository')
1716

1817
const TEST_ENV = {
1918
host: 'localhost',
2019
port: 5005,
2120
}
2221
const getServerUrl = (path) => `http://${TEST_ENV.host}:${TEST_ENV.port}${path}`
2322

24-
test(`Should record active trace in multiple call`, function (t) {
25-
agent.bindHttp()
26-
27-
const PATH = '/active-trace'
28-
const LASTONE_PATH = '/active-trace/lastone'
29-
const SHUTDOWN = '/shutdown'
30-
const app = new express()
31-
32-
app.get(PATH, async (req, res) => {
33-
await util.sleep(2000)
34-
res.send('ok get')
35-
})
36-
37-
app.get(LASTONE_PATH, async (req, res) => {
38-
res.send('ok get')
39-
})
40-
41-
const server = app.listen(TEST_ENV.port, async function () {
42-
Promise.all([
43-
axios.get(getServerUrl(PATH), { httpAgent: new http.Agent({ keepAlive: false }) }),
44-
axios.get(getServerUrl(PATH), { httpAgent: new http.Agent({ keepAlive: false }) }),
45-
axios.get(getServerUrl(LASTONE_PATH), { httpAgent: new http.Agent({ keepAlive: false }) }),
46-
]).then((result) => {
47-
t.equal(activeTrace.getAllTraces().length, 0)
48-
t.equal('' + agent.mockAgentStartTime, agent.agentInfo.startTimestamp, "startTimestamp equals")
49-
50-
server.close()
51-
t.end()
52-
}).catch(() => {
53-
server.close()
54-
t.end()
55-
})
56-
})
57-
58-
t.equal(agent.mockAgentId, fixture.config.agentId, "Agent ID equals")
59-
t.equal(agent.agentInfo, agent.pinpointClient.agentInfo, "AgentInfo equals")
60-
})
61-
6223
test(`Active trace should be recorded with HTTP call`, function (t) {
6324
const collectorServer = new grpc.Server()
6425
collectorServer.addService(services.MetadataService, {
@@ -73,7 +34,7 @@ test(`Active trace should be recorded with HTTP call`, function (t) {
7334
const app = new express()
7435
app.get('/active-trace', async (req, res) => {
7536
agent.callbackTraceClose((trace) => {
76-
const actualCached = agent.getTraceContext().activeRequestRepository.activeTraceCache.get(trace.getTraceRoot().getTransactionId())
37+
const actualCached = activeRequestRepository.activeTraceCache.get(trace.getTraceRoot().getTransactionId())
7738
t.equal(actualCached, trace.getTraceRoot(), 'active trace traceRoot is cached')
7839
})
7940
setTimeout(() => {
@@ -85,7 +46,7 @@ test(`Active trace should be recorded with HTTP call`, function (t) {
8546
const result = await axios.get(getServerUrl('/active-trace'), { httpAgent: new http.Agent({ keepAlive: false }) })
8647
t.equal(result.status, 200, 'status code is 200')
8748
server.close(() => {
88-
const cacheSize = agent.getTraceContext().activeRequestRepository.activeTraceCache.cache.size
49+
const cacheSize = activeRequestRepository.activeTraceCache.cache.size
8950
t.equal(cacheSize, 0, 'active trace cache is empty')
9051
t.end()
9152
})

test/support/agent-singleton-mock.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const transactionIdGenerator = require('../../lib/context/sequence-generators').
2323
const closedTraceWrapped = Symbol('closedTraceWrapped')
2424
const stringMetaService = require('../../lib/context/string-meta-service')
2525
const apiMetaService = require('../../lib/context/api-meta-service')
26+
const activeRequestRepository = require('../../lib/metric/active-request-repository')
2627

2728
let traces = []
2829
const resetTraces = () => {
@@ -111,7 +112,7 @@ class MockAgent extends Agent {
111112
if (sampler.getSamplingCountGenerator()) {
112113
sampler.getSamplingCountGenerator().reset()
113114
}
114-
this.traceContext.activeRequestRepository.activeTraceCache.cache.clear()
115+
activeRequestRepository.activeTraceCache.cache.clear()
115116
transactionIdGenerator.reset()
116117

117118
httpShared.clearPathMatcher()

0 commit comments

Comments
 (0)