Firebase offers default Crashlytics alerts (see the
[alerting overview page](https://firebase.google.com/docs/crashlytics/alerts.md.txt)).
However, these default alerts and their notification channels may not be
sufficient for your needs.

In these cases, you can use Cloud Monitoring to send
**custom alerts to custom notification channels** based on Crashlytics data
in Cloud Logging, which is automatically routed to the `firebase-telemetry`
bucket for web apps, or exported to Cloud Logging for mobile apps (along with
optional Firebase sessions data).

To use Cloud Monitoring for custom alerts, you set up an *alerting policy*,
which describes the circumstances under which you want to be alerted and how you
want to be notified.

For example, if your crash-free rate goes below a specific threshold or your web
app error rate spikes, Cloud Monitoring can send a customized alert message
to a specific email address or post it to a third-party service, like Discord,
Slack, PagerDuty, or Jira. You can also fully customize the information sent in
the alert, like including helpful deep-links into the Firebase console or
company-specific troubleshooting information.

In the Google Cloud documentation, learn more about
[Cloud Monitoring and alerts](https://docs.cloud.google.com/monitoring/alerts),
including
[pricing](https://docs.cloud.google.com/monitoring/alerts#pricing).

### Prerequisites

1. Make sure your Firebase project is on the [pay-as-you-go Blaze pricing plan](https://firebase.google.com/pricing.md.txt).

2. Ensure your Crashlytics logs are available in Cloud Logging:

   - **For mobile apps:** [Set up export to Cloud Logging of Crashlytics data and (optionally) Firebase sessions data.](https://firebase.google.com/docs/crashlytics/cloud-logging-export.md.txt)
   - **For web apps:** Logs are automatically sent to the `firebase-telemetry` bucket (`firebasetelemetry.googleapis.com/events`) when you set up Crashlytics in your web app.
3. [Set up notification channels.](https://docs.cloud.google.com/monitoring/support/notification-options)

### Set up an alerting policy

The following are high-level instructions; for detailed instructions, see the
Google Cloud documentation (for example:
[Create metric-threshold alerting policies](https://docs.cloud.google.com/monitoring/alerts/using-alerting-ui)
or
[Use PromQL to create alerting policies](https://docs.cloud.google.com/monitoring/alerts/using-promql)).

As part of running queries, you can set up either a log-based alert or a
SQL-based alert:

- **Set up a log-based alert:**

  1. Run a query in
     [Logs Explorer](https://docs.cloud.google.com/logging/docs/view/logs-explorer-interface).

  2. Click the **Add alert** button that appears.

  3. Follow the on-screen instructions to add a log-based alert.

- **Set up a SQL-based alert:**

  1. Run a query in
     [Logs Analytics](https://docs.cloud.google.com/logging/docs/log-analytics#analytics).

  2. Click the **Add alert** button that appears.

  3. Follow the on-screen instructions to add a SQL-based alert.

Alternatively, you can set up alerts for your
[log-based metrics](https://firebase.google.com/docs/crashlytics/cloud-logging-work-with-data#use-logs-based-metrics)
by using the
[policy configuration tool](https://console.cloud.google.com/monitoring/alerting/policies/create)
in the Google Cloud console, where you can choose one of the following options:

- **Builder**:
  The console UI provides a guided workflow to build the alerting policy.

  <br />

  Example: Alert on web app error spikes

  <br />

  For web apps, you can create a log-based counter metric on the
  `firebase-telemetry` Cloud Logging bucket and configure a threshold alert
  using the Builder:
  1. In the Google Cloud console, go to **Logging** \> [**Log-based Metrics**](https://console.cloud.google.com/logs/metrics), and then click **Create Metric**.
  2. Select **Counter** as the metric type and name your metric (for example, `web_crashlytics_errors`).
  3. In the **Build filter** box, enter the following LQL query to count
     incoming web error records:

     ```text
     logName="projects/PROJECT_ID/logs/firebasetelemetry.googleapis.com%2Fevents"
     severity=ERROR
     ```
  4. Click **Create Metric**.

  5. From the newly created metric's **Actions** menu (three vertical dots),
     select **Create alert from metric**.

  6. Set the **Rolling window** (for example, `5 min`) and **Rolling window
     function** (`rate` or `count`), choose **Above threshold** as the
     condition type, and select your notification channels (such as email,
     Slack, PagerDuty, or webhooks).

  <br />

  <br />

- **PromQL**:
  You provide a PromQL query for your alerting policy

  <br />

  Example PromQL query

  <br />

  For example, say that you've created two log-based metrics for your
  exported Crashlytics and Firebase sessions data:
  - One metric named `firebase/crashlytics_events`
    with a label of `errorType`,
    and it's defined as:

    `logName="projects/PROJECT_ID/logs/firebasecrashlytics.googleapis.com%2Fevents"`
  - Another metric named `firebase/session_events`
    with a label of `eventType`,
    and it's defined as:

    `logName="projects/PROJECT_ID/logs/firebasecrashlytics.googleapis.com%2Fsession_events"`

  Here's an example PromQL query using these log-based metrics to alert when
  your crash-free drops below 70%:

  ```
  1 - (sum by (project_id) (rate({__name__="logging.googleapis.com/user/firebase/crashlytics_events", errorType="FATAL"}[5m])) /
  sum by (project_id) (rate({__name__="logging.googleapis.com/user/firebase/session_events", eventType="SESSION_START"}[5m]))
  ) < 0.7
  ```

  <br />

  <br />