Skip to content

Commit

Permalink
Reduce log verbosity on info
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Jul 28, 2024
1 parent d32a31d commit 3055941
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class PrometheusDhtBridge extends ReadyResource {
_forceFlushOnClientReady = false,
prometheusTargetsLoc = DEFAULT_PROM_TARGETS_LOC,
entryExpiryMs = 3 * 60 * 60 * 1000,
checkExpiredsIntervalMs = 60 * 60 * 1000
checkExpiredsIntervalMs = 60 * 60 * 1000,
serverLogLevel = 'warn'
} = {}) {
super()

Expand All @@ -39,7 +40,7 @@ class PrometheusDhtBridge extends ReadyResource {
this.server = server
this.server.get(
'/scrape/:alias/metrics',
{ logLevel: 'info' },
{ logLevel: serverLogLevel },
this._handleGet.bind(this)
)

Expand Down
24 changes: 15 additions & 9 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ const goodbye = require('graceful-goodbye')
function loadConfig () {
const config = {
prometheusTargetsLoc: process.env.DHT_PROM_PROMETHEUS_TARGETS_LOC || './prometheus/targets.json',
logLevel: process.env.DHT_PROM_LOG_LEVEL || 'info',
logLevel: (process.env.DHT_PROM_LOG_LEVEL || 'info').toLowerCase(),
httpPort: process.env.DHT_PROM_HTTP_PORT || 0,
httpHost: '127.0.0.1',
_forceFlushOnClientReady: process.env._DHT_PROM_FORCE_FLUSH || 'false' // Tests only
}

config.serverLogLevel = config.logLevel === 'debug'
? 'info'
: 'warn' // No need to log all metrics requests

try {
config.sharedSecret = idEnc.decode(idEnc.normalize(process.env.DHT_PROM_SHARED_SECRET))
} catch (e) {
Expand Down Expand Up @@ -51,6 +55,7 @@ async function main () {
httpPort,
httpHost,
keyPairSeed,
serverLogLevel,
_forceFlushOnClientReady
} = loadConfig()

Expand All @@ -62,7 +67,8 @@ async function main () {
const bridge = new PrometheusDhtBridge(dht, server, sharedSecret, {
keyPairSeed,
prometheusTargetsLoc,
_forceFlushOnClientReady
_forceFlushOnClientReady,
serverLogLevel
})

setupLogging(bridge, logger)
Expand Down Expand Up @@ -91,17 +97,19 @@ function setupLogging (bridge, logger) {
scrapeClient.on('connection-open', ({ uid, targetKey, peerInfo }) => {
logger.info(`Scraper for ${alias}->${idEnc.normalize(targetKey)} opened connection from ${idEnc.normalize(peerInfo.publicKey)} (uid: ${uid})`)
})
scrapeClient.on('connection-ignore', ({ uid }) => {
logger.info(`Scraper for ${alias} ignored connection (uid: ${uid})`)
})
scrapeClient.on('connection-close', ({ uid }) => {
logger.info(`Scraper for ${alias} closed connection (uid: ${uid})`)
})

scrapeClient.on('connection-error', ({ error, uid }) => {
logger.info(`Scraper for ${alias} connection error (uid: ${uid})`)
logger.info(error)
})

if (logger.level === 'debug') {
scrapeClient.on('connection-ignore', ({ uid }) => {
logger.debug(`Scraper for ${alias} ignored connection (uid: ${uid})`)
})
}
})

bridge.on('aliases-updated', (loc) => {
Expand All @@ -113,6 +121,7 @@ function setupLogging (bridge, logger) {
})

bridge.on('load-aliases-error', e => { // TODO: test
// Expected first time the service starts (creates it then)
logger.error('failed to load aliases file')
logger.error(e)
})
Expand All @@ -133,20 +142,17 @@ function setupLogging (bridge, logger) {
logger.info(`Alias request from ${idEnc.normalize(remotePublicKey)} to set ${alias}->${idEnc.normalize(targetPublicKey)} (uid ${uid})`)
}
)

bridge.aliasRpcServer.on(
'register-success', ({ uid, alias, targetPublicKey, updated }) => {
logger.info(`Alias success for ${alias}->${idEnc.normalize(targetPublicKey)}--updated: ${updated} (uid: ${uid})`)
}
)

// TODO: log IP address + rate limit
bridge.aliasRpcServer.on(
'alias-unauthorised', ({ uid, remotePublicKey, targetPublicKey, alias }) => {
logger.info(`Unauthorised alias request from ${idEnc.normalize(remotePublicKey)} to set alias ${alias}->${idEnc.normalize(targetPublicKey)} (uid: ${uid})`)
}
)

bridge.aliasRpcServer.on(
'register-error', ({ uid, error }) => {
logger.info(`Alias error: ${error} (${uid})`)
Expand Down
6 changes: 3 additions & 3 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const z32 = require('z32')
const BRIDGE_EXECUTABLE = path.join(path.dirname(__dirname), 'run.js')
const PROMETHEUS_EXECUTABLE = path.join(path.dirname(__dirname), 'prometheus', 'prometheus')

const DEBUG = false
const DEBUG = true
const DEBUG_PROMETHEUS = false

// Note: move this inside the test if we ever have >1 integration test
Expand Down Expand Up @@ -77,8 +77,8 @@ test('Integration test, happy path', async t => {
DHT_PROM_SHARED_SECRET: z32SharedSecret,
DHT_PROM_KEY_PAIR_SEED: idEnc.normalize(hypCrypto.randomBytes(32)),
DHT_PROM_BOOTSTRAP_PORT: testnet.bootstrap[0].port,
_DHT_PROM_FORCE_FLUSH: true

_DHT_PROM_FORCE_FLUSH: true,
DHT_PROM_LOG_LEVEL: 'debug'
}

const firstBridgeProc = spawn(
Expand Down

0 comments on commit 3055941

Please sign in to comment.