Skip to content

Commit

Permalink
feat: telemetry - loop duration & delay
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
bajtos committed Nov 21, 2024
1 parent 3e20055 commit 44b259a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
17 changes: 10 additions & 7 deletions observer/bin/spark-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as SparkImpactEvaluator from '@filecoin-station/spark-impact-evaluator'
import { ethers } from 'ethers'
import * as Sentry from '@sentry/node'
import timers from 'node:timers/promises'
import { InfluxDB } from '@influxdata/influxdb-client'
import { createInflux } from '../lib/telemetry.js'
import assert from 'node:assert/strict'

import { RPC_URL, rpcHeaders } from '../lib/config.js'
Expand All @@ -26,11 +26,7 @@ const provider = new ethers.JsonRpcProvider(fetchRequest, null, { polling: true

const ieContract = new ethers.Contract(SparkImpactEvaluator.ADDRESS, SparkImpactEvaluator.ABI, provider)

const influx = new InfluxDB({
url: 'https://eu-central-1-1.aws.cloud2.influxdata.com',
// spark-stats-observer-read
token: INFLUXDB_TOKEN
})
const { influx, recordTelemetry } = createInflux(INFLUXDB_TOKEN)

const influxQueryApi = influx.getQueryApi('Filecoin Station')

Expand All @@ -47,7 +43,14 @@ const loop = async (name, fn, interval) => {
}
const dt = Date.now() - start
console.log(`Loop "${name}" took ${dt}ms`)
await timers.setTimeout(interval - dt)
recordTelemetry(`loop_${name.replaceAll(' ', '_')}`, point => {
point.intField('interval_ms', interval)
point.intField('duration_ms', dt)
point.intField('delay_ms', interval - dt)
})
if (dt < interval) {
await timers.setTimeout(interval - dt)
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions observer/lib/telemetry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { InfluxDB, Point } from '@influxdata/influxdb-client'
import createDebug from 'debug'

const debug = createDebug('spark:observer:telemetry')

export const createInflux = token => {
const influx = new InfluxDB({
url: 'https://eu-central-1-1.aws.cloud2.influxdata.com',
// bucket permissions: spark-evaluate:read spark-observer:write
token
})
const writeClient = influx.getWriteApi(
'Filecoin Station', // org
'spark-observer', // bucket
'ms' // precision
)
setInterval(() => {
writeClient.flush().catch(console.error)
}, 10_000).unref()

return {
influx,

/**
* @param {string} name
* @param {(p: Point) => void} fn
*/
recordTelemetry: (name, fn) => {
const point = new Point(name)
fn(point)
writeClient.writePoint(point)
debug('%s %o', name, point)
}
}
}

export { Point }

0 comments on commit 44b259a

Please sign in to comment.