-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_receive_logs.js
67 lines (53 loc) · 1.7 KB
/
node_receive_logs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
process.env["SERVICE_NAME"] =
process.env["SERVICE_NAME"] ?? "node_receive_logs";
// ========================================================================
// Begin instrumentation
require("./node_tracer");
// End instrumentation
// ========================================================================
// ========================================================================
// Start subscriber
const amqp = require("amqplib");
const http = require("http");
async function connectAndConsume() {
const amqpUrl = process.env["RABBITMQ_URL"] ?? "amqp://localhost";
const connection = await amqp.connect(amqpUrl);
const channel = await connection.createChannel();
const exchange = "topic_logs";
await channel.assertExchange(exchange, "topic", { durable: true });
const { queue } = await channel.assertQueue("node", {
exclusive: false,
durable: true,
});
console.log(" [*] Waiting for logs. To exit press CTRL+C");
await channel.bindQueue(queue, exchange, "node.info");
await channel.consume(
queue,
(msg) => {
const headers = msg?.properties.headers;
console.log(
` [x] body: ${msg?.content.toString()}, headers: ${JSON.stringify(
headers
)}`
);
httpGet();
},
{ noAck: true }
);
}
function httpGet() {
let url = process.env["HTTP_LOGGER_URL"];
if (url) {
url = `${url}/${process.env["SERVICE_NAME"]}`;
http.get(url).on("error", (err) => {
console.error(`Error HTTP GET ${url}: `, err.message);
});
}
}
connectAndConsume().catch((error) => {
console.error(error);
process.exit(1);
});
// End subscriber
// ========================================================================