-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
50 lines (44 loc) · 1.51 KB
/
index.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
'use strict'
const binary = require('@mapbox/node-pre-gyp')
const path = require('path')
const pkgpath = path.resolve(path.join(__dirname, './package.json'))
const binding_path = binary.find(pkgpath) // eslint-disable-line camelcase
const bindings = require(binding_path)
module.exports = bindings
module.exports.version = require('./package.json').version
module.exports.init = function (sk) {
return module.exports.oboeInit({ serviceKey: sk || process.env.APPOPTICS_SERVICE_KEY })
}
try {
// this is hardcoded as node-pre-gyp doesn't know about multiple targets.
const metrics_path = binding_path.replace('apm_bindings.node', 'ao_metrics.node') // eslint-disable-line camelcase
module.exports.metrics = require(metrics_path)
} catch (e) {
// eslint-disable-next-line no-console
console.warn(`appoptics metrics disabled ${e.message}`)
// return an dummy metrics if this can't be loaded
module.exports.metrics = {
start () { return true },
stop () { return true },
getMetrics () { return {} }
}
}
const Event = module.exports.Event
//
// augment the c++ event object with a string conversion
// function.
//
Event.makeFromString = function (string) {
if (!string || string.length != 60) { // eslint-disable-line eqeqeq
return undefined
}
const b = Buffer.from(string, 'hex')
if (b.length !== 30 || b[0] !== 0x2b || b[29] & 0xFE) {
return undefined
}
// an all zero op id is not valid.
if (string.startsWith('0'.repeat(16), 42)) {
return undefined
}
return Event.makeFromBuffer(b)
}