diff --git a/docs/platforms/javascript/guides/gcp-functions/index.mdx b/docs/platforms/javascript/guides/gcp-functions/index.mdx index c52d028ba907ba..9934e0140270f5 100644 --- a/docs/platforms/javascript/guides/gcp-functions/index.mdx +++ b/docs/platforms/javascript/guides/gcp-functions/index.mdx @@ -1,6 +1,6 @@ --- title: Google Cloud Functions -description: "Learn how to use Sentry's Google Cloud Functions SDK." +description: "Learn how to manually set up Sentry in your Google Cloud Functions and capture your first errors." sdk: sentry.javascript.google-cloud-serverless fallbackGuide: javascript.node categories: @@ -11,29 +11,29 @@ categories: --- - This guide is for version 8.0.0 and up of `@sentry/google-cloud-serverless`. + This guide is for `@sentry/google-cloud-serverless` version `8.0.0` and up. + -## Features +## Step 1: Install -In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also collect and analyze performance profiles from real users with [profiling](/product/explore/profiling/). - -Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below. - -## Install +Choose the features you want to configure, and this guide will show you how: -Add `@sentry/google-cloud-serverless` as a dependency to `package.json`: + - +### Install the Sentry SDK + +Run the command for your preferred package manager to add the Sentry SDK to your application: -## Configure + -To set up Sentry for a Google Cloud Function: +## Step 2: Configure +Make sure to initialize Sentry at the top of your function code and wrap each function with the appropriate helper. Select the tab that matches the kind of function you're using (HTTP, Background, or CloudEvent): ```javascript {tabTitle:Http functions} const Sentry = require("@sentry/google-cloud-serverless"); @@ -61,7 +61,7 @@ Sentry.init({ }); exports.helloHttp = Sentry.wrapHttpFunction((req, res) => { - throw new Error("oh, hello there!"); + // your code }); ``` @@ -71,15 +71,19 @@ const Sentry = require("@sentry/google-cloud-serverless"); Sentry.init({ dsn: "___PUBLIC_DSN___", // ___PRODUCT_OPTION_START___ performance + + // Add Tracing by setting tracesSampleRate and adding integration + // Set tracesSampleRate to 1.0 to capture 100% of transactions + // We recommend adjusting this value in production + // Learn more at + // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate tracesSampleRate: 1.0, // ___PRODUCT_OPTION_END___ performance }); -exports.helloEvents = Sentry.wrapEventFunction( - (data, context, callback) => { - throw new Error("oh, hello there!"); - } -); +exports.helloEvents = Sentry.wrapEventFunction((data, context, callback) => { + // your code +}); ``` ```javascript {tabTitle:CloudEvent functions} @@ -88,35 +92,118 @@ const Sentry = require("@sentry/google-cloud-serverless"); Sentry.init({ dsn: "___PUBLIC_DSN___", // ___PRODUCT_OPTION_START___ performance + + // Add Tracing by setting tracesSampleRate and adding integration + // Set tracesSampleRate to 1.0 to capture 100% of transactions + // We recommend adjusting this value in production + // Learn more at + // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate tracesSampleRate: 1.0, // ___PRODUCT_OPTION_END___ performance }); -exports.helloEvents = Sentry.wrapCloudEventFunction( - (context, callback) => { - throw new Error("oh, hello there!"); +exports.helloEvents = Sentry.wrapCloudEventFunction((context, callback) => { + // your code +}); +``` + +## Step 3: Add Readable Stack Traces With Source Maps (Optional) + + + +## Step 4: Verify Your Setup + +Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project. + +### Issues + +First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add an intentional error in your function: + +```javascript {tabTitle:Http functions} +exports.helloHttp = Sentry.wrapHttpFunction((req, res) => { + throw new Error("Sentry Test Error - This is intentional!"); +}); +``` + +```javascript {tabTitle:Background functions} +exports.helloBackground = Sentry.wrapBackgroundFunction( + (data, context, callback) => { + throw new Error("Sentry Test Error - This is intentional!"); } ); ``` - +```javascript {tabTitle:CloudEvent functions} +exports.helloEvents = Sentry.wrapCloudEventFunction((context, callback) => { + throw new Error("Sentry Test Error - This is intentional!"); +}); +``` -If you are using Firestore or Cloud Functions for Firebase, you need to enable the [Firebase integration](/platforms/javascript/guides/node/configuration/integrations/firebase/). + +### Tracing +To test tracing, wrap your code in a span: - +```javascript {tabTitle:Http functions} +exports.helloHttp = Sentry.wrapHttpFunction(async (req, res) => { + await Sentry.startSpan( + { op: "test", name: "My First Test Transaction" }, + async () => { + // Simulate some work + await new Promise((resolve) => setTimeout(resolve, 100)); + } + ); + + res.status(200).send("Success!"); +}); +``` + +```javascript {tabTitle:Background functions} +exports.helloBackground = Sentry.wrapBackgroundFunction( + async (data, context) => { + await Sentry.startSpan( + { op: "test", name: "My First Test Transaction" }, + async () => { + // Simulate some work + await new Promise((resolve) => setTimeout(resolve, 100)); + } + ); + } +); +``` + +```javascript {tabTitle:CloudEvent functions} +exports.helloEvent = Sentry.wrapCloudEventFunction(async (context) => { + await Sentry.startSpan( + { op: "test", name: "My First Test Transaction" }, + async () => { + // Simulate some work + await new Promise((resolve) => setTimeout(resolve, 100)); + } + ); +}); +``` + + + +### View Captured Data in Sentry + +Now, head over to your project on [Sentry.io](https://sentry.io/) to view the collected data (it takes a couple of moments for the data to appear). + + + +## Next Steps + +At this point, you should have integrated Sentry into your Google Cloud Platform functions, which should already be sending data to your Sentry project. + +Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are: -Check out Sentry's [GCP sample apps](https://github.com/getsentry/examples/tree/master/gcp-cloud-functions/node) for detailed examples. Refer to the [JavaScript docs](/platforms/javascript/) for more configuration options. +- Continue to customize your configuration +- Learn how to manually capture errors +- Get familiar with [Sentry's product features](/product/) like tracing, insights, and alerts -## Behavior + -With the Google Cloud Functions integration enabled, the Node SDK will: +- Find various topics in Troubleshooting +- [Get support](https://sentry.zendesk.com/hc/en-us/) -- Automatically report all events from your Cloud Functions. -- Allows you to modify the transaction sample rate using . -- Issue reports automatically include: - - A link to the Stackdriver logs - - Function details - - sys.argv for the function - - Function execution time - - Function version -- Sentry holds the thread for up to two seconds to report errors. You can change flush time limit by defining a `flushTimeout` value in the handler options + diff --git a/platform-includes/getting-started-prerequisites/javascript.gcp-functions.mdx b/platform-includes/getting-started-prerequisites/javascript.gcp-functions.mdx new file mode 100644 index 00000000000000..b22b687efb7bc2 --- /dev/null +++ b/platform-includes/getting-started-prerequisites/javascript.gcp-functions.mdx @@ -0,0 +1,6 @@ +## Prerequisites + +You need: + +- A Sentry [account](https://sentry.io/signup/) and [project](/product/projects/) +- Your application up and running