fix: telemetry
This commit is contained in:
@@ -1,57 +1,89 @@
|
||||
const _ = require('lodash')
|
||||
const axios = require('axios')
|
||||
const bugsnag = require('bugsnag')
|
||||
const path = require('path')
|
||||
const bugsnag = require('@bugsnag/node')
|
||||
const uuid = require('uuid/v4')
|
||||
const qs = require('querystring')
|
||||
const os = require('os')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
module.exports = {
|
||||
cid: '',
|
||||
client: null,
|
||||
enabled: false,
|
||||
init() {
|
||||
this.cid = uuid()
|
||||
bugsnag.register(WIKI.data.telemetry.BUGSNAG_ID, {
|
||||
this.client = bugsnag({
|
||||
apiKey: WIKI.data.telemetry.BUGSNAG_ID,
|
||||
appType: 'server',
|
||||
appVersion: WIKI.version,
|
||||
autoNotify: false,
|
||||
hostname: this.cid,
|
||||
collectUserIp: false,
|
||||
hostname: _.get(WIKI.config, 'telemetry.clientId', uuid()),
|
||||
notifyReleaseStages: ['production'],
|
||||
packageJSON: path.join(WIKI.ROOTPATH, 'package.json'),
|
||||
releaseStage: WIKI.IS_DEBUG ? 'development' : 'production',
|
||||
projectRoot: WIKI.ROOTPATH,
|
||||
useSSL: true
|
||||
})
|
||||
bugsnag.onBeforeNotify((notification, originalError) => {
|
||||
if (!this.enabled) { return false }
|
||||
logger: null,
|
||||
beforeSend: (report) => {
|
||||
if (!WIKI.telemetry.enabled) { return false }
|
||||
}
|
||||
})
|
||||
WIKI.telemetry = this
|
||||
|
||||
if (_.get(WIKI.config, 'logging.telemetry', false) === true) {
|
||||
if (_.get(WIKI.config, 'telemetry.isEnabled', false) === true) {
|
||||
this.enabled = true
|
||||
this.sendOSInfo()
|
||||
}
|
||||
|
||||
return this
|
||||
},
|
||||
sendOSInfo() {
|
||||
this.sendBatchEvents([
|
||||
{
|
||||
eventCategory: 'node-version',
|
||||
eventAction: process.version
|
||||
},
|
||||
{
|
||||
eventCategory: 'os-platform',
|
||||
eventAction: os.platform()
|
||||
},
|
||||
{
|
||||
eventCategory: 'cpu-cores',
|
||||
eventAction: os.cpus().length
|
||||
},
|
||||
{
|
||||
eventCategory: 'db-type',
|
||||
eventAction: WIKI.config.db.type
|
||||
}
|
||||
])
|
||||
},
|
||||
sendError(err) {
|
||||
bugsnag.notify(err, { userId: this.cid })
|
||||
this.client.notify(err)
|
||||
},
|
||||
sendEvent(eventCategory, eventAction, eventLabel) {
|
||||
if (!this.enabled) { return false }
|
||||
this.sendBatchEvents([{
|
||||
eventCategory,
|
||||
eventAction,
|
||||
eventLabel
|
||||
}])
|
||||
},
|
||||
sendBatchEvents(events) {
|
||||
if (!this.enabled || WIKI.IS_DEBUG) { return false }
|
||||
axios({
|
||||
method: 'post',
|
||||
url: WIKI.data.telemetry.GA_REMOTE,
|
||||
headers: {
|
||||
'Content-type': 'application/x-www-form-urlencoded'
|
||||
'Content-type': 'text/plain'
|
||||
},
|
||||
params: {
|
||||
v: 1, // API version
|
||||
tid: WIKI.data.telemetry.GA_ID, // Tracking ID
|
||||
aip: 1, // Anonymize IP
|
||||
ds: 'server', // Data source
|
||||
cid: this.cid, // Client ID
|
||||
t: 'event', // Hit Type
|
||||
ec: eventCategory, // Event Category
|
||||
ea: eventAction, // Event Action
|
||||
el: eventLabel // Event Label
|
||||
}
|
||||
data: events.map(ev => {
|
||||
return qs.stringify({
|
||||
v: 1, // API version
|
||||
tid: WIKI.data.telemetry.GA_ID, // Tracking ID
|
||||
aip: 1, // Anonymize IP
|
||||
ds: 'server', // Data source
|
||||
cid: WIKI.telemetry.cid, // Client ID
|
||||
t: 'event', // Hit Type
|
||||
ec: ev.eventCategory, // Event Category
|
||||
ea: ev.eventAction, // Event Action
|
||||
el: ev.eventLabel // Event Label
|
||||
})
|
||||
}).join('\n')
|
||||
}).then(resp => {
|
||||
if (resp.status !== 200) {
|
||||
WIKI.logger.warn('Unable to send analytics telemetry request.')
|
||||
@@ -59,5 +91,9 @@ module.exports = {
|
||||
}, err => {
|
||||
WIKI.logger.warn('Unable to send analytics telemetry request.')
|
||||
})
|
||||
},
|
||||
generateClientId() {
|
||||
_.set(WIKI.config, 'telemetry.clientId', uuid())
|
||||
return WIKI.config.telemetry.clientId
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user