forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
active-trace.js
71 lines (58 loc) · 1.53 KB
/
active-trace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const SimpleCache = require('../utils/simple-cache')
const HistogramSchema = require('./histogram-schema')
const ActiveTraceHistogram = require('./active-trace-histogram')
const log = require('../utils/logger')
const KEY_PREFIX = '_AT_'
const activeTraceCache = new SimpleCache()
const register = (trace) => {
if (!trace || !trace.sampling) {
return
}
activeTraceCache.put(getKeyFromTrace(trace), trace)
}
const remove = (trace) => {
if (!trace) {
return
}
const key = getKeyFromTrace(trace)
const activeTrace = activeTraceCache.get(key)
if (activeTrace) {
activeTraceCache.delete(key)
}
// response time
}
const getKeyFromTrace = (trace) => {
return KEY_PREFIX + (trace.traceId && trace.traceId.transactionSequence())
}
const getAllTraces = () => {
return activeTraceCache.getAll()
}
const getCurrentActiveTraceHistogram = () => {
const currentTime = Date.now()
return getActiveTraceHistogram(currentTime)
}
const getActiveTraceHistogram = (currentTime) => {
const histogram = new ActiveTraceHistogram(HistogramSchema.NORMAL_SCHEMA)
if (activeTraceCache.isEmpty()) {
return histogram
}
getAllTraces().forEach(activeTrace => {
if (activeTrace.getStartTime() > 0) {
const elapsedTime = currentTime - activeTrace.getStartTime()
histogram.increase(elapsedTime)
}
})
return histogram
}
module.exports = {
getAllTraces,
getCurrentActiveTraceHistogram,
register,
remove
}