Skip to content

Commit

Permalink
Add CF-Ray header value to Sentry errors if available (#10339)
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB authored Jul 9, 2024
1 parent e685b1a commit 9d31aa0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
14 changes: 13 additions & 1 deletion core/base-service/check-error-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const defaultErrorMessages = {
429: 'rate limited by upstream service',
}

const headersToInclude = ['cf-ray']

export default function checkErrorResponse(httpErrors = {}, logErrors = [429]) {
return async function ({ buffer, res }) {
let error
Expand All @@ -28,7 +30,17 @@ export default function checkErrorResponse(httpErrors = {}, logErrors = [429]) {
}

if (logErrors.includes(res.statusCode)) {
log.error(new Error(`${res.statusCode} calling ${res.requestUrl.origin}`))
const tags = {}
for (const headerKey of headersToInclude) {
const headerValue = res.headers[headerKey]
if (headerValue) {
tags[`header-${headerKey}`] = headerValue
}
}
log.error(
new Error(`${res.statusCode} calling ${res.requestUrl.origin}`),
tags,
)
}

if (error) {
Expand Down
6 changes: 5 additions & 1 deletion core/base-service/check-error-response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ describe('async error handler', function () {

context('when status is 429', function () {
const buffer = Buffer.from('some stuff')
const res = { statusCode: 429, requestUrl: new URL('https://example.com/') }
const res = {
statusCode: 429,
headers: { 'some-key': 'some-value' },
requestUrl: new URL('https://example.com/'),
}

it('throws InvalidResponse', async function () {
try {
Expand Down
6 changes: 4 additions & 2 deletions core/server/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ const log = (...msg) => {
console.log(d, ...msg)
}

const error = err => {
const error = (err, tags) => {
const d = date()
listeners.forEach(f => f(d, err))
Sentry.captureException(err)
Sentry.captureException(err, {
tags,
})
console.error(d, err)
}

Expand Down

0 comments on commit 9d31aa0

Please sign in to comment.