Skip to content

Commit a81ace7

Browse files
abawchenma-zal
authored andcommitted
Add pm2_id as a part of notification title (#23)
* Add pm2_id as a part of notification title. * Show pm2_id only for those running in cluster mode.
1 parent 8a0ed77 commit a81ace7

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

index.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var globalMessageQueue = [];
2222

2323
/**
2424
* Sends immediately the message(s) to Slack's Incoming Webhook.
25-
*
25+
*
2626
* @param {Message[]) messages - List of messages, ready to send.
2727
* This list can be trimmed and concated base on module configuration.
2828
*/
@@ -49,7 +49,7 @@ function sendToSlack(messages) {
4949
};
5050

5151

52-
// Merge together all messages from same process and with same event
52+
// Merge together all messages from same process and with same event
5353
// Convert messages to Slack message's attachments
5454
payload.attachments = convertMessagesToSlackAttachments(mergeSimilarMessages(limitedCountOfMessages));
5555

@@ -62,7 +62,7 @@ function sendToSlack(messages) {
6262
.join(", ");
6363
}
6464

65-
// Group together all messages with same title.
65+
// Group together all messages with same title.
6666
// payload.attachments = groupSameSlackAttachmentTypes(payload.attachments);
6767

6868
// Add warning, if some messages has been suppresed
@@ -98,7 +98,7 @@ function sendToSlack(messages) {
9898
/**
9999
* Sends the message to Slack's Incoming Webhook.
100100
* If buffer is enabled, the message is added to queue and sending is postponed for couple of seconds.
101-
*
101+
*
102102
* @param {Message} message
103103
*/
104104
function scheduleSendToSlack(message) {
@@ -121,30 +121,32 @@ function scheduleSendToSlack(message) {
121121

122122
/**
123123
* Converts messages to json format, that can be sent as Slack message's attachments.
124-
*
124+
*
125125
* @param {Message[]) messages
126126
* @returns {SlackAttachment[]}
127127
*/
128128
function convertMessagesToSlackAttachments(messages) {
129129
return messages.reduce(function(slackAttachments, message) {
130-
130+
131131
// The default color for events should be green
132132
var color = commonColor;
133133
// If the event is listed in redEvents, set the color to red
134134
if (redEvents.indexOf(message.event) > -1) {
135135
color = redColor;
136136
}
137-
138-
var fallbackText = message.name + ' ' + message.event + (message.description ? ': ' + message.description.trim().replace(/[\r\n]+/g, ', ') : '');
137+
138+
var title = `${message.name} ${message.event}`;
139+
var description = (message.description || '').trim();
140+
var fallbackText = title + (description ? ': ' + description.replace(/[\r\n]+/g, ', ') : '');
139141
slackAttachments.push({
140142
fallback: escapeSlackText(fallbackText),
141143
color: color,
142-
title: escapeSlackText(message.name + ' ' + message.event),
143-
text: escapeSlackText(message.description ? message.description.trim() : ''),
144+
title: escapeSlackText(title),
145+
text: escapeSlackText(description),
144146
ts: message.timestamp,
145-
// footer: message.name,
147+
// footer: message.name,
146148
});
147-
149+
148150
return slackAttachments;
149151
}, []);
150152
}
@@ -153,7 +155,7 @@ function convertMessagesToSlackAttachments(messages) {
153155
/**
154156
* New PM2 is storing log messages with date in format "YYYY-MM-DD hh:mm:ss +-zz:zz"
155157
* Parses this date from begin of message
156-
*
158+
*
157159
* @param {string} logMessage
158160
* @returns {{description:string|null, timestamp:number|null}}
159161
*/
@@ -188,7 +190,7 @@ function parseIncommingLog(logMessage) {
188190

189191
/**
190192
* Merge together all messages from same process and with same event
191-
*
193+
*
192194
* @param {Messages[]} messages
193195
* @returns {Messages[]}
194196
*/
@@ -212,8 +214,19 @@ function mergeSimilarMessages(messages) {
212214
// ----- APP INITIALIZATION -----
213215

214216
// Initialize buffer configuration from PM config variables
215-
scheduler.config.buffer_seconds = Number.parseInt(conf.buffer_seconds);
216-
scheduler.config.buffer_max_seconds = Number.parseInt(conf.buffer_max_seconds);
217+
scheduler.config.buffer_seconds = Number.parseInt(conf.buffer_seconds);
218+
scheduler.config.buffer_max_seconds = Number.parseInt(conf.buffer_max_seconds);
219+
220+
/**
221+
* Get pm2 app display name.
222+
* If the app is running in cluster mode, id will append [pm_id] as the suffix.
223+
*
224+
* @param {object} process
225+
* @returns {string} name
226+
*/
227+
function parseProcessName(process) {
228+
return process.name + (process.exec_mode === 'cluster_mode' && process.instances > 1 ? `[${process.pm_id}]` : '');
229+
}
217230

218231
// Start listening on the PM2 BUS
219232
pm2.launchBus(function(err, bus) {
@@ -224,7 +237,7 @@ pm2.launchBus(function(err, bus) {
224237
if (data.process.name !== 'pm2-slack') {
225238
var parsedLog = parseIncommingLog(data.data);
226239
scheduleSendToSlack({
227-
name: data.process.name,
240+
name: parseProcessName(data.process),
228241
event: 'log',
229242
description: parsedLog.description,
230243
timestamp: parsedLog.timestamp,
@@ -239,7 +252,7 @@ pm2.launchBus(function(err, bus) {
239252
if (data.process.name !== 'pm2-slack') {
240253
var parsedLog = parseIncommingLog(data.data);
241254
scheduleSendToSlack({
242-
name: data.process.name,
255+
name: parseProcessName(data.process),
243256
event: 'error',
244257
description: parsedLog.description,
245258
timestamp: parsedLog.timestamp,
@@ -267,7 +280,7 @@ pm2.launchBus(function(err, bus) {
267280
// If it is instance of Error, use it. If type is unknown, stringify it.
268281
var description = (data.data && data.data.message) ? (data.data.code || '') + data.data.message : JSON.stringify(data.data);
269282
scheduleSendToSlack({
270-
name: data.process.name,
283+
name: parseProcessName(data.process),
271284
event: 'exception',
272285
description: description,
273286
timestamp: Math.floor(Date.now() / 1000),
@@ -294,7 +307,7 @@ pm2.launchBus(function(err, bus) {
294307

295308
}
296309
scheduleSendToSlack({
297-
name: data.process.name,
310+
name: parseProcessName(data.process),
298311
event: data.event,
299312
description: description,
300313
timestamp: Math.floor(Date.now() / 1000),
@@ -309,7 +322,7 @@ pm2.launchBus(function(err, bus) {
309322
/**
310323
* Escapes the plain text before sending to Slack's Incoming webhook.
311324
* @see https://api.slack.com/docs/message-formatting#how_to_escape_characters
312-
*
325+
*
313326
* @param {string} text
314327
* @returns {string}
315328
*/
@@ -320,7 +333,7 @@ function escapeSlackText(text) {
320333

321334
/**
322335
* @typedef {Object} SlackAttachment
323-
*
336+
*
324337
* @property {string} fallback
325338
* @property {string} title
326339
* @property {string} [color]

0 commit comments

Comments
 (0)