-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtelemetry.js
54 lines (44 loc) · 1.29 KB
/
telemetry.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
import { InfluxDB, Point } from '@influxdata/influxdb-client'
const {
INFLUXDB_TOKEN
} = process.env
if (!INFLUXDB_TOKEN) {
console.warn('Warning: INFLUXDB_TOKEN was not provided by the environment')
}
const influx = new InfluxDB({
url: 'https://eu-central-1-1.aws.cloud2.influxdata.com',
token: INFLUXDB_TOKEN
})
const publishWriteClient = influx.getWriteApi(
'Filecoin Station', // org
'spark-publish', // bucket
'ns' // precision
)
const networkInfoWriteClient = influx.getWriteApi(
'Filecoin Station', // org
'spark-network-info', // bucket
's' // precision
)
// Add new write client for batch metrics
const batchMetricsWriteClient = influx.getWriteApi(
'Filecoin Station', // org
'spark-batch-metrics', // bucket
'ns' // precision
)
setInterval(() => {
publishWriteClient.flush().catch(console.error)
networkInfoWriteClient.flush().catch(console.error)
}, 10_000).unref()
const recordFn = (client, name, fn) => {
const point = new Point(name)
fn(point)
client.writePoint(point)
}
const recordPublishTelemetry = (name, fn) => recordFn(publishWriteClient, name, fn)
const recordNetworkInfoTelemetry = (name, fn) => recordFn(networkInfoWriteClient, name, fn)
export {
publishWriteClient,
networkInfoWriteClient,
recordPublishTelemetry,
recordNetworkInfoTelemetry
}