Skip to content

Commit fc48b90

Browse files
author
Hamid Tavakoli
authored
Merge pull request #60 from topcoder-platform/tracing
Tracing
2 parents 4a73617 + bc01189 commit fc48b90

File tree

8 files changed

+732
-295
lines changed

8 files changed

+732
-295
lines changed

config/default.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,8 @@ module.exports = {
4848
API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || '/v5/email',
4949

5050
PAYLOAD_SENDGRID_TEMPLATE_KEY: process.env.PAYLOAD_SENDGRID_TEMPLATE_KEY || 'sendgrid_template_id',
51-
51+
//Tracing information
52+
APM_OTLP_TRACE_EXPORTER_URL: process.env.APM_OTLP_TRACE_EXPORTER_URL || '',
53+
APM_SERVICE_NAME: process.env.APM_SERVICE_NAME || 'tc-email-service',
54+
APM_TRACER_NAME: process.env.APM_TRACER_NAME || 'tc-email-service',
5255
};

connect/connectEmailServer.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const config = require('config');
66
const emailServer = require('../index');
77
const service = require('./service');
88
const logger = require('../src/common/logger');
9+
const { functionWrapper } = require('../src/common/wrapper');
910

1011
// set configuration for the server, see ../config/default.js for available config parameters
1112
// setConfig should be called before initDatabase and start functions
@@ -17,20 +18,23 @@ emailServer.setConfig({ LOG_LEVEL: config.LOG_LEVEL });
1718
// the topic is topic name,
1819
// the message is JSON event message,
1920
const handler = async (topic, message) => {
20-
let templateId = config.TEMPLATE_MAP[topic];
21-
templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId);
22-
if (!templateId) {
23-
return { success: false, error: `Template not found for topic ${topic}` };
24-
}
25-
26-
try {
27-
await service.sendEmail(templateId, message)
28-
return { success: true };
29-
} catch (err) {
30-
logger.error("Error occurred in sendgrid api calling:", err);
31-
return { success: false, error: err };
32-
}
21+
(await functionWrapper(async () => {
3322

23+
let templateId = config.TEMPLATE_MAP[topic];
24+
templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId);
25+
if (!templateId) {
26+
return { success: false, error: `Template not found for topic ${topic}` };
27+
}
28+
29+
try {
30+
await service.sendEmail(templateId, message)
31+
return { success: true };
32+
} catch (err) {
33+
logger.error("Error occurred in sendgrid api calling:", err);
34+
return { success: false, error: err };
35+
}
36+
37+
}, 'emailHandler'))(topic, message);
3438
};
3539

3640
// init all events

connect/service.js

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,57 @@ const logger = require('../src/common/logger');
1010
sgMail.setApiKey(config.SENDGRID_API_KEY);
1111

1212
const sendEmail = async (templateId, message) => { // send email
13+
(await functionWrapper(async () => {
1314

14-
let msg = {}
15-
const from = message.from ? message.from : config.EMAIL_FROM;
16-
const replyTo = message.replyTo ? message.replyTo : config.EMAIL_FROM;
17-
const substitutions = message.data;
18-
const categories = message.categories ? message.categories : [];
19-
const to = message.recipients;
20-
const cc = message.cc ? message.cc : [];
21-
const bcc = message.bcc ? message.bcc : [];
22-
const sendAt = message.sendAt ? message.sendAt : undefined;
23-
const personalizations = message.personalizations ? message.personalizations : undefined
24-
const attachments = message.attachments ? message.attachments : [];
15+
let msg = {}
16+
const from = message.from ? message.from : config.EMAIL_FROM;
17+
const replyTo = message.replyTo ? message.replyTo : config.EMAIL_FROM;
18+
const substitutions = message.data;
19+
const categories = message.categories ? message.categories : [];
20+
const to = message.recipients;
21+
const cc = message.cc ? message.cc : [];
22+
const bcc = message.bcc ? message.bcc : [];
23+
const sendAt = message.sendAt ? message.sendAt : undefined;
24+
const personalizations = message.personalizations ? message.personalizations : undefined
25+
const attachments = message.attachments ? message.attachments : [];
2526

26-
if (message.version && message.version == "v3") {
27-
msg = {
28-
to,
29-
templateId,
30-
dynamicTemplateData: substitutions,
31-
personalizations,
32-
from,
33-
replyTo,
34-
categories,
35-
cc,
36-
bcc,
37-
attachments,
38-
sendAt
39-
};
40-
} else {
41-
msg = {
42-
to,
43-
templateId,
44-
substitutions,
45-
substitutionWrappers: ['{{', '}}'],
46-
from,
47-
replyTo,
48-
categories,
49-
cc,
50-
bcc,
51-
};
52-
}
53-
logger.info(`Sending email with templateId: ${templateId} and message: ${JSON.stringify(msg)}`);
54-
try {
55-
const result = await sgMail.send(msg)
56-
logger.info(`Email sent successfully with result: ${JSON.stringify(result)}`);
57-
return result
58-
} catch (err) {
59-
logger.error(`Error occurred in sendgrid api calling: ${err}`);
60-
throw err
61-
}
27+
if (message.version && message.version == "v3") {
28+
msg = {
29+
to,
30+
templateId,
31+
dynamicTemplateData: substitutions,
32+
personalizations,
33+
from,
34+
replyTo,
35+
categories,
36+
cc,
37+
bcc,
38+
attachments,
39+
sendAt
40+
};
41+
} else {
42+
msg = {
43+
to,
44+
templateId,
45+
substitutions,
46+
substitutionWrappers: ['{{', '}}'],
47+
from,
48+
replyTo,
49+
categories,
50+
cc,
51+
bcc,
52+
};
53+
}
54+
logger.info(`Sending email with templateId: ${templateId} and message: ${JSON.stringify(msg)}`);
55+
try {
56+
const result = await sgMail.send(msg)
57+
logger.info(`Email sent successfully with result: ${JSON.stringify(result)}`);
58+
return result
59+
} catch (err) {
60+
logger.error(`Error occurred in sendgrid api calling: ${err}`);
61+
throw err
62+
}
63+
}, 'sendgridSendEmail'))(templateId, message);
6264
}
6365
module.exports = {
6466
sendEmail,

0 commit comments

Comments
 (0)