From 1b2a4dbaaef9c024b242d6a26210d93c544f6e36 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:36:47 +0200 Subject: [PATCH] expose own metrics --- index.js | 13 +++++++++++++ package.json | 2 +- prometheus/prometheus.yml | 4 ++-- run.js | 4 ++++ test/integration.js | 5 +++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e8b4977..68f9489 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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)) diff --git a/package.json b/package.json index c27345c..3146357 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" diff --git a/prometheus/prometheus.yml b/prometheus/prometheus.yml index 6c0c6cb..d9cef26 100644 --- a/prometheus/prometheus.yml +++ b/prometheus/prometheus.yml @@ -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: diff --git a/run.js b/run.js index 0d281c5..ee0e62b 100755 --- a/run.js +++ b/run.js @@ -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 = { @@ -59,6 +60,8 @@ async function main () { _forceFlushOnClientReady } = loadConfig() + promClient.collectDefaultMetrics() + const logger = pino({ level: logLevel }) logger.info('Starting up Prometheus DHT bridge') @@ -66,6 +69,7 @@ async function main () { const server = fastify({ logger }) const bridge = new PrometheusDhtBridge(dht, server, sharedSecret, { keyPairSeed, + ownPromClient: promClient, prometheusTargetsLoc, _forceFlushOnClientReady, serverLogLevel diff --git a/test/integration.js b/test/integration.js index 749c2b4..c0ba49f 100644 --- a/test/integration.js +++ b/test/integration.js @@ -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') @@ -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)