Skip to content

Commit

Permalink
expose own metrics (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote authored Aug 1, 2024
1 parent 694c206 commit f07b0d1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const DEFAULT_PROM_TARGETS_LOC = './targets.json'
class PrometheusDhtBridge extends ReadyResource {
constructor (dht, server, sharedSecret, {
keyPairSeed,
ownPromClient,
_forceFlushOnClientReady = false,
prometheusTargetsLoc = DEFAULT_PROM_TARGETS_LOC,
entryExpiryMs = 3 * 60 * 60 * 1000,
Expand Down Expand Up @@ -44,6 +45,18 @@ class PrometheusDhtBridge extends ReadyResource {
this._handleGet.bind(this)
)

ownPromClient = ownPromClient || null
if (ownPromClient) {
this.server.get(
'/metrics',
{ logLevel: serverLogLevel },
async function (req, reply) {
const metrics = await ownPromClient.register.metrics()
reply.send(metrics)
}
)
}

this.promTargetsLoc = prometheusTargetsLoc
this.aliasRpcServer = new AliasRpcServer(this.swarm, this.secret, this.putAlias.bind(this))

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"hypercore-crypto": "^3.4.2",
"hyperdht": "^6.15.1",
"newline-decoder": "^1.0.2",
"prom-client": "^15.1.2",
"standard": "^17.1.0",
"test-tmp": "^1.2.1",
"z32": "^1.1.0"
Expand All @@ -48,6 +47,7 @@
"hypercore-id-encoding": "^1.3.0",
"hyperswarm": "^4.7.15",
"pino": "^9.2.0",
"prom-client": "^15.1.3",
"protomux-rpc": "^1.5.2",
"ready-resource": "^1.1.1",
"safety-catch": "^1.0.2"
Expand Down
4 changes: 2 additions & 2 deletions prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This is an example config.

global:
scrape_interval: 1s
evaluation_interval: 1s
scrape_interval: 5s
evaluation_interval: 5s

# Source for relabeling approach: https://stackoverflow.com/questions/59866342/prometheus-dynamic-metrics-path
scrape_configs:
Expand Down
4 changes: 4 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const pino = require('pino')
const fastify = require('fastify')
const idEnc = require('hypercore-id-encoding')
const goodbye = require('graceful-goodbye')
const promClient = require('prom-client')

function loadConfig () {
const config = {
Expand Down Expand Up @@ -59,13 +60,16 @@ async function main () {
_forceFlushOnClientReady
} = loadConfig()

promClient.collectDefaultMetrics()

const logger = pino({ level: logLevel })
logger.info('Starting up Prometheus DHT bridge')

const dht = new HyperDHT({ bootstrap })
const server = fastify({ logger })
const bridge = new PrometheusDhtBridge(dht, server, sharedSecret, {
keyPairSeed,
ownPromClient: promClient,
prometheusTargetsLoc,
_forceFlushOnClientReady,
serverLogLevel
Expand Down
5 changes: 5 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const promClient = require('prom-client')
const DhtPromClient = require('dht-prom-client')
const HyperDHT = require('hyperdht')
const z32 = require('z32')
const axios = require('axios')

const BRIDGE_EXECUTABLE = path.join(path.dirname(__dirname), 'run.js')
const PROMETHEUS_EXECUTABLE = path.join(path.dirname(__dirname), 'prometheus', 'prometheus')
Expand Down Expand Up @@ -163,6 +164,10 @@ test('Integration test, happy path', async t => {

await tAliasReq

const res = await axios.get(`${bridgeHttpAddress}/metrics`)
t.is(res.status, 200, 'can scrape own metrics')
t.is(res.data.includes('nodejs_eventloop_lag_mean_seconds'), true, 'sanity check')

// 3) Setup prometheus
const promConfigFileLoc = path.join(tmpDir, 'prometheus.yml')
await writePromConfig(promConfigFileLoc, bridgeHttpAddress, promTargetsLoc)
Expand Down

0 comments on commit f07b0d1

Please sign in to comment.