Skip to content

Tracing #60

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

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ module.exports = {
API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || '/v5/email',

PAYLOAD_SENDGRID_TEMPLATE_KEY: process.env.PAYLOAD_SENDGRID_TEMPLATE_KEY || 'sendgrid_template_id',

//Tracing information
APM_OTLP_TRACE_EXPORTER_URL: process.env.APM_OTLP_TRACE_EXPORTER_URL || '',
APM_SERVICE_NAME: process.env.APM_SERVICE_NAME || 'tc-email-service',
APM_TRACER_NAME: process.env.APM_TRACER_NAME || 'tc-email-service',
};
30 changes: 17 additions & 13 deletions connect/connectEmailServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config = require('config');
const emailServer = require('../index');
const service = require('./service');
const logger = require('../src/common/logger');
const { functionWrapper } = require('../src/common/wrapper');

// set configuration for the server, see ../config/default.js for available config parameters
// setConfig should be called before initDatabase and start functions
Expand All @@ -17,20 +18,23 @@ emailServer.setConfig({ LOG_LEVEL: config.LOG_LEVEL });
// the topic is topic name,
// the message is JSON event message,
const handler = async (topic, message) => {
let templateId = config.TEMPLATE_MAP[topic];
templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId);
if (!templateId) {
return { success: false, error: `Template not found for topic ${topic}` };
}

try {
await service.sendEmail(templateId, message)
return { success: true };
} catch (err) {
logger.error("Error occurred in sendgrid api calling:", err);
return { success: false, error: err };
}
(await functionWrapper(async () => {

let templateId = config.TEMPLATE_MAP[topic];
templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId);
if (!templateId) {
return { success: false, error: `Template not found for topic ${topic}` };
}

try {
await service.sendEmail(templateId, message)
return { success: true };
} catch (err) {
logger.error("Error occurred in sendgrid api calling:", err);
return { success: false, error: err };
}

}, 'emailHandler'))(topic, message);
};

// init all events
Expand Down
96 changes: 49 additions & 47 deletions connect/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,57 @@ const logger = require('../src/common/logger');
sgMail.setApiKey(config.SENDGRID_API_KEY);

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

let msg = {}
const from = message.from ? message.from : config.EMAIL_FROM;
const replyTo = message.replyTo ? message.replyTo : config.EMAIL_FROM;
const substitutions = message.data;
const categories = message.categories ? message.categories : [];
const to = message.recipients;
const cc = message.cc ? message.cc : [];
const bcc = message.bcc ? message.bcc : [];
const sendAt = message.sendAt ? message.sendAt : undefined;
const personalizations = message.personalizations ? message.personalizations : undefined
const attachments = message.attachments ? message.attachments : [];
let msg = {}
const from = message.from ? message.from : config.EMAIL_FROM;
const replyTo = message.replyTo ? message.replyTo : config.EMAIL_FROM;
const substitutions = message.data;
const categories = message.categories ? message.categories : [];
const to = message.recipients;
const cc = message.cc ? message.cc : [];
const bcc = message.bcc ? message.bcc : [];
const sendAt = message.sendAt ? message.sendAt : undefined;
const personalizations = message.personalizations ? message.personalizations : undefined
const attachments = message.attachments ? message.attachments : [];

if (message.version && message.version == "v3") {
msg = {
to,
templateId,
dynamicTemplateData: substitutions,
personalizations,
from,
replyTo,
categories,
cc,
bcc,
attachments,
sendAt
};
} else {
msg = {
to,
templateId,
substitutions,
substitutionWrappers: ['{{', '}}'],
from,
replyTo,
categories,
cc,
bcc,
};
}
logger.info(`Sending email with templateId: ${templateId} and message: ${JSON.stringify(msg)}`);
try {
const result = await sgMail.send(msg)
logger.info(`Email sent successfully with result: ${JSON.stringify(result)}`);
return result
} catch (err) {
logger.error(`Error occurred in sendgrid api calling: ${err}`);
throw err
}
if (message.version && message.version == "v3") {
msg = {
to,
templateId,
dynamicTemplateData: substitutions,
personalizations,
from,
replyTo,
categories,
cc,
bcc,
attachments,
sendAt
};
} else {
msg = {
to,
templateId,
substitutions,
substitutionWrappers: ['{{', '}}'],
from,
replyTo,
categories,
cc,
bcc,
};
}
logger.info(`Sending email with templateId: ${templateId} and message: ${JSON.stringify(msg)}`);
try {
const result = await sgMail.send(msg)
logger.info(`Email sent successfully with result: ${JSON.stringify(result)}`);
return result
} catch (err) {
logger.error(`Error occurred in sendgrid api calling: ${err}`);
throw err
}
}, 'sendgridSendEmail'))(templateId, message);
}
module.exports = {
sendEmail,
Expand Down
Loading