|
| 1 | +// '@google-cloud/functions-framework/build/src/functions' import is expected to be type-only so it's erased in the final .js file. |
| 2 | +// When TypeScript compiler is upgraded, use `import type` syntax to explicitly assert that we don't want to load a module here. |
| 3 | +import { HttpFunction } from '@google-cloud/functions-framework/build/src/functions'; |
| 4 | +import { captureException, flush, getCurrentHub, Handlers, startTransaction, withScope } from '@sentry/node'; |
| 5 | +import { logger, stripUrlQueryAndFragment } from '@sentry/utils'; |
| 6 | + |
| 7 | +import { addServerlessEventProcessor, getActiveDomain, WrapperOptions } from './general'; |
| 8 | + |
| 9 | +type Request = Parameters<HttpFunction>[0]; |
| 10 | +type Response = Parameters<HttpFunction>[1]; |
| 11 | +type ParseRequestOptions = Handlers.ParseRequestOptions; |
| 12 | + |
| 13 | +export interface HttpFunctionWrapperOptions extends WrapperOptions { |
| 14 | + parseRequestOptions: ParseRequestOptions; |
| 15 | +} |
| 16 | + |
| 17 | +export { Request, Response }; |
| 18 | + |
| 19 | +const { parseRequest } = Handlers; |
| 20 | + |
| 21 | +/** |
| 22 | + * Capture exception with additional request information. |
| 23 | + * |
| 24 | + * @param e exception to be captured |
| 25 | + * @param req incoming request |
| 26 | + * @param options request capture options |
| 27 | + */ |
| 28 | +function captureRequestError(e: unknown, req: Request, options: ParseRequestOptions): void { |
| 29 | + withScope(scope => { |
| 30 | + addServerlessEventProcessor(scope); |
| 31 | + scope.addEventProcessor(event => parseRequest(event, req, options)); |
| 32 | + captureException(e); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Wraps an HTTP function handler adding it error capture and tracing capabilities. |
| 38 | + * |
| 39 | + * @param fn HTTP Handler |
| 40 | + * @param options Options |
| 41 | + * @returns HTTP handler |
| 42 | + */ |
| 43 | +export function wrapHttpFunction( |
| 44 | + fn: HttpFunction, |
| 45 | + wrapOptions: Partial<HttpFunctionWrapperOptions> = {}, |
| 46 | +): HttpFunction { |
| 47 | + const options: HttpFunctionWrapperOptions = { |
| 48 | + flushTimeout: 2000, |
| 49 | + parseRequestOptions: {}, |
| 50 | + ...wrapOptions, |
| 51 | + }; |
| 52 | + return (req, res) => { |
| 53 | + const reqMethod = (req.method || '').toUpperCase(); |
| 54 | + const reqUrl = req.url && stripUrlQueryAndFragment(req.url); |
| 55 | + |
| 56 | + const transaction = startTransaction({ |
| 57 | + name: `${reqMethod} ${reqUrl}`, |
| 58 | + op: 'gcp.function.http', |
| 59 | + }); |
| 60 | + |
| 61 | + // We put the transaction on the scope so users can attach children to it |
| 62 | + getCurrentHub().configureScope(scope => { |
| 63 | + scope.setSpan(transaction); |
| 64 | + }); |
| 65 | + |
| 66 | + // We also set __sentry_transaction on the response so people can grab the transaction there to add |
| 67 | + // spans to it later. |
| 68 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access |
| 69 | + (res as any).__sentry_transaction = transaction; |
| 70 | + |
| 71 | + // functions-framework creates a domain for each incoming request so we take advantage of this fact and add an error handler. |
| 72 | + // BTW this is the only way to catch any exception occured during request lifecycle. |
| 73 | + getActiveDomain().on('error', err => { |
| 74 | + captureRequestError(err, req, options.parseRequestOptions); |
| 75 | + }); |
| 76 | + |
| 77 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 78 | + const _end = res.end; |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 80 | + res.end = function(chunk?: any | (() => void), encoding?: string | (() => void), cb?: () => void): void { |
| 81 | + transaction.setHttpStatus(res.statusCode); |
| 82 | + transaction.finish(); |
| 83 | + |
| 84 | + flush(options.flushTimeout) |
| 85 | + .then(() => { |
| 86 | + _end.call(this, chunk, encoding, cb); |
| 87 | + }) |
| 88 | + .then(null, e => { |
| 89 | + logger.error(e); |
| 90 | + }); |
| 91 | + }; |
| 92 | + |
| 93 | + return fn(req, res); |
| 94 | + }; |
| 95 | +} |
0 commit comments