Skip to content

Commit

Permalink
[pinpoint-apm#242] Fix Histogram gRPC Data convert No Data
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Feb 5, 2025
1 parent cf42f1c commit 008d6c2
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 133 deletions.
4 changes: 3 additions & 1 deletion demo/express/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ router.get('/', async function(req, res, next) {
const json = await response.json()
console.log(json)

res.render('index', { title: 'Express' })
setTimeout(() => {
res.render('index', { title: 'Express' })
}, 3000)
})

router.get('/api', function(req, res, next) {
Expand Down
4 changes: 2 additions & 2 deletions lib/client/grpc-data-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,12 @@ class GrpcDataSender {
try {
const pStatMessage = dataConvertor.convertStat(stat)
if (log.isDebug()) {
log.debug(`sendStats pStatMessage: ${stat}`)
log.debug('sendStats pStatMessage: ', stat)
}
this.statStream.write(pStatMessage)
} catch (e) {
if (e && e.stack) {
log.error(`sendStat(stat) Error: ${e.stack}`)
log.error('sendStat(stat) Error: ', e)
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions lib/context/trace-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const AsyncSpanChunkBuilder = require('./trace/async-span-chunk-builder')
const ChildTraceBuilder = require('./trace/child-trace-builder')
const DisableChildTrace = require('./trace/disable-child-trace')
const disableAsyncId = require('./trace/disable-async-id')
const ActiveTraceRepository = require('../metric/active-trace-repository')
const activeRequestRepository = require('../metric/active-request-repository')

class TraceContext {
constructor(agentInfo, dataSender, config) {
Expand All @@ -35,7 +35,6 @@ class TraceContext {
this.enableSampling = config.sampling
}
this.traceSampler = new TraceSampler(agentInfo, config)
this.activeRequestRepository = new ActiveTraceRepository()
}

getAgentInfo() {
Expand Down Expand Up @@ -81,10 +80,10 @@ class TraceContext {
if (!trace) {
return
}

try {
trace.close()
this.activeRequestRepository.remove(trace.getTraceRoot())
// activeTrace.remove(trace)
activeRequestRepository.remove(trace.getTraceRoot())
} catch (e) {
log.error('Fail to complete trace object', e)
}
Expand Down Expand Up @@ -118,7 +117,7 @@ class TraceContext {
}

newLocalTrace(traceRoot) {
this.activeRequestRepository.register(traceRoot)
activeRequestRepository.register(traceRoot)
return new DisableTrace(traceRoot)
}

Expand All @@ -137,7 +136,7 @@ class TraceContext {
const spanBuilder = new SpanBuilder(traceRoot)
const spanChunkBuilder = new SpanChunkBuilder(traceRoot)
const repository = new SpanRepository(spanChunkBuilder, this.dataSender, this.agentInfo)
this.activeRequestRepository.register(traceRoot)
activeRequestRepository.register(traceRoot)
return new Trace2(spanBuilder, repository)
}

Expand Down
7 changes: 4 additions & 3 deletions lib/data/grpc-data-convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,11 @@ const convertStat = (stat) => {
pActiveTraceHistogram.setVersion(0)
pActiveTraceHistogram.setHistogramschematype(stat.activeTrace.typeCode)

const count = stat.activeTrace.fastCount + stat.activeTrace.normalCount + stat.activeTrace.slowCount + stat.activeTrace.verySlowCount
pActiveTraceHistogram.addActivetracecount(count)

stat.activeTrace.histogramValues?.().forEach((value, index) => {
pActiveTraceHistogram.addActivetracecount(value)
})
pActiveTrace.setHistogram(pActiveTraceHistogram)
pAgentStat.setActivetrace(pActiveTrace)
}

pStatMessage.setAgentstat(pAgentStat)
Expand Down
48 changes: 48 additions & 0 deletions lib/metric/active-request-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

const SimpleCache = require('../utils/simple-cache')
const ActiveTraceHistogram = require('./active-trace-histogram')
const HistogramSchema = require('./histogram-schema')

// DefaultActiveTraceRepository.java
class ActiveRequestRepository {
constructor() {
this.activeTraceCache = new SimpleCache()
}

register(localTraceRoot) {
const id = localTraceRoot.getTransactionId()
if (typeof id !== 'string' || id.length < 1) {
return
}

this.activeTraceCache.put(id, localTraceRoot)
}

remove(localTraceRoot) {
const id = localTraceRoot.getTransactionId()
this.activeTraceCache.delete(id)
}

getCurrentActiveTraceHistogram() {
const currentTime = Date.now()
return this.getActiveTraceHistogram(currentTime)
}

getActiveTraceHistogram(currentTime) {
const histogram = new ActiveTraceHistogram(HistogramSchema.NORMAL_SCHEMA)
this.activeTraceCache.getAll().forEach((traceRoot) => {
const elapsedTime = currentTime - traceRoot.getTraceStartTime()
histogram.increase(elapsedTime)
})
return histogram
}
}

module.exports = new ActiveRequestRepository()
15 changes: 12 additions & 3 deletions lib/metric/active-trace-histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

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

class ActiveTraceHistogram{
constructor (schema) {
class ActiveTraceHistogram {
constructor(schema) {
this.schema = schema || HistogramSchema.NORMAL_SCHEMA
this.typeCode = schema.typeCode
this.fastCount = 0
Expand All @@ -18,7 +18,7 @@ class ActiveTraceHistogram{
this.verySlowCount = 0
}

increase (elapsedTime) {
increase(elapsedTime) {
if (!elapsedTime) {
return
}
Expand All @@ -33,6 +33,15 @@ class ActiveTraceHistogram{
this.verySlowCount++
}
}

histogramValues() {
return [
this.fastCount,
this.normalCount,
this.slowCount,
this.verySlowCount
]
}
}

module.exports = ActiveTraceHistogram
Expand Down
32 changes: 0 additions & 32 deletions lib/metric/active-trace-repository.js

This file was deleted.

3 changes: 2 additions & 1 deletion lib/metric/agent-stats-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

class AgentStatsMonitor {
constructor(dataSender, agentId, agentStartTime) {
Expand Down Expand Up @@ -47,7 +48,7 @@ class AgentStatsMonitor {
collectInterval: 1000,
memory: this.resourceStatCollector.getMemoryStats(),
cpu: cpuStatus,
// activeTrace: activeTrace.getCurrentActiveTraceHistogram(),
activeTrace: activeRequestRepository.getCurrentActiveTraceHistogram(),
}
}
}
Expand Down
Loading

0 comments on commit 008d6c2

Please sign in to comment.