diff --git a/node/app.js b/node/app.js index 8e55e70..43c1471 100755 --- a/node/app.js +++ b/node/app.js @@ -1,28 +1,50 @@ -var express = require('express'); -var app = express(); -var mongoose = require('mongoose'); -var Schema = mongoose.Schema; -var database = 'mongodb://' + process.env.mongousr + ':' + process.env.mongopwd + '@mongo:27017/test'; -var today = new Date(); +const express = require('express'); +const app = express(); +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; +const database = 'mongodb://' + process.env.mongousr + ':' + process.env.mongopwd + '@mongo:27017/test'; +const today = new Date(); var counter = 0; -var Prometheus = require("prom-client"); +const Prometheus = require("prom-client"); + +// database connection (with retries) +const options = { + autoIndex: false, // Don't build indexes + reconnectTries:30, // Retry up to 30 times + reconnectInterval: 500, // Reconnect every 500ms + poolSize: 10, // Maintain up to 10 socket connections + // If not connected, return errors immediately rather than waiting for reconnect + bufferMaxEntries: 0, + useNewUrlParser: true +} + +const connectWithRetry = () => { + console.log('MongoDB connection with retry') + mongoose.connect(database, options).then(()=>{ + console.log('MongoDB is connected') + }).catch(err=>{ + console.log('MongoDB connection unsuccessful, retry after 5 seconds.') + setTimeout(connectWithRetry, 5000) + }) +} const libCounter = new Prometheus.Counter({ name: 'lib_invocation_count', - help: 'A simple counter for app access during runtime created with unofficial prometheus nodejs library' + help: 'A simple counter for app access during runtime created with prometheus nodejs library' }); const libUptime = new Prometheus.Counter({ name: 'lib_upTime', - help: 'uptime A counter of the application\'s uptime in seconds.' + help: 'uptime A counter of the application\'s uptime in seconds created with prometheus nodejs library.' }) console.log('mongousr: ', process.env.mongousr); console.log('mongopwd: ', process.env.mongopwd); -// const collectDefaultMetrics = Prometheus.collectDefaultMetrics; +// Prometheus Default Metrics collector +const collectDefaultMetrics = Prometheus.collectDefaultMetrics; // Probe every 5th second. -// collectDefaultMetrics({ timeout: 5000 }); +collectDefaultMetrics({ timeout: 5000 }); // new schema model object based on the structure of what I want to put on MongoDB collection var testSchema = new Schema({ @@ -36,34 +58,32 @@ var testSchema = new Schema({ // new object that will hold the data using model structure made above var colors = mongoose.model('colorName', testSchema); -// Prometheus metrics +// Prometheus metrics endpoint - Library app.get('/metrics', function(req, res){ - // var now = new Date(); - // var passedTime = now - today; - // res.writeHead(200, {'Content-Type':'text/plain'}); - // res.write('# HELP uptime A counter of the application\'s uptime in millisenconds.' + '\n'); - // res.write('# TYPE uptime counter' + '\n'); - // res.write('uptime ' + passedTime + '\n'); - // res.write('# HELP invocation_count A simple counter for app access during runtime' + '\n'); - // res.write('# TYPE invocation_count counter'+ '\n'); - // res.write('invocation_count ' + counter + '\n'); - // res.end(); libUptime.inc(Math.floor(process.uptime())); res.set('Content-Type', Prometheus.register.contentType) res.end(Prometheus.register.metrics()) - res.end(); libUptime.reset(); }); -app.get('/env', function(req, res){ - -}); - +// Prometheus metrics endpoint - Handmade +app.get('/metrics2', function(req, res){ + var now = new Date(); + var passedTime = now - today; + res.writeHead(200, {'Content-Type':'text/plain'}); + res.write('# HELP uptime A counter of the application\'s uptime in millisenconds.' + '\n'); + res.write('# TYPE uptime counter' + '\n'); + res.write('uptime ' + passedTime + '\n'); + res.write('# HELP invocation_count A simple counter for app access during runtime' + '\n'); + res.write('# TYPE invocation_count counter'+ '\n'); + res.write('invocation_count ' + counter + '\n'); + res.end(); +}) app.get('/', (req, res)=>{ res.json([{message:'yes, your nodejs app is really running'}]); - counter++; - libCounter.inc(); + counter++; // for prometheus invocation_count metric + libCounter.inc(); // for prometheus lib_invocation_count metric }); app.get('/info', function(req, res){ @@ -72,50 +92,13 @@ app.get('/info', function(req, res){ }); }); -app.get('/info/:name', function(req, res){ - colors.find({color: req.params.name}).then(function(colors){ - res.json(colors); - }); -}); - -app.get('/colors', function(req, res){ - colors.find({}).then(function (colors) { - res.json(colors); - }); -}); - -app.get('/colors/query/:name', function(req, res){ - res.send('color is ' + req.params.name); -}); - -app.post('/colors/add/:name', function(req, res){ +app.post('/info/add/:name', function(req, res){ var item = {color: req.params.name}; var data = new colors(item); data.save(); res.send('color ' + req.params.name + ' added to database'); }); -// database connection (with retries) -const options = { - autoIndex: false, // Don't build indexes - reconnectTries:30, // Retry up to 30 times - reconnectInterval: 500, // Reconnect every 500ms - poolSize: 10, // Maintain up to 10 socket connections - // If not connected, return errors immediately rather than waiting for reconnect - bufferMaxEntries: 0, - useNewUrlParser: true -} - -const connectWithRetry = () => { - console.log('MongoDB connection with retry') - mongoose.connect(database, options).then(()=>{ - console.log('MongoDB is connected') - }).catch(err=>{ - console.log('MongoDB connection unsuccessful, retry after 5 seconds.') - setTimeout(connectWithRetry, 5000) - }) -} - connectWithRetry(); app.listen(3001, () => {