Skip to content

Commit 63880f7

Browse files
committed
[#242] Fix Histogram gRPC Data convert No Data
1 parent cf42f1c commit 63880f7

File tree

11 files changed

+197
-133
lines changed

11 files changed

+197
-133
lines changed

demo/express/routes/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ router.get('/', async function(req, res, next) {
4848
const json = await response.json()
4949
console.log(json)
5050

51-
res.render('index', { title: 'Express' })
51+
setTimeout(() => {
52+
res.render('index', { title: 'Express' })
53+
}, 3000)
5254
})
5355

5456
router.get('/api', function(req, res, next) {

lib/client/grpc-data-sender.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,12 @@ class GrpcDataSender {
350350
try {
351351
const pStatMessage = dataConvertor.convertStat(stat)
352352
if (log.isDebug()) {
353-
log.debug(`sendStats pStatMessage: ${stat}`)
353+
log.debug('sendStats pStatMessage: ', stat)
354354
}
355355
this.statStream.write(pStatMessage)
356356
} catch (e) {
357357
if (e && e.stack) {
358-
log.error(`sendStat(stat) Error: ${e.stack}`)
358+
log.error('sendStat(stat) Error: ', e)
359359
}
360360
}
361361
}

lib/context/trace-context.js

Lines changed: 5 additions & 6 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() {
@@ -81,10 +80,10 @@ class TraceContext {
8180
if (!trace) {
8281
return
8382
}
83+
8484
try {
8585
trace.close()
86-
this.activeRequestRepository.remove(trace.getTraceRoot())
87-
// activeTrace.remove(trace)
86+
activeRequestRepository.remove(trace.getTraceRoot())
8887
} catch (e) {
8988
log.error('Fail to complete trace object', e)
9089
}
@@ -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

lib/data/grpc-data-convertor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,11 @@ const convertStat = (stat) => {
278278
pActiveTraceHistogram.setVersion(0)
279279
pActiveTraceHistogram.setHistogramschematype(stat.activeTrace.typeCode)
280280

281-
const count = stat.activeTrace.fastCount + stat.activeTrace.normalCount + stat.activeTrace.slowCount + stat.activeTrace.verySlowCount
282-
pActiveTraceHistogram.addActivetracecount(count)
283-
281+
stat.activeTrace.histogramValues?.().forEach((value, index) => {
282+
pActiveTraceHistogram.addActivetracecount(value)
283+
})
284284
pActiveTrace.setHistogram(pActiveTraceHistogram)
285+
pAgentStat.setActivetrace(pActiveTrace)
285286
}
286287

287288
pStatMessage.setAgentstat(pAgentStat)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Pinpoint Node.js Agent
3+
* Copyright 2020-present NAVER Corp.
4+
* Apache License v2.0
5+
*/
6+
7+
'use strict'
8+
9+
const SimpleCache = require('../utils/simple-cache')
10+
const ActiveTraceHistogram = require('./active-trace-histogram')
11+
const HistogramSchema = require('./histogram-schema')
12+
13+
// DefaultActiveTraceRepository.java
14+
class ActiveRequestRepository {
15+
constructor() {
16+
this.activeTraceCache = new SimpleCache()
17+
}
18+
19+
register(localTraceRoot) {
20+
const id = localTraceRoot.getTransactionId()
21+
if (typeof id !== 'string' || id.length < 1) {
22+
return
23+
}
24+
25+
this.activeTraceCache.put(id, localTraceRoot)
26+
}
27+
28+
remove(localTraceRoot) {
29+
const id = localTraceRoot.getTransactionId()
30+
this.activeTraceCache.delete(id)
31+
}
32+
33+
getCurrentActiveTraceHistogram() {
34+
const currentTime = Date.now()
35+
return this.getActiveTraceHistogram(currentTime)
36+
}
37+
38+
getActiveTraceHistogram(currentTime) {
39+
const histogram = new ActiveTraceHistogram(HistogramSchema.NORMAL_SCHEMA)
40+
this.activeTraceCache.getAll().forEach((traceRoot) => {
41+
const elapsedTime = currentTime - traceRoot.getTraceStartTime()
42+
histogram.increase(elapsedTime)
43+
})
44+
return histogram
45+
}
46+
}
47+
48+
module.exports = new ActiveRequestRepository()

lib/metric/active-trace-histogram.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
const HistogramSchema = require('./histogram-schema')
1010

11-
class ActiveTraceHistogram{
12-
constructor (schema) {
11+
class ActiveTraceHistogram {
12+
constructor(schema) {
1313
this.schema = schema || HistogramSchema.NORMAL_SCHEMA
1414
this.typeCode = schema.typeCode
1515
this.fastCount = 0
@@ -18,7 +18,7 @@ class ActiveTraceHistogram{
1818
this.verySlowCount = 0
1919
}
2020

21-
increase (elapsedTime) {
21+
increase(elapsedTime) {
2222
if (!elapsedTime) {
2323
return
2424
}
@@ -33,6 +33,15 @@ class ActiveTraceHistogram{
3333
this.verySlowCount++
3434
}
3535
}
36+
37+
histogramValues() {
38+
return [
39+
this.fastCount,
40+
this.normalCount,
41+
this.slowCount,
42+
this.verySlowCount
43+
]
44+
}
3645
}
3746

3847
module.exports = ActiveTraceHistogram

lib/metric/active-trace-repository.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/metric/agent-stats-monitor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const ResourceStatsCollector = require('./resource-stats-collector')
1010
const log = require('../utils/logger')
11+
const activeRequestRepository = require('../metric/active-request-repository')
1112

1213
class AgentStatsMonitor {
1314
constructor(dataSender, agentId, agentStartTime) {
@@ -47,7 +48,7 @@ class AgentStatsMonitor {
4748
collectInterval: 1000,
4849
memory: this.resourceStatCollector.getMemoryStats(),
4950
cpu: cpuStatus,
50-
// activeTrace: activeTrace.getCurrentActiveTraceHistogram(),
51+
activeTrace: activeRequestRepository.getCurrentActiveTraceHistogram(),
5152
}
5253
}
5354
}

0 commit comments

Comments
 (0)