mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-12 19:23:13 +08:00
b58c11605c
- With the Metrics module inside nylas-core, and bc of our current lerna setup, we required other modules like sequelize and redis before requiring newrelic, thus preventing them from being properly instrumented
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
const {env: {NODE_ENV, SIGNALFX_TOKEN}, pid} = process
|
|
const os = require('os')
|
|
const signalfx = require('signalfx')
|
|
|
|
let newrelicClient = null
|
|
let signalfxClient = null
|
|
|
|
const MetricTypes = {
|
|
Gauge: 'gauges',
|
|
Counter: 'counters',
|
|
CumulativeCounter: 'cumulative_counters',
|
|
}
|
|
const shouldReport = NODE_ENV && NODE_ENV !== 'development'
|
|
|
|
|
|
const Metrics = {
|
|
|
|
MetricTypes,
|
|
|
|
startCapturing(name) {
|
|
if (!shouldReport) { return }
|
|
newrelicClient = require('newrelic')
|
|
signalfxClient = new signalfx.Ingest(SIGNALFX_TOKEN, {
|
|
dimensions: {
|
|
name,
|
|
host: os.hostname(),
|
|
pid: pid.toString(),
|
|
env: NODE_ENV,
|
|
},
|
|
})
|
|
},
|
|
|
|
reportError(error) {
|
|
if (!newrelicClient || !shouldReport) { return }
|
|
newrelicClient.noticeError(error)
|
|
},
|
|
|
|
reportMetric({name, value, type, dimensions = {}} = {}) {
|
|
if (!signalfxClient || !shouldReport) { return }
|
|
if (!name) {
|
|
throw new Error('Metrics.reportMetric requires a metric.name')
|
|
}
|
|
if (value == null) {
|
|
throw new Error('Metrics.reportMetric requires a metric.value')
|
|
}
|
|
if (!type) {
|
|
throw new Error('Metrics.reportMetric requires a metric.type from Metrics.MetricTypes')
|
|
}
|
|
const metric = {metric: name, value, timestamp: Date.now(), dimensions}
|
|
signalfxClient.send({[type]: [metric]})
|
|
},
|
|
}
|
|
|
|
module.exports = Metrics
|