You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.7 KiB
130 lines
3.7 KiB
var express = require('express'); |
|
var app = express(); |
|
var mongoose = require('mongoose'); |
|
var Schema = mongoose.Schema; |
|
var database = 'mongodb://root:ruth@mongo:27017/admin'; |
|
var today = new Date(); |
|
var counter = 0; |
|
var Prometheus = require("prom-client"); |
|
|
|
const libCounter = new Prometheus.Counter({ |
|
name: 'lib_invocation_count', |
|
help: 'A simple counter for app access during runtime created with unofficial prometheus nodejs library' |
|
}); |
|
|
|
const libUptime = new Prometheus.Counter({ |
|
name: 'lib_upTime', |
|
help: 'uptime A counter of the application\'s uptime in seconds.' |
|
}) |
|
|
|
|
|
// const collectDefaultMetrics = Prometheus.collectDefaultMetrics; |
|
// Probe every 5th second. |
|
// collectDefaultMetrics({ timeout: 5000 }); |
|
|
|
// new schema model object based on the structure of what I want to put on MongoDB collection |
|
var testSchema = new Schema({ |
|
color: { |
|
type: String |
|
} |
|
},{ |
|
collection: 'test' |
|
}); |
|
|
|
// new object that will hold the data using model structure made above |
|
var colors = mongoose.model('colorName', testSchema); |
|
|
|
// Prometheus metrics |
|
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('/', (req, res)=>{ |
|
res.json([{message:'yes, your nodejs app is really running'}]); |
|
counter++; |
|
libCounter.inc(); |
|
}); |
|
|
|
app.get('/info', function(req, res){ |
|
colors.find({}).then(function (colors) { |
|
res.json(colors); |
|
}); |
|
}); |
|
|
|
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){ |
|
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, () => { |
|
console.log('Server running on port 3001'); |
|
}); |
|
|
|
// var today = new Date(); |
|
// |
|
// |
|
// setInterval(()=>{ |
|
// // console.log('process.uptime(): ' + process.uptime()); |
|
// var now = new Date(); |
|
// var passed = now - today; |
|
// console.log('# HELP uptime A summary of the application\'s uptime.') |
|
// console.log('# TYPE uptime summary') |
|
// console.log('uptime ' + passed); |
|
// }, 1000)
|
|
|