impress-2020/api/sendFeedback.js
Matchu fc6cb2dfdd Fix Honeycomb trace labels for API endpoints
I'm learning that top-level traces should use operation_name as well as name, because name is the low-level thing that every trace gets (including child traces like db queries and net requests)!

Also there was an incorrect label in one of these, and validPetPoses was missing it altogether
2021-04-23 12:31:50 -07:00

54 lines
1.5 KiB
JavaScript

const beeline = require("honeycomb-beeline")({
writeKey: process.env["HONEYCOMB_WRITE_KEY"],
dataset:
process.env["NODE_ENV"] === "production"
? "Dress to Impress (2020)"
: "Dress to Impress (2020, dev)",
serviceName: "impress-2020-gql-server",
});
import sendgridMail from "@sendgrid/mail";
sendgridMail.setApiKey(process.env.SENDGRID_API_KEY);
async function handle(req, res) {
const { content, email } = req.body;
if (!content) {
return res.status(400).send("Content must not be empty");
}
let contentSummary = content.trim();
if (contentSummary.length > 60) {
contentSummary = contentSummary.slice(0, 40) + "…";
}
const senderText = email || "<anonymous>";
console.info(`Sending from ${senderText}:\n${content}`);
const body = `${content}\n\nSent by ${senderText}`;
try {
await sendgridMail.send({
to: "matchu1993@gmail.com",
from: "impress-2020-feedback@openneo.net",
subject: `DTI feedback: ${contentSummary}`,
replyTo: email || undefined,
text: body,
});
} catch (e) {
console.error(e);
if (e.response && e.response.body && e.response.body.errors) {
console.error(e.response.body.errors);
}
return res.status(500).send("Error sending message, see logs");
}
return res.status(200).send();
}
export default async (req, res) => {
beeline.withTrace(
{ name: "api/sendFeedback", operation_name: "api/sendFeedback" },
() => handle(req, res)
);
};