Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:metrics exposed #59

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 68 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
"@types/express": "^4.16.1",
"@types/mustache": "^0.8.32",
"@types/node": "^12.0.2",
"@types/request": "^2.48.1",
"body-parser": "^1.20.2",
"@types/request": "^2.48.12",
"axios": "1.6.8",
"body-parser": "^1.20.2",
"express": "^4.19.2",
"js-yaml": "^3.13.1",
"js-yaml": "^4.1.0",
"json-rules-engine": "^2.3.6",
"moment-timezone": "^0.5.31",
"mustache": "^3.0.1",
"notifme-sdk": "^1.14.1",
"pg": "^8.2.1",
"prom-client": "^15.1.2",
"reflect-metadata": "^0.1.13",
"typeorm": "0.3.17",
"winston": "^3.2.1"
Expand Down
58 changes: 54 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import { WebhookService } from './destination/destinationHandlers/webhookHandler
import { WebhookConfig } from './entities/webhookconfig';
import * as process from "process";
import bodyParser from 'body-parser';
import {collectDefaultMetrics, Registry, Counter, Histogram} from 'prom-client';


const app = express();
app.use(bodyParser.json({ limit: '10mb' }));

Expand Down Expand Up @@ -91,22 +94,69 @@ createConnection(dbOptions).then(async connection => {
logger.error("shutting down notifier due to un-successful database connection...")
process.exit(1)
});
// create a registry to hold metrics
const registry = new Registry()
// Initialize Prometheus metrics
collectDefaultMetrics({register:registry});

const requestCounter = new Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests',
registers: [registry],
labelNames: ['method', 'path', 'status'],
})

const httpRequestTimer = new Histogram({
name: 'http_request_duration_ms',
help: 'Duration of HTTP requests in ms',
labelNames: ['method', 'path', 'status'],
registers: [registry],
});
app.get('/', (req, res) => res.send('Welcome to notifier Notifier!'))

app.get('/health', (req, res) =>{
res.status(200).send("healthy")
requestCounter.labels(req.method, req.path, res.statusCode.toString()).inc()
const start = Date.now();
try{res.status(200).send("healthy")}
finally {
const responseTimeInMs = Date.now() - start;
httpRequestTimer.labels(req.method, req.route.path, res.statusCode.toString()).observe(responseTimeInMs);
}
})

app.get('/test', (req, res) => {
send();
requestCounter.labels(req.method, req.path, res.statusCode.toString()).inc()
const start = Date.now();
try{send();}
finally {
const responseTimeInMs = Date.now() - start;
httpRequestTimer.labels(req.method, req.route.path, res.statusCode.toString()).observe(responseTimeInMs);
}
res.send('Test!');
})

app.post('/notify', (req, res) => {
requestCounter.labels(req.method, req.path, res.statusCode.toString()).inc()
const start = Date.now();
logger.info("notifications Received")
notificationService.sendNotification(req.body)
res.send('notifications sent')
try{
notificationService.sendNotification(req.body)
res.send('notifications sent')
}finally {
const responseTimeInMs = Date.now() - start;
httpRequestTimer.labels(req.method, req.route.path, res.statusCode.toString()).observe(responseTimeInMs);
}
});




// Endpoint to expose metrics
app.get('/metrics', async (req, res) => {
const result = await registry.metrics()
res.send(result)
});



app.listen(3000, () => logger.info('Notifier app listening on port 3000!'))
Loading