firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.97k stars 915 forks source link

Add support for firealerts events in Eventarc emulator. #7355

Open GarrettBurroughs opened 1 week ago

GarrettBurroughs commented 1 week ago

Description

Added support for native events within the event arc emulator and

Scenarios Tested

In a firebase functions project, created the following function:

export const handlePerformanceAlert = onThresholdAlertPublished((thresholdAlert) => {
  console.log(thresholdAlert);
});

Ran the emulator suite with:

$ firebase emulators:start

Then used the following curl command to "publish" an event:

curl -X POST -d '
{"events": [
{
  "alerttype": "performance.threshold",
  "id": "",
  "source": "//firebasealerts.googleapis.com/projects/12345",
  "specVersion": "1.0",
  "appid": "",
  "time": "2024-05-31T18:07:36.470521Z",
  "type": "google.firebase.firebasealerts.alerts.v1.published",
  "project": "",
  "data": {
    "@type": "type.googleapis.com/google.events.firebase.firebasealerts.v1.AlertData",
    "createTime": "2024-05-31T18:07:36.470521Z",
    "endTime": "2024-05-31T18:07:36.470521Z",
    "payload": {
      "metricType": "duration",
      "appVersion": "1 (1.0.0)",
      "violationValue": 0.205629,
      "thresholdUnit": "seconds",
      "violationUnit": "seconds",
      "@type": "type.googleapis.com/google.events.firebase.firebasealerts.v1.FireperfThresholdAlertPayload",
      "numSamples": "200",
      "eventName": "custom-trace-3",
      "thresholdValue": 0.15,
      "eventType": "duration_trace",
      "conditionPercentile": 90,
      "investigateUri": ""
    }
  }
}
]}
' http://localhost:9299/google/publishEvents

To which the emulator suite properly responded by running the handlePerformanceAlert function and logging the event to the emulator logs page.