Skip to main content

[JavaScript] Can I monitor my Cloud Functions with Sentry?

Issue

I want to integrate Sentry into my Cloud Functions, including Firebase Cloud Functions and Firestore triggers. I saw a note in the Google Cloud Functions docs that said the JavaScript Serverless SDK does not support Cloud Functions for Firebase, and I am not sure which package or setup to use.

Applies To

  • All SaaS Customers and Self-Hosted Users

  • Cloud Functions for Firebase

  • Google Cloud Functions

  • @sentry/node (Firebase)

  • @sentry/google-cloud-serverless (Google Cloud Functions)

  • Error Monitoring

  • Tracing

Resolution

Which package you use depends on the runtime.

Cloud Functions for Firebase

Use @sentry/node. Do not use @sentry/google-cloud-serverless for Firebase.

  1. Install @sentry/node.

  2. Create an instrument.js (or instrument.cjs) file that calls Sentry.init.

  3. Require that file at the very top of your functions entry point, before any Firebase imports, so Sentry can instrument modules.

  4. Deploy and trigger a test error.

// instrument.js
const Sentry = require("@sentry/node");Sentry.init({
  dsn: "___PUBLIC_DSN___",
  tracesSampleRate: 1.0,
});

// index.js
require("./instrument");const { onRequest } = require("firebase-functions/https");
const { onDocumentCreated } = require("firebase-functions/firestore");exports.helloWorld = onRequest(async (request, response) => {
  response.send("Hello from Firebase!");
});exports.onUserCreated = onDocumentCreated(
  "users/{userId}",
  async (event) => {
    // Automatically instrumented
  },
);

firebaseIntegration is enabled by default in @sentry/node from version 10.22.0. It instruments Cloud Functions (HTTP, background, and event triggers) and Firestore operations. Supported ranges include firebase-functions >=6.0.0 <7.

If you catch errors yourself, still call Sentry.captureException(error) and await Sentry.flush(2000) before the function exits so events can send before the runtime freezes. For example:

const Sentry = require("@sentry/node");
const functions = require("firebase-functions");exports.sendEmail = functions.firestore
  .document("users/{userId}")
  .onCreate(async (snap, context) => {
    try {
      doSomething();
    } catch (error) {
      Sentry.captureException(error);
      await Sentry.flush(2000);
      throw error;
    }
  });

Rethrow after capture if you want the Cloud Function marked as failed.

Google Cloud Functions (not Firebase)

Use @sentry/google-cloud-serverless and follow the Google Cloud Functions guide.

Did this answer your question?