Created
September 3, 2021 02:23
-
-
Save nicwise/35a38c6c17cab6d01bfae7e17ced37ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import honeycomb from 'honeycomb-beeline'; | |
import {config} from '../config'; | |
if (config.enableHoneycomb && !config.isServerlessOffline) { | |
honeycomb({ | |
writeKey: config.honeycombWriteKey, | |
dataset: config.honeycombDataset, | |
serviceName: config.honeycombServiceName, | |
transmission: 'stdout', | |
presendHook: (ev: any) => { | |
const {data} = ev; | |
if (data.url) { | |
data.url = scrubUrl(data.url); | |
} | |
}, | |
}); | |
} else { | |
honeycomb({ | |
writeKey: '', | |
dataset: config.honeycombDataset, | |
serviceName: config.honeycombServiceName, | |
impl: 'mock', | |
transmission: 'stdout', | |
enabledInstrumentations: [], | |
}); | |
} | |
export const beeline = honeycomb(); | |
const scrubUrl = (url: string) => { | |
if (url.indexOf('key') === -1 && url.indexOf('token') === -1) { | |
return url; | |
} | |
return url.replace(/([?&])(key=|token=)([^&]*)(&|$)/, '$1$2secret_redacted$4'); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {SNSHandler} from 'aws-lambda/trigger/sns'; | |
import {config} from '../config'; | |
import {beeline} from '../shared/honeycombWrapper'; | |
import {parseSnsRecord} from './SesEvent'; | |
export const handler: SNSHandler = event => { | |
const trace = beeline.startTrace({ | |
name: config.honeycombServiceName, | |
}); | |
for (const record of event.Records) { | |
const eventSpan = beeline.startSpan({name: 'ses-event'}); | |
const {Sns} = record; | |
const message = parseSnsRecord(Sns.Message); | |
if (message) { | |
const {destination, messageId, sendingAccountId, source, sourceIp} = message.mail; | |
eventSpan?.addContext({mail: message.mail, destination, messageId, sendingAccountId, source, sourceIp}); | |
//beeline.addContext(....) should work but doesn't | |
switch (message.notificationType) { | |
case 'Delivery': | |
{ | |
const {processingTimeMillis, reportingMTA, remoteMtaIp, smtpResponse, recipients} = message.delivery; | |
eventSpan?.addContext({ | |
name: 'ses-event-delivered', | |
processingTimeMillis, | |
reportingMTA, | |
remoteMtaIp, | |
smtpResponse, | |
recipients, | |
delivery: message.delivery, | |
}); | |
} | |
break; | |
case 'Complaint': | |
{ | |
const {complainedRecipients, feedbackId, arrivalDate, complaintFeedbackType, userAgent} = message.complaint; | |
eventSpan?.addContext({ | |
name: 'ses-event-complaint', | |
complainedRecipients, | |
feedbackId, | |
arrivalDate, | |
complaintFeedbackType, | |
userAgent, | |
complaint: message.complaint, | |
}); | |
} | |
break; | |
case 'Bounce': | |
{ | |
const {bounceSubType, bounceType, bounceRecipients, feedbackId, remoteMtaIp, reportingMTA} = message.bounce; | |
eventSpan?.addContext({ | |
name: 'ses-event-bounce', | |
bounceSubType, | |
bounceType, | |
bounceRecipients, | |
feedbackId, | |
remoteMtaIp, | |
reportingMTA, | |
bounce: message.bounce, | |
}); | |
} | |
break; | |
default: | |
eventSpan?.addContext({ | |
name: 'ses-event-unknown', | |
msg: Sns.Message, | |
}); | |
} | |
} | |
if (eventSpan) beeline.finishSpan(eventSpan); | |
} | |
if (trace) { | |
beeline.finishTrace(trace); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment