forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context-manager.js
54 lines (44 loc) · 1.1 KB
/
context-manager.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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const async_hooks = require('async_hooks')
const log = require('../utils/logger')
const traceObjectMap = new Map()
const init = (asyncId, type, triggerAsyncId, resource) => {
if (type === 'TIMERWRAP') return
if (traceObjectMap.has(triggerAsyncId)) {
traceObjectMap.set(asyncId, traceObjectMap.get(triggerAsyncId))
}
}
const destroy = (asyncId) => {
if (traceObjectMap.has(asyncId)) {
traceObjectMap.delete(asyncId)
}
}
const start = () => {
if (global.__PINPOINT_ENABLED__) {
async_hooks.createHook({init, destroy}).enable()
}
}
const getObject = () => {
const asyncId = async_hooks.executionAsyncId()
// log.debug('>> GETTER ASYNC ID:', asyncId)
return traceObjectMap.get(asyncId)
}
const setObject = (value) => {
const asyncId = async_hooks.executionAsyncId()
// log.debug('>> SETTER ASYNC ID :', asyncId)
traceObjectMap.set(asyncId, value)
}
const getAllObject = () => {
return traceObjectMap
}
module.exports = {
getObject,
setObject,
getAllObject,
start,
}