Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: log payload #94

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
be91ca9
feat: configure cluster filter in notification settings (#78)
gireesh-naidu Sep 17, 2024
bacc177
chore: upgrade express to 4.21.0
eshankvaish Sep 20, 2024
4a3f342
chore: upgrade body parser to 1.20.3
eshankvaish Sep 20, 2024
a1b9b2c
fix: add Mustache.escape for template parsing
eshankvaish Sep 20, 2024
9d8c020
Merge pull request #81 from devtron-labs/fix/dependabot
eshankvaish Sep 20, 2024
af165be
Merge branch 'main' into main-sync-to-develop
vivek-devtron Sep 24, 2024
6e10f02
Merge pull request #83 from devtron-labs/main-sync-to-develop
vivek-devtron Sep 24, 2024
464285b
changes to support vul for ci artifact with env ids
Shivam-nagar23 Oct 18, 2024
1334d0c
Merge branch 'main' into feat-vul-notification-for-env
Shivam-nagar23 Oct 18, 2024
1b813e2
mustache .escape removed
Shivam-nagar23 Oct 18, 2024
2648103
fix: use async await and remove for each loops for better synchronus …
arunjaindev Oct 22, 2024
8848b13
Merge branch 'develop' of github.com:devtron-labs/notifier into fix/a…
arunjaindev Oct 22, 2024
d84c079
feat: add metrics for success and failed notifications and endpoints
arunjaindev Oct 23, 2024
57f6e04
chore: add end of line
arunjaindev Oct 23, 2024
042170f
chore: add engine.run in try catch
arunjaindev Oct 24, 2024
f9a2332
feat: add metric counter on all routes
arunjaindev Oct 24, 2024
9717ef4
Merge pull request #88 from devtron-labs/feat-vul-notification-for-env
Shivam-nagar23 Oct 29, 2024
6a513a7
Merge branch 'develop' into fix/async-flow
eshankvaish Nov 12, 2024
b321f60
fix: vulnerability with cookie package
eshankvaish Nov 12, 2024
45accfe
fix: vulnerability with cookie package
eshankvaish Nov 12, 2024
0b9605d
Merge pull request #93 from devtron-labs/fix/dependabot-fixes
eshankvaish Nov 12, 2024
3713499
Merge branch 'develop' into fix/async-flow
arunjaindev Nov 13, 2024
f909707
chore: log payloaf for nats and rest requests
arunjaindev Nov 13, 2024
1619f75
chore: add logger in notify api
arunjaindev Nov 14, 2024
187ca2f
added log
vivek-devtron Nov 15, 2024
952ada6
console.log added
vivek-devtron Nov 15, 2024
359a254
chore: remove loggers
arunjaindev Nov 18, 2024
2bc0d6a
Merge pull request #91 from devtron-labs/fix/async-flow
arunjaindev Nov 18, 2024
6f48cb3
chore: add log for payload
arunjaindev Nov 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
"@types/request": "^2.48.1",
"axios": "^1.7.7",
"body-parser": "^1.20.3",
"express": "^4.21.0",
"express": "^4.21.1",
"js-yaml": "^3.13.1",
"json-rules-engine": "^2.3.6",
"moment-timezone": "^0.5.31",
"mustache": "^3.0.1",
"nats": "2.10.0",
"notifme-sdk": "^1.16.13",
"pg": "^8.2.1",
"prom-client": "^15.1.3",
"reflect-metadata": "^0.1.13",
"typeorm": "0.3.17",
"winston": "^3.2.1"
Expand Down
28 changes: 28 additions & 0 deletions src/common/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Counter, Histogram, register } from "prom-client"

export const successNotificationMetricsCounter = new Counter({
name: 'successfulNotifications',
help: 'Number of successful notifications',
})

export const failedNotificationMetricsCounter = new Counter({
name: 'failedNotifications',
help: 'Number of failed notifications',
})

export const httpRequestMetricsCounter = new Counter({
name: 'httpRequestsCounter',
help: 'Number of requests on http endpoints',
labelNames: ['method', 'endpoint', 'statusCode']
})

export const natsHistogram = new Histogram({
name: 'natsConsumerHistogram',
help: 'nats consumer duration histogram',
labelNames: ['streamName', 'consumerName']
})

register.registerMetric(successNotificationMetricsCounter)
register.registerMetric(failedNotificationMetricsCounter)
register.registerMetric(httpRequestMetricsCounter)
register.registerMetric(natsHistogram)
94 changes: 56 additions & 38 deletions src/destination/destinationHandlers/sesHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class SESService implements Handler {
this.mh = mh
}

handle(event: Event, templates: NotificationTemplates[], setting: NotificationSettings, configsMap: Map<string, boolean>, destinationMap: Map<string, boolean>): boolean {
async handle(event: Event, templates: NotificationTemplates[], setting: NotificationSettings, configsMap: Map<string, boolean>, destinationMap: Map<string, boolean>): Promise<boolean> {
let sesTemplate: NotificationTemplates = templates.find(t => {
return 'ses' == t.channel_type
})
Expand All @@ -65,7 +65,7 @@ export class SESService implements Handler {
this.sesConfig = null
for (const element of providersSet) {
if (element['dest'] === "ses") {
this.getDefaultConfig(providersSet, event, sesTemplate, setting, destinationMap, configsMap)
await this.getDefaultConfig(providersSet, event, sesTemplate, setting, destinationMap, configsMap)
break
}
}
Expand All @@ -82,7 +82,7 @@ export class SESService implements Handler {
from_email: config['from_email']
}
if(this.sesConfig && this.sesConfig.from_email){
providersSet.forEach(p => {
for (const p of providersSet) {
if (p['dest'] == "ses") {
let userId = p['configId']
let recipient = p['recipient']
Expand All @@ -93,19 +93,19 @@ export class SESService implements Handler {
configKey = p['dest'] + '-' + userId
}
if (!configsMap.get(configKey)) {
this.processNotification(userId, recipient, event, sesTemplate, setting, p, emailMap)
await this.processNotification(userId, recipient, event, sesTemplate, setting, p, emailMap)
configsMap.set(configKey, true)
}
}
});
};
}
} catch (error) {
this.logger.error('getDefaultConfig', error)
throw new CustomError("Unable to send ses notification",500);
}
}

private preparePaylodAndSend(event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string){
private async preparePayloadAndSend(event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string){
let sdk: NotifmeSdk = new NotifmeSdk({
channels: {
email: {
Expand All @@ -125,49 +125,67 @@ export class SESService implements Handler {
// let options = { allowUndefinedFacts: true }
let conditions: string = p['rule']['conditions'];
if (conditions) {
engine.addRule({conditions: conditions, event: event});
engine.run(event).then(e => {
this.sendNotification(event, sdk, sesTemplate.template_payload).then(result => {
this.saveNotificationEventSuccessLog(result, event, p, setting);
}).catch((error) => {
this.logger.error(error.message);
this.saveNotificationEventFailureLog(event, p, setting);
});
})
engine.addRule({ conditions: conditions, event: event });
try {
await engine.run(event);
const result = await this.sendNotification(
event,
sdk,
sesTemplate.template_payload
);
await this.saveNotificationEventSuccessLog(
result,
event,
p,
setting
);
} catch (error: any) {
this.logger.error(error.message);
await this.saveNotificationEventFailureLog(event, p, setting);
}
} else {
this.sendNotification(event, sdk, sesTemplate.template_payload).then(result => {
this.saveNotificationEventSuccessLog(result, event, p, setting);
}).catch((error) => {
this.logger.error(error.message);
this.saveNotificationEventFailureLog(event, p, setting);
});
try {
const result = await this.sendNotification(
event,
sdk,
sesTemplate.template_payload
);
await this.saveNotificationEventSuccessLog(
result,
event,
p,
setting
);
} catch (error: any) {
this.logger.error(error.message);
await this.saveNotificationEventFailureLog(event, p, setting);
}
}
}

private processNotification(userId: number, recipient: string, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
private async processNotification(userId: number, recipient: string, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
if(userId) {
this.usersRepository.findByUserId(userId).then(user => {
if (!user) {
this.logger.info('no user found for id - ' + userId)
this.logger.info(event.correlationId)
return
}
this.sendEmailIfNotDuplicate(user['email_id'], event, sesTemplate, setting, p, emailMap)
})
const user = await this.usersRepository.findByUserId(userId)
if (!user) {
this.logger.info('no user found for id - ' + userId)
this.logger.info(event.correlationId)
return
}
await this.sendEmailIfNotDuplicate(user['email_id'], event, sesTemplate, setting, p, emailMap)
}else{
if (!recipient) {
this.logger.error('recipient is blank')
return
}
this.sendEmailIfNotDuplicate(recipient, event, sesTemplate, setting, p, emailMap)
await this.sendEmailIfNotDuplicate(recipient, event, sesTemplate, setting, p, emailMap)
}
}

private sendEmailIfNotDuplicate(recipient : string, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
private async sendEmailIfNotDuplicate(recipient : string, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
if (!emailMap.get(recipient)) {
emailMap.set(recipient, true)
event.payload['toEmail'] = recipient
this.preparePaylodAndSend(event, sesTemplate, setting, p)
await this.preparePayloadAndSend(event, sesTemplate, setting, p)
} else {
this.logger.info('duplicate email filtered out')
}
Expand Down Expand Up @@ -205,17 +223,17 @@ export class SESService implements Handler {
}
}

private saveNotificationEventSuccessLog(result: any, event: Event, p: any, setting: NotificationSettings) {
private async saveNotificationEventSuccessLog(result: any, event: Event, p: any, setting: NotificationSettings) {
if (result["status"] == "error") {
this.saveNotificationEventFailureLog(event, p, setting)
await this.saveNotificationEventFailureLog(event, p, setting)
} else {
let eventLog = this.eventLogBuilder.buildEventLog(event, p.dest, true, setting);
this.eventLogRepository.saveEventLog(eventLog);
await this.eventLogRepository.saveEventLog(eventLog);
}
}

private saveNotificationEventFailureLog(event: Event, p: any, setting: NotificationSettings) {
private async saveNotificationEventFailureLog(event: Event, p: any, setting: NotificationSettings) {
let eventLog = this.eventLogBuilder.buildEventLog(event, p.dest, false, setting);
this.eventLogRepository.saveEventLog(eventLog);
await this.eventLogRepository.saveEventLog(eventLog);
}
}
Loading