-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gregorsulcer
committed
Dec 15, 2023
1 parent
1b3a522
commit ad24996
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { v4 as uuidd } from 'uuid'; | ||
import * as process from 'process'; | ||
import { getRabbitMQChannel } from '../rabbitmq/rabbitmq'; | ||
|
||
@Injectable() | ||
export class LoggerMiddleware implements NestMiddleware { | ||
use(req: Request, res: Response, next: NextFunction) { | ||
if (next) { | ||
next(); | ||
} | ||
|
||
const correlationIdHeader = 'X-Correlation-Id'; | ||
if (!req[correlationIdHeader]) { | ||
req.headers[correlationIdHeader] = uuidd(); | ||
} | ||
|
||
const timestamp = new Date().toISOString(); | ||
const urlString = `${req.protocol}://${req.get('host')}${req.originalUrl}`; | ||
const serviceName = '[discounts-service]'; | ||
const message = `<* ${req.method} ${req.originalUrl} *>`; | ||
|
||
res.on('finish', () => { | ||
const { statusCode } = res; | ||
|
||
const log = { | ||
timestamp, | ||
type: statusCode >= 500 ? 'ERROR' : statusCode >= 400 ? 'WARN' : 'INFO', | ||
correlationId: req.headers[correlationIdHeader], | ||
urlString, | ||
message, | ||
serviceName, | ||
}; | ||
|
||
const rabbitMQChannel = getRabbitMQChannel(); | ||
if (rabbitMQChannel) { | ||
rabbitMQChannel.publish( | ||
process.env.RABBITMQ_EXCHANGE, | ||
'', | ||
Buffer.from(JSON.stringify(log)), | ||
); | ||
console.log('Log message sent to RabbitMQ:', log); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as process from 'process'; | ||
import * as amqp from 'amqplib/callback_api'; | ||
|
||
const rabbitMQURL = process.env.RABBITMQ_URL; | ||
const rabbitMQExchange = process.env.EXCHANGE_NAME; | ||
const rabbitMQQueue = process.env.QUEUE_NAME; | ||
let rabbitMQChannel: amqp.Channel; | ||
|
||
export const setupRabbitMQ = () => { | ||
try { | ||
amqp.connect(rabbitMQURL, (error0, connection) => { | ||
if (error0) throw error0; | ||
|
||
connection.createChannel((error1, channel) => { | ||
if (error1) throw error1; | ||
|
||
channel.assertExchange(rabbitMQExchange, 'direct', { durable: true }); | ||
channel.assertQueue(rabbitMQQueue, { durable: true }); | ||
channel.bindQueue(rabbitMQQueue, rabbitMQExchange, ''); | ||
rabbitMQChannel = channel; | ||
console.log('Connected to RabbitMQ'); | ||
}); | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
|
||
export const getRabbitMQChannel = () => rabbitMQChannel; |