add metrics to notify route
All checks were successful
Build and push image for doorman-homeassistant / docker (push) Successful in 32s
Build and push Doorman UI / API / docker (push) Successful in 1m28s
Build and push image for doorman-homeassistant / deploy-gitainer (push) Successful in 5s

This commit is contained in:
Martin Dimitrov 2025-02-02 13:01:45 -08:00
parent dd2d57c794
commit cc408ab0fe
3 changed files with 52 additions and 16 deletions

View File

@ -1,6 +1,9 @@
import { ServerlessEventObject, ServerlessFunctionSignature } from "@twilio-labs/serverless-runtime-types/types";
import { TwilioContext } from "../../../types/TwilioContext";
import { jsonMsgSuffix, sendMessageToUser } from "../../../utils/discord";
import { getMetricFromRegistry, withMetrics } from "../../../common/DoormanHandler";
import { NotifyMetrics, registerMetrics } from "../../../metrics/NotifyMetrics";
import { Counter, Summary } from "prom-client";
export interface NotifyRequest extends ServerlessEventObject {
@ -12,9 +15,11 @@ export interface NotifyRequest extends ServerlessEventObject {
json: string;
}
export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest> = async function(context, event, callback) {
export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest> = withMetrics('notify', async (context, event, callback, metricsRegistry) => {
const response = new Twilio.Response();
registerMetrics(metricsRegistry);
let users: string[];
let msgs: string[];
let jsons: string[];
@ -27,12 +32,22 @@ export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest>
jsons = JSON.parse(event.json);
console.log("after parsing", event.json);
const recordNotifyLatency = getMetricFromRegistry<Summary>(metricsRegistry, NotifyMetrics.DISCORD_LATENCY)
.startTimer();
promises = msgs.map((msg, i) =>
sendMessageToUser(
context,
users[i],
msg + jsonMsgSuffix(jsons[i])
).catch((e: Error) => console.error(e))
)
.then(() => {
recordNotifyLatency();
})
.catch((e: Error) => {
recordNotifyLatency();
getMetricFromRegistry<Counter>(metricsRegistry, NotifyMetrics.DISCORD_FAILURE).inc({ error: e.name }, 1);
})
);
} catch (e) {
console.error(e);
@ -45,10 +60,11 @@ export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest>
let timer = setTimeout(() => {
console.log("Ungraceful finish: running out of time");
getMetricFromRegistry<Counter>(metricsRegistry, NotifyMetrics.NOTIFY_TIMEOUT).inc(1);
callback(null, response);
}, 9500);
await Promise.all(promises);
clearTimeout(timer);
return callback(null, response);
};
});

View File

@ -0,0 +1,25 @@
import { Counter, Registry, Summary } from "prom-client";
export enum NotifyMetrics {
DISCORD_LATENCY = "DiscordLatency",
DISCORD_FAILURE = "DiscordFailure",
NOTIFY_TIMEOUT = "NotifyTimeout"
}
export const registerMetrics = (metricsRegistry: Registry) => {
metricsRegistry.registerMetric(new Summary({
name: NotifyMetrics.DISCORD_LATENCY,
help: "Latency for the Discord API",
}));
metricsRegistry.registerMetric(new Counter({
name: NotifyMetrics.DISCORD_FAILURE,
help: "Failure calling discord API",
labelNames: ['error'],
}));
metricsRegistry.registerMetric(new Counter({
name: NotifyMetrics.NOTIFY_TIMEOUT,
help: "Timeout before all notify calls completed",
}));
}

View File

@ -33,20 +33,15 @@ export const sendMessageToUser = async (
userId: string,
msg: string,
) => {
try {
const client = await getDiscordClient(context);
if (userCache[userId] === undefined) {
console.log("[UserCache] cache miss for", userId);
userCache[userId] = await client.users.fetch(userId);
} else {
console.log("[UserCache] cache hit for", userId);
}
const user = userCache[userId];
return user.send(msg);
} catch (e) {
console.log(e);
console.log(`Failed to send msg to ${userId}`);
const client = await getDiscordClient(context);
if (userCache[userId] === undefined) {
console.log("[UserCache] cache miss for", userId);
userCache[userId] = await client.users.fetch(userId);
} else {
console.log("[UserCache] cache hit for", userId);
}
const user = userCache[userId];
return user.send(msg);
};
export const jsonMsgSuffix = (jsonString: string = "") => {