Skip to content

Commit

Permalink
Simplifies logging to kafka by reducing the message size.
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolfgrauberger committed Oct 19, 2018
1 parent 6319447 commit a989cd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
13 changes: 8 additions & 5 deletions messaging/kafka.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ producer.connect().then((_) => {
console.log(error);
});

const createKafkaMessage = (req, res, par) => JSON.stringify({
request: req,
response: res,
const createKafkaMessage = (req, par) => JSON.stringify({
request: {
url: req.url,
method: req.method,
headers: req.headers
},
params: par
}, undefined, 3);

const logToKafka = (req, res, par) => {
const logToKafka = (req, par) => {
if (connected) {
const msg = createKafkaMessage(req, res, par);
const msg = createKafkaMessage(req, par);
producer.send('logging', JSON.stringify({
service_name: '1_NodeJs_1',
operation: req.method,
Expand Down
12 changes: 5 additions & 7 deletions routes/students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ router.addRoute('/students', (req, res, params) => {
if (req.method !== 'GET' && req.method !== 'POST') {
res.statusCode = 405;
res.end();
kafkalogger.logToKafka(req, res, params);
return;
}

res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');

if (req.method === 'GET') {
kafkalogger.logToKafka(req, params);
studentController.getStudents(res);
kafkalogger.logToKafka(req, res, params);
} else if (req.method === 'POST') {
kafkalogger.logToKafka(req, params);
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
req.on('end', () => {
studentController.createStudent(req.url, res, JSON.parse(body));
kafkalogger.logToKafka(req, res, params);
});
}
});
Expand All @@ -33,7 +32,6 @@ router.addRoute('/students/:id', (req, res, params) => {
if (req.method !== 'GET' && req.method !== 'PATCH' && req.method !== 'DELETE') {
res.statusCode = 405;
res.end();
kafkalogger.logToKafka(req, res, params);
return;
}

Expand All @@ -42,19 +40,19 @@ router.addRoute('/students/:id', (req, res, params) => {

if (req.method === 'GET') {
studentController.getStudent(res, params);
kafkalogger.logToKafka(req, res, params);
kafkalogger.logToKafka(req, params);
} else if (req.method === 'PATCH') {
kafkalogger.logToKafka(req, params);
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
req.on('end', () => {
studentController.updateStudent(res, params, body);
kafkalogger.logToKafka(req, res, params);
});
} else if (req.method === 'DELETE') {
kafkalogger.logToKafka(req, params);
studentController.deleteStudent(res, params);
kafkalogger.logToKafka(req, res, params);
}
});

Expand Down

0 comments on commit a989cd5

Please sign in to comment.