Skip to content

need to return the promise and await for it to execute, or it doesn't work in stateless environments (cloudflare workers) #22

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 41 additions & 16 deletions src/cloudflare-workers/module-worker-treblle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
const { sendPayload } = require("./send-payload");
const { generateFieldsToMaskMap } = require("../maskFields");

async function sendToTreblle({
requestClone,
response,
apiKey,
projectId,
fieldsToMaskMap,
showErrors,
requestEndTime,
requestStartTime,
error,
}) {
try {
await sendPayload(requestClone, response ? response.clone() : null, {
apiKey,
projectId,
fieldsToMaskMap,
showErrors,
requestExecutionTime: requestEndTime - requestStartTime,
error,
});
} catch (err) {
// Just catch and log Treblle error - we do not want to crash app on Treblle's failure
console.error(
"Error occurred when sending payload to Treblle, have you set appropriate headers for your content type?",
err
);
}
}

const moduleWorkerTreblle = function ({
apiKey,
projectId,
Expand All @@ -25,22 +54,18 @@ const moduleWorkerTreblle = function ({
error = err;
}
const requestEndTime = Date.now();
try {
await sendPayload(requestClone, response ? response.clone() : null, {
apiKey,
projectId,
fieldsToMaskMap,
showErrors,
requestExecutionTime: requestEndTime - requestStartTime,
error,
});
} catch (err) {
// Just catch and log Treblle error - we do not want to crash app on Treblle's failure
console.error(
"Error occurred when sending payload to Treblle, have you set appropriate headers for your content type?",
err
);
}

context.waitUntil(sendToTreblle({
requestClone,
response,
apiKey,
projectId,
fieldsToMaskMap,
showErrors,
requestEndTime,
requestStartTime,
error,
}))

if (error) {
// Rethrow application errors
Expand Down
2 changes: 1 addition & 1 deletion src/cloudflare-workers/send-payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function sendPayload(
error,
});

sendPayloadToTreblleApi({ apiKey, trebllePayload, showErrors });
await sendPayloadToTreblleApi({ apiKey, trebllePayload, showErrors });
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions src/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function sendKoaPayloadToTreblle(
sendPayloadToTreblleApi({ apiKey, trebllePayload, showErrors });
}

function sendPayloadToTreblleApi({ apiKey, trebllePayload, showErrors }) {
async function sendPayloadToTreblleApi({ apiKey, trebllePayload, showErrors }) {
let f;
if (typeof fetch === "function") {
f = fetch;
Expand All @@ -294,7 +294,7 @@ function sendPayloadToTreblleApi({ apiKey, trebllePayload, showErrors }) {
return;
}

f("https://rocknrolla.treblle.com", {
return f("https://rocknrolla.treblle.com", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down