Skip to content

Commit cc56467

Browse files
authored
Add example code for Performance threshold alert (#1012)
Add function v2 example for performance alerts
1 parent 5af7d1f commit cc56467

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

2nd-gen/alerts-to-discord/functions/index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const {
2121
const {
2222
onNewTesterIosDevicePublished,
2323
} = require("firebase-functions/v2/alerts/appDistribution");
24+
const {
25+
onThresholdAlertPublished,
26+
} = require("firebase-functions/v2/alerts/performance");
2427
const logger = require("firebase-functions/logger");
2528
// [END v2import]
2629

@@ -139,3 +142,64 @@ UDID **${testerDeviceIdentifier}** for ${testerDeviceModelName}
139142
}
140143
});
141144
// [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]

2nd-gen/alerts-to-discord/functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"main": "index.js",
1717
"dependencies": {
1818
"firebase-admin": "^10.1.0",
19-
"firebase-functions": "^3.21.1",
19+
"firebase-functions": "^3.24.0",
2020
"node-fetch": "^2.6.7"
2121
},
2222
"devDependencies": {

pnpm-lock.yaml

Lines changed: 37 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)