add metrics to notify route
This commit is contained in:
parent
dd2d57c794
commit
cc408ab0fe
@ -1,6 +1,9 @@
|
|||||||
import { ServerlessEventObject, ServerlessFunctionSignature } from "@twilio-labs/serverless-runtime-types/types";
|
import { ServerlessEventObject, ServerlessFunctionSignature } from "@twilio-labs/serverless-runtime-types/types";
|
||||||
import { TwilioContext } from "../../../types/TwilioContext";
|
import { TwilioContext } from "../../../types/TwilioContext";
|
||||||
import { jsonMsgSuffix, sendMessageToUser } from "../../../utils/discord";
|
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 {
|
export interface NotifyRequest extends ServerlessEventObject {
|
||||||
@ -12,9 +15,11 @@ export interface NotifyRequest extends ServerlessEventObject {
|
|||||||
json: string;
|
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();
|
const response = new Twilio.Response();
|
||||||
|
|
||||||
|
registerMetrics(metricsRegistry);
|
||||||
|
|
||||||
let users: string[];
|
let users: string[];
|
||||||
let msgs: string[];
|
let msgs: string[];
|
||||||
let jsons: string[];
|
let jsons: string[];
|
||||||
@ -27,12 +32,22 @@ export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest>
|
|||||||
jsons = JSON.parse(event.json);
|
jsons = JSON.parse(event.json);
|
||||||
console.log("after parsing", event.json);
|
console.log("after parsing", event.json);
|
||||||
|
|
||||||
|
const recordNotifyLatency = getMetricFromRegistry<Summary>(metricsRegistry, NotifyMetrics.DISCORD_LATENCY)
|
||||||
|
.startTimer();
|
||||||
|
|
||||||
promises = msgs.map((msg, i) =>
|
promises = msgs.map((msg, i) =>
|
||||||
sendMessageToUser(
|
sendMessageToUser(
|
||||||
context,
|
context,
|
||||||
users[i],
|
users[i],
|
||||||
msg + jsonMsgSuffix(jsons[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) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@ -45,10 +60,11 @@ export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest>
|
|||||||
|
|
||||||
let timer = setTimeout(() => {
|
let timer = setTimeout(() => {
|
||||||
console.log("Ungraceful finish: running out of time");
|
console.log("Ungraceful finish: running out of time");
|
||||||
|
getMetricFromRegistry<Counter>(metricsRegistry, NotifyMetrics.NOTIFY_TIMEOUT).inc(1);
|
||||||
callback(null, response);
|
callback(null, response);
|
||||||
}, 9500);
|
}, 9500);
|
||||||
|
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
return callback(null, response);
|
return callback(null, response);
|
||||||
};
|
});
|
||||||
|
|||||||
25
packages/doorman-api/src/metrics/NotifyMetrics.ts
Normal file
25
packages/doorman-api/src/metrics/NotifyMetrics.ts
Normal 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",
|
||||||
|
}));
|
||||||
|
}
|
||||||
@ -33,20 +33,15 @@ export const sendMessageToUser = async (
|
|||||||
userId: string,
|
userId: string,
|
||||||
msg: string,
|
msg: string,
|
||||||
) => {
|
) => {
|
||||||
try {
|
const client = await getDiscordClient(context);
|
||||||
const client = await getDiscordClient(context);
|
if (userCache[userId] === undefined) {
|
||||||
if (userCache[userId] === undefined) {
|
console.log("[UserCache] cache miss for", userId);
|
||||||
console.log("[UserCache] cache miss for", userId);
|
userCache[userId] = await client.users.fetch(userId);
|
||||||
userCache[userId] = await client.users.fetch(userId);
|
} else {
|
||||||
} else {
|
console.log("[UserCache] cache hit for", userId);
|
||||||
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 user = userCache[userId];
|
||||||
|
return user.send(msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const jsonMsgSuffix = (jsonString: string = "") => {
|
export const jsonMsgSuffix = (jsonString: string = "") => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user