forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-meta-service.js
48 lines (40 loc) · 1.18 KB
/
string-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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const stringMetaCacheKeyGenerator = require('../context/sequence-generator').stringMetaCacheKeyGenerator
const SimpleCache = require('../utils/simple-cache')
const StringMetaInfo = require('../data/dto/string-meta-info')
const log = require('../utils/logger')
class StringMetaService {
constructor() {
this.cache = new SimpleCache(1024)
this.dataSender = null
}
init (dataSender) {
this.dataSender = dataSender
}
get (stringValue) {
if (!stringValue) return 0
const cachedValue = this.cache.get(stringValue)
if (cachedValue === null) {
const id = stringMetaCacheKeyGenerator.next
const stringMetaInfo = StringMetaInfo.create(id, stringValue)
this.cache.put(stringValue, stringMetaInfo)
this.sendStringMetaInfo(stringMetaInfo)
return stringMetaInfo
}
return cachedValue
}
sendStringMetaInfo (stringMetaInfo) {
try {
this.dataSender.send(stringMetaInfo)
} catch (e) {
log.error('Fail to send string meta info', stringMetaInfo, e)
}
return true
}
}
module.exports = new StringMetaService()