forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-meta-service.js
54 lines (44 loc) · 1.42 KB
/
api-meta-service.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 apiMetaCacheKeyGenerator = require('../context/sequence-generator').apiMetaCacheKeyGenerator
const SimpleCache = require('../utils/simple-cache')
const GeneralMethodDescriptor = require('../constant/method-descriptor').GeneralMethodDescriptor
const ApiMetaInfo = require('../data/dto/api-meta-info')
const log = require('../utils/logger')
class StringMetaCache {
constructor() {
this.cache = new SimpleCache(1024)
this.dataSender = null
}
init(dataSender) {
this.dataSender = dataSender
Object.keys(GeneralMethodDescriptor).forEach(name => {
this.cacheApi(GeneralMethodDescriptor[name])
})
}
cacheApi(methodDescriptor) {
if (!methodDescriptor) return
const apiDescriptor = methodDescriptor.apiDescriptor
const cachedValue = this.cache.get(apiDescriptor)
if (cachedValue === null) {
methodDescriptor.apiId = apiMetaCacheKeyGenerator.next
this.cache.put(apiDescriptor, methodDescriptor.apiId)
this.sendApiMetaInfo(methodDescriptor)
}
return methodDescriptor
}
sendApiMetaInfo(methodDescriptor) {
const apiMetaInfo = ApiMetaInfo.create(methodDescriptor)
try {
this.dataSender.send(apiMetaInfo)
} catch (e) {
log.error('Fail to send api meta info', apiMetaInfo, e)
}
return true
}
}
module.exports = new StringMetaCache()