Docker containers running sample nodejs app among others tools for learning purposes.
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.
 
 
 
 

139 lines
4.2 KiB

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;
const Prometheus = require('prom-client');
var fs = require('file-system');
var marked = require('marked');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
// 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 prometheus nodejs library'
});
const libUptime = new Prometheus.Counter({
name: 'lib_upTime',
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);
// Prometheus Default Metrics collector
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 endpoint - Library
app.get('/metrics', function(req, res){
libUptime.inc(Math.floor(process.uptime()));
res.set('Content-Type', Prometheus.register.contentType)
res.end(Prometheus.register.metrics())
libUptime.reset();
});
// 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.use(express.json());
app.post('/token', function(request, response){
console.log(request.body); // your JSON
response.send(request.body); // echo the result back
});
app.get('/', (req, res)=>{
res.json([{message:'yes, your nodejs app is really running'}]);
counter++; // for prometheus invocation_count metric
libCounter.inc(); // for prometheus lib_invocation_count metric
});
app.get('/test', function(req, res) {
var path = '/app/README.md';
var file = fs.readFileSync(path, 'utf8');
res.send(marked(file.toString()));
});
app.get('/info', function(req, res){
colors.find({}).then(function (colors) {
res.json(colors);
});
});
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');
});
// app.get('/token/:user&:password', function(req, res){
// res.json(req.params);
// })
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)