Skip to content

Commit

Permalink
feat: [UDP-3222] graphana adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
sayinmehmet47 committed Aug 5, 2023
1 parent 6e3f79c commit b873658
Show file tree
Hide file tree
Showing 21 changed files with 1,976 additions and 24 deletions.
6 changes: 6 additions & 0 deletions backend/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request, Response } from 'express';
import 'express-async-errors';
const { metricsServer, updateMetrics } = require('./metrics');

import { json } from 'body-parser';

Expand Down Expand Up @@ -30,13 +31,18 @@ app.use(cors(corsOptions));
app.use('/api/books', books);
app.use('/api/user', user);
app.use('/api/messages', messages);
app.use(updateMetrics);

app.all('*', (req: Request, res: Response) => {
throw new NotFoundError();
});

app.use(errorHandler);

metricsServer.listen(3010, () => {
console.log('Prometheus Metrics server listening on http://localhost:3010');
});

if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
Expand Down
70 changes: 70 additions & 0 deletions backend/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { NextFunction } from 'express';

const express = require('express');
const morgan = require('morgan');
const { register, Counter, Gauge, Histogram } = require('prom-client');

const app = express();

// Customized Http Metrics (Optional)
const httpMetricsLabelNames = ['method', 'route', 'app'];

// Buckets of response time for each route grouped by seconds
const httpRequestDurationBuckets = new Histogram({
name: 'nodejs_http_response_time',
help: 'Response time of all requests',
labelNames: [...httpMetricsLabelNames, 'code'],
});

// Count of all requests - gets increased by 1
const totalHttpRequestCount = new Counter({
name: 'nodejs_http_total_count',
help: 'Total Requests',
labelNames: [...httpMetricsLabelNames, 'code'],
});

// Response time for each route's last request
const totalHttpRequestDuration = new Gauge({
name: 'nodejs_http_total_duration',
help: 'Response time of the Last Request',
labelNames: httpMetricsLabelNames,
});

app.use(morgan('dev'));

function updateMetrics(req: any, res: any, next: NextFunction) {
let startTime = new Date().valueOf();
res.addListener('finish', () => {
// console.log(req.method, req.route, res.statusCode);
let responseTime = new Date().valueOf() - startTime; // milliseconds
totalHttpRequestDuration
.labels(req.method, req.route.path, 'fibonacci-api')
.set(responseTime);
totalHttpRequestCount
.labels(req.method, req.route.path, 'fibonacci-api', res.statusCode)
.inc();
httpRequestDurationBuckets
.labels(req.method, req.route.path, 'fibonacci-api', res.statusCode)
.observe(responseTime);
});
next();
}

app.get(
'/metrics',
async (
req: { headers: { authorization: any } },
res: {
setHeader: (arg0: string, arg1: string) => void;
send: (arg0: any) => void;
}
) => {
res.setHeader('Content-Type', register.contentType);
res.send(await register.metrics());
}
);

module.exports = {
metricsServer: app,
updateMetrics,
};
78 changes: 77 additions & 1 deletion backend/package-lock.json

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

14 changes: 8 additions & 6 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"jsonwebtoken": "^9.0.0",
"mongodb": "^5.0.0",
"mongoose": "^6.5.1",
"morgan": "^1.10.0",
"node-cache": "^5.1.2",
"node-url-shortener": "^2.0.3",
"prom-client": "^14.2.0",
"validator": "^13.7.0"
},
"license": "MIT",
Expand All @@ -40,16 +42,16 @@
"@types/bcrypt": "^5.0.0",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.2",
"@types/validator": "^13.7.17",
"nodemon": "^2.0.12",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"@types/jest": "^29.5.2",
"@types/jsonwebtoken": "^9.0.2",
"@types/supertest": "^2.0.12",
"@types/validator": "^13.7.17",
"jest": "^29.5.0",
"mongodb-memory-server": "^8.12.2",
"nodemon": "^2.0.12",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0"
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
}
}
21 changes: 21 additions & 0 deletions backend/routes/api/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@ import { NotFoundError } from '../../errors/not-found-error';
import { ServerError } from '../../errors/server-error';
import { body } from 'express-validator';
import { validateRequest } from '../../middleware/validate-request';
import { Counter, Histogram } from 'prom-client';
const router = express.Router();
const NodeCache = require('node-cache');
const cache = new NodeCache();

const booksRequestCounter = new Counter({
name: 'api_books_requests_total',
help: 'Total number of /api/books requests',
});

const booksRequestDurationHistogram = new Histogram({
name: 'api_books_request_duration_ms',
help: 'Duration of /api/books requests in ms',
labelNames: ['status'],
});

router.get('/allBooks', async (req: Request, res: Response) => {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
booksRequestCounter.inc();
booksRequestDurationHistogram
.labels(res.statusCode.toString())
.observe(duration);
});

Books.find(
{},
(
Expand Down
57 changes: 57 additions & 0 deletions backend/routes/api/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { NextFunction } from 'express';

const express = require('express');
const morgan = require('morgan');
const {
collectDefaultMetrics,
register,
Counter,
Gauge,
Histogram,
} = require('prom-client');

const app = express();

// Customized Http Metrics (Optional)
const httpMetricsLabelNames = ['method', 'route', 'app'];

// Buckets of response time for each route grouped by seconds
const httpRequestDurationBuckets = new Histogram({
name: 'nodejs_http_response_time',
help: 'Response time of all requests',
labelNames: [...httpMetricsLabelNames, 'code'],
});

// Count of all requests - gets increased by 1
const totalHttpRequestCount = new Counter({
name: 'nodejs_http_total_count',
help: 'Total Requests',
labelNames: [...httpMetricsLabelNames, 'code'],
});

// Response time for each route's last request
const totalHttpRequestDuration = new Gauge({
name: 'nodejs_http_total_duration',
help: 'Response time of the Last Request',
labelNames: httpMetricsLabelNames,
});

app.use(morgan('dev'));

app.get(
'/metrics',
async (
req: { headers: { authorization: any } },
res: {
setHeader: (arg0: string, arg1: string) => void;
send: (arg0: any) => void;
}
) => {
res.setHeader('Content-Type', register.contentType);
res.send(await register.metrics());
}
);

module.exports = {
metricsServer: app,
};
2 changes: 1 addition & 1 deletion client/src/pages/AllBooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const AllBooks = () => {
};

if (isLoading || isFetching) {
console.log('loading');
console.log('loading!!');
return <Loading />;
}

Expand Down
Loading

0 comments on commit b873658

Please sign in to comment.