You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wiki/server/core/telemetry.js

100 lines
2.6 KiB

const _ = require('lodash')
const axios = require('axios')
6 years ago
const bugsnag = require('@bugsnag/node')
const uuid = require('uuid/v4')
6 years ago
const qs = require('querystring')
const os = require('os')
/* global WIKI */
module.exports = {
6 years ago
client: null,
enabled: false,
init() {
6 years ago
this.client = bugsnag({
apiKey: WIKI.data.telemetry.BUGSNAG_ID,
appType: 'server',
appVersion: WIKI.version,
autoNotify: false,
6 years ago
collectUserIp: false,
hostname: _.get(WIKI.config, 'telemetry.clientId', uuid()),
notifyReleaseStages: ['production'],
6 years ago
releaseStage: WIKI.IS_DEBUG ? 'development' : 'production',
projectRoot: WIKI.ROOTPATH,
6 years ago
logger: null,
beforeSend: (report) => {
if (!WIKI.telemetry.enabled) { return false }
}
})
6 years ago
WIKI.telemetry = this
6 years ago
if (_.get(WIKI.config, 'telemetry.isEnabled', false) === true) {
this.enabled = true
6 years ago
this.sendOSInfo()
}
6 years ago
},
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) {
6 years ago
this.client.notify(err)
},
sendEvent(eventCategory, eventAction, eventLabel) {
6 years ago
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: {
6 years ago
'Content-type': 'text/plain'
},
6 years ago
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.')
}
}, err => {
WIKI.logger.warn('Unable to send analytics telemetry request.')
})
6 years ago
},
generateClientId() {
_.set(WIKI.config, 'telemetry.clientId', uuid())
return WIKI.config.telemetry.clientId
}
}