-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (64 loc) · 2.21 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import got from 'got'
import prom from 'prom-client'
import Koa from 'koa'
import _ from 'koa-route'
import debug from 'debug'
import assert from 'assert'
import { config as envConfig } from 'dotenv'
import apps from './applications'
envConfig()
const app = new Koa()
const serverLog = debug('app:server')
const apiLog = debug('app:api:update')
const errorLog = debug('app:api:error')
const { CUSTOMER_ID: customerId, AUTHORIZATION: Authorization } = process.env
assert(customerId && Authorization, 'Error: must have CUSTOMER_ID and AUTHORIZATION in environment or .env file')
const prefixUrl = `https://api.trackjs.com/${customerId}/v1`
const options = {
prefixUrl,
headers: { Authorization },
searchParams: { size: 1, startDate: 0 } // setting startDate is best practice per TrackJS people
}
const updateDate = () => {
let d = new Date()
d.setDate(d.getDate() - 2)
options.searchParams.startDate = d.toISOString()
}
const api = got.extend(options)
const labelNames = ['application']
const errorGauge = new prom.Gauge({
labelNames,
name: 'trackjs_error_count',
help: 'trackjs_errors_by_day'
})
const visitGauge = new prom.Gauge({
labelNames,
name: 'trackjs_visit_count',
help: 'trackjs_visits_by_day'
})
const updateAllApps = async () => await Promise.all(apps.map(async app => await updateMetricsForApp(app)))
const getCount = ({ data }) => data[0].count
async function updateMetricsForApp(application) {
try {
const apiData = await Promise.all([
api('errors/daily', { searchParams: { application } }).json(),
api('hits/daily', { searchParams: { application } }).json(),
])
const [errorCount, visitCount] = apiData.map(getCount)
errorGauge.labels(application).set(errorCount)
visitGauge.labels(application).set(visitCount)
apiLog(application, '::', 'errors:', errorCount, ' - ', 'visits:', visitCount)
} catch (e) {
errorLog(e)
}
}
app.use(_.get('/list', ctx => ctx.body = apps))
app.use(_.get('/_/metrics', ctx => {
ctx.status = 200
ctx.set('Content-Type', prom.register.contentType)
ctx.body = prom.register.metrics()
}))
app.listen(8080, () => serverLog("listening on port 8080"))
updateDate()
setInterval(updateAllApps, 30 * 1000)
setInterval(updateDate, 30 * 60 * 1000)