Skip to content

Instantly share code, notes, and snippets.

@manjeshpv
Last active February 23, 2021 05:59
Show Gist options
  • Save manjeshpv/106dc2e05d4b9f252dc9f3f41afccb59 to your computer and use it in GitHub Desktop.
Save manjeshpv/106dc2e05d4b9f252dc9f3f41afccb59 to your computer and use it in GitHub Desktop.
Without Restart Dynamically enable/disable debug in runtime via API [NOT IDEAL if u running pm2 cluster mode]
const debug = require('debug');
const log = debug('namespace1')
const express = require('express');
const app = express();
app.get('/', (req, res) => {
log('Homepage loading')
res.json({ title: 'Homepage' });
})
app.get('/enable', (req, res) => {
if(!req.query.tag) return res.json({
message: 'req.query.tag missing'
})
// - Enable Debug Namespace
debug.enable(req.query.tag);
log(`Enabled debug namespace: ${req.query.tag} in runtime`).
res.json({
status: debug.enabled(req.query.tag)
})
})
app.get('/disable', (req, res) => {
log('Disabling debug dynamically in runtime').
debug.disable();
res.json({ status: debug.enabled(req.query.tag || '*') })
})
app.listen(8090, () => {
console.log('express listeing at http://localhost:8090')
})
@manjeshpv
Copy link
Author

Docs

on first terminal

npm install debug express
node debug-runtime-dynamic-example.js

on second terminal

curl http://locahost:8090
# check first terminal

curl  http://locahost:8090/enable?tag=namespace1
curl http://locahost:8090
# check first console for logs generated by debug

curl  http://locahost:8090/disable?tag=namespace1
curl http://locahost:8090
# no logs should show in console

@manjeshpv
Copy link
Author

manjeshpv commented Feb 23, 2021

comments by @yogesum

  • Service is run in cluster mode. This will not work
  • For each one we need to hit
  • Better would be passing signal to each worker via command line or some endpoint which gets served by master worker

Another way to control is using flagr. That will provide us with way to update flag on Flagr Server UI and only authenticated user will be able to do it

@domil

@manjeshpv
Copy link
Author

@yogesum flagr solution looks like solving the issue of

  1. PM2 Cluster or Nodejs Cluster
  2. Multiple Physical app servers behind load balancer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment