getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.77k stars 1.52k forks source link

feat(google-cloud-functions): add ability to pass HttpsOptions #12738

Closed mburger81 closed 1 week ago

mburger81 commented 1 week ago

Problem Statement

Looking at your documentation and trying it out, it looks like to me there is no way to pass functions HttpsOptions to the wrapped functions which should pass them to the google cloud functions itself, or maybe Im even Overlooking something

The documentation is saying this:

import * as Sentry from "@sentry/google-cloud-serverless"
Sentry.init({
  dsn: "xxxxxxx",
  integrations: [
    nodeProfilingIntegration(),
  ],
  // Performance Monitoring
  tracesSampleRate: 1.0, //  Capture 100% of the transactions

  // Set sampling rate for profiling - this is relative to tracesSampleRate
  profilesSampleRate: 1.0,
});

// Use wrapHttpFunction to instrument your http functions
exports.helloHttp = Sentry.wrapHttpFunction((req, res) => {
  /* Your function code */
});
.....

But we need to handle CORS and Headers, so we have for example something like this,

const opts: functions.https.HttpsOptions = {
  cors: true,
  region: "europe-west1"
};
exports.obtainusergroups = functions.https.onRequest(opts, async (request, response) => {})

So opts is passed to functions.https.onRequest, but this with Sentry is not doable which brings limitations

Solution Brainstorm

There should be a way to configure the native functions

chargome commented 1 week ago

Hi @mburger81 thanks for reaching out. Just to confirm: I'm assuming you are using firebase cloud functions?

mburger81 commented 1 week ago

@chargome yes we use firebase cloud functions, in v2 here the import part

import * as functions from "firebase-functions/v2";

chargome commented 1 week ago

@mburger81 Currently we do not support auto-instrumentation for firebase cloud functions (there is a note for that on the gcp docs).

You can however make use of manual instrumentation.

Hope that works for you!

mburger81 commented 1 week ago

@chargome I think I've somewhere read that performance data is not supported, but Uncaught Exceptions should be supported, or not?

If not, for what is wrapHttpFunction? I was supposing this is the wrapper around the firebase functions which handles uncaught exceptions?

So If I got your point, to caught exceptions we should just do something like this:

exports.obtainusergroups = functions.https.onRequest(opts, Sentry.wrapHttpFunction(async (request, response) => {
  try {
    // Your function logic here
    response.status(200).send({
      // Your response data here
    });
  } catch (error) {
    Sentry.captureException(error);
    response.status(500).send("An internal error occurred");
  }
}));
chargome commented 1 week ago

@mburger81 Theoretically wrapHttpFunction will auto instrument the serverless function for you and also handle uncaught errors. I haven't tested this myself yet, so if this does not work for you, you could still use @sentry/node and write your custom wrapper for your cloud functions. Feel free to ping me again if you have any questions, I'll open an issue for updating our docs in regards of cloud functions and hopefully provide a working integration for firebase anytime soon.

mburger81 commented 1 week ago

Hey @chargome thx for your response.

Honestly this wasn't the point, Ive not said its not working. Ive opened here a feature request. Your custom wrapper doesn't support passing HttpOptions, that was my feature request. Nothing more

I think its an important feature