getsentry/examples

Outdated setup of sentry for cloud function

nwaughachukwuma opened this issue · 1 comments

Environment

Sentry: v7.11.1
Node: v16.13.1
Firebase functions: 3.21.2

Steps to Reproduce

  1. Integrate and init sentry like so:
// sentry.ts
import * as Sentry from '@sentry/serverless'

Sentry.GCPFunction.init({
  dsn: 'dsn-number',
  tracesSampleRate: 1.0,
  enabled: Boolean(process.env.FUNCTIONS_EMULATOR !== 'true'),
  environment: 'prod',
  release: build.env.VERSION,
})

export const GCPFunction = Sentry.GCPFunction
export const captureException = Sentry.captureException
export default Sentry
  1. Use it like so
import {GCPFunction} from './sentry.ts'

// like in the example - it didn't work.
export const forceErrorA = GCPFunction.wrapHttpFunction(((_, res) => {
  throw new Error('Forced error')
}))

// a working construct - doesn't report error
export const forceErrorB = GCPFunction.wrapHttpFunction(
  https.onRequest(async (_, res) => {
    throw new Error('Forced error')
  }),
)
  1. captureException from @sentry/serverless throws 'not a function' error

Expected Result

  1. to see an updated example on how to integrate @sentry/serveless on GCP cloud functions
  2. for the integration to actually work.

Actual Result

No error is reported or captured. I had to do the following to report error, which renders GCPFunction.wrapHttpFunction redundant :

// sentry.ts
import * as Serverless from '@sentry/serverless'
import * as Sentry from '@sentry/node'

Serverless.GCPFunction.init({
  dsn: 'dsn-number',
  tracesSampleRate: 1.0,
  enabled: Boolean(process.env.FUNCTIONS_EMULATOR !== 'true'),
  environment: 'prod',
  release: build.env.VERSION,
})

export const GCPFunction = Serverless.GCPFunction
export const captureException = Sentry.captureException
export default Sentry

then at use site

import {GCPFunction, captureException} from './sentry.ts'

export const forceError = GCPFunction.wrapHttpFunction(
  https.onRequest(async (_, res) => {
    captureException(new Error('Forced error'))
    res.status(400).send('Forced error')
  }),
)

Closing this issue for repo clean up - we are planning to deprecate this examples repo and will be adding new examples elsewhere.