@@ -21,6 +21,9 @@ const {
21
21
const {
22
22
onNewTesterIosDevicePublished,
23
23
} = require ( "firebase-functions/v2/alerts/appDistribution" ) ;
24
+ const {
25
+ onThresholdAlertPublished,
26
+ } = require ( "firebase-functions/v2/alerts/performance" ) ;
24
27
const logger = require ( "firebase-functions/logger" ) ;
25
28
// [END v2import]
26
29
@@ -139,3 +142,64 @@ UDID **${testerDeviceIdentifier}** for ${testerDeviceModelName}
139
142
}
140
143
} ) ;
141
144
// [END v2Alerts]
145
+
146
+ /**
147
+ * Function triggered by Firebase Performance Monitoring that publishes
148
+ * a message to Discord whenever a performance threshold alert is fired.
149
+ */
150
+ // [START v2PerformanceAlertTrigger]
151
+ exports . postperformancealerttodiscord = onThresholdAlertPublished (
152
+ async ( event ) => {
153
+ // [END v2PerformanceAlertTrigger]
154
+ // [START v2PerformanceEventPayload]
155
+ // construct a helpful message to send to Discord
156
+ const appId = event . appId ;
157
+ const {
158
+ eventName,
159
+ metricType,
160
+ eventType,
161
+ numSamples,
162
+ thresholdValue,
163
+ thresholdUnit,
164
+ conditionPercentile,
165
+ appVersion,
166
+ violationValue,
167
+ violationUnit,
168
+ investigateUri,
169
+ } = event . data . payload ;
170
+ const message = `
171
+ ⚠️ Performance Alert for ${ metricType } of ${ eventType } : **${ eventName } ** ⚠️
172
+
173
+ App id: ${ appId }
174
+ Alert condition: ${ thresholdValue } ${ thresholdUnit }
175
+ Percentile (if applicable): ${ conditionPercentile }
176
+ App version (if applicable): ${ appVersion }
177
+
178
+ Violation: ${ violationValue } ${ violationUnit }
179
+ Number of samples checked: ${ numSamples }
180
+
181
+ **Investigate more:** ${ investigateUri }
182
+ ` ;
183
+ // [END v2PerformanceEventPayload]
184
+
185
+ try {
186
+ // [START v2SendPerformanceAlertToDiscord]
187
+ const response = await postMessageToDiscord (
188
+ "Firebase Performance Bot" , message ) ;
189
+ if ( response . ok ) {
190
+ logger . info (
191
+ `Posted Firebase Performance alert ${ eventName } to Discord` ,
192
+ event . data . payload ,
193
+ ) ;
194
+ } else {
195
+ throw new Error ( response . error ) ;
196
+ }
197
+ // [END v2SendPerformanceAlertToDiscord]
198
+ } catch ( error ) {
199
+ logger . error (
200
+ `Unable to post Firebase Performance alert ${ eventName } to Discord` ,
201
+ error ,
202
+ ) ;
203
+ }
204
+ } ) ;
205
+ // [END v2Alerts]
0 commit comments