add metrics to auth route
This commit is contained in:
parent
c424476f55
commit
dd2d57c794
@ -8,3 +8,9 @@ AWS_SECRET_ACCESS_KEY=
|
|||||||
|
|
||||||
# discord notifs
|
# discord notifs
|
||||||
DISCORD_BOT_TOKEN=
|
DISCORD_BOT_TOKEN=
|
||||||
|
|
||||||
|
# metrics
|
||||||
|
PUSHGATEWAY_URL=https://metrics.chromart.cc
|
||||||
|
STAGE=prod
|
||||||
|
PUSHGATEWAY_USER=doorman
|
||||||
|
PUSHGATEWAY_PW=doormanmetrics
|
||||||
@ -4,12 +4,15 @@
|
|||||||
|
|
||||||
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 { shouldBlockRequest, UserAgentHeader } from "../../../utils/blockUserAgent";
|
import { UserAgentHeader } from "../../../utils/blockUserAgent";
|
||||||
import { clearLockStatusCommand, createDDBClient, ddbItemToJSON, getDoorConfigCommand, getLockStatusCommand, isLockOpen, setLockStatusCommand } from "../../../utils/ddb";
|
import { clearLockStatusCommand, createDDBClient, ddbItemToJSON, getDoorConfigCommand, getLockStatusCommand, isLockOpen, setLockStatusCommand } from "../../../utils/ddb";
|
||||||
import { DoorConfig } from "../../../types/DoorConfig";
|
import { DoorConfig } from "../../../types/DoorConfig";
|
||||||
import { AuthMethod } from "../../../types/AuthMethod";
|
import { AuthMethod } from "../../../types/AuthMethod";
|
||||||
import { Lock } from "../../../types/Lock";
|
import { Lock } from "../../../types/Lock";
|
||||||
import { DoorStatus } from "../../../types/DoorStatus";
|
import { DoorStatus } from "../../../types/DoorStatus";
|
||||||
|
import { getMetricFromRegistry, withMetrics } from "../../../common/DoormanHandler";
|
||||||
|
import { AuthMetrics, registerMetrics } from "../../../metrics/AuthMetrics";
|
||||||
|
import { Counter } from "prom-client";
|
||||||
|
|
||||||
export interface AuthRequest extends ServerlessEventObject<{}, UserAgentHeader> {
|
export interface AuthRequest extends ServerlessEventObject<{}, UserAgentHeader> {
|
||||||
door?: string;
|
door?: string;
|
||||||
@ -18,13 +21,10 @@ export interface AuthRequest extends ServerlessEventObject<{}, UserAgentHeader>
|
|||||||
timeout?: string;
|
timeout?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handler: ServerlessFunctionSignature<TwilioContext, AuthRequest> = async function(context, event, callback) {
|
export const handler: ServerlessFunctionSignature<TwilioContext, AuthRequest> = withMetrics('auth', async (context, event, callback, metricsRegistry) => {
|
||||||
const response = new Twilio.Response();
|
const response = new Twilio.Response();
|
||||||
|
|
||||||
if (shouldBlockRequest(event)) {
|
registerMetrics(metricsRegistry);
|
||||||
response.setStatusCode(200);
|
|
||||||
return callback(null, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
let door = event.door;
|
let door = event.door;
|
||||||
let pin = event.key;
|
let pin = event.key;
|
||||||
@ -40,6 +40,7 @@ export const handler: ServerlessFunctionSignature<TwilioContext, AuthRequest> =
|
|||||||
const config: DoorConfig = ddbItemToJSON<DoorConfig>(ddbConfig);
|
const config: DoorConfig = ddbItemToJSON<DoorConfig>(ddbConfig);
|
||||||
|
|
||||||
if (!config) {
|
if (!config) {
|
||||||
|
getMetricFromRegistry<Counter>(metricsRegistry, AuthMetrics.DOOR_CONFIG_NOT_FOUND).inc({ door }, 1);
|
||||||
response.setStatusCode(404);
|
response.setStatusCode(404);
|
||||||
return callback(null, response);
|
return callback(null, response);
|
||||||
}
|
}
|
||||||
@ -62,6 +63,9 @@ export const handler: ServerlessFunctionSignature<TwilioContext, AuthRequest> =
|
|||||||
return callback(null, response);
|
return callback(null, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getMetricFromRegistry<Counter>(metricsRegistry, AuthMetrics.AUTH_METHOD)
|
||||||
|
.inc({ method }, 1);
|
||||||
|
|
||||||
const fingerprint = {
|
const fingerprint = {
|
||||||
method,
|
method,
|
||||||
userAgent: event.request.headers['user-agent'],
|
userAgent: event.request.headers['user-agent'],
|
||||||
@ -109,4 +113,4 @@ export const handler: ServerlessFunctionSignature<TwilioContext, AuthRequest> =
|
|||||||
|
|
||||||
await client.destroy();
|
await client.destroy();
|
||||||
return callback(null, response);
|
return callback(null, response);
|
||||||
};
|
});
|
||||||
|
|||||||
20
packages/doorman-api/src/metrics/AuthMetrics.ts
Normal file
20
packages/doorman-api/src/metrics/AuthMetrics.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { Counter, Registry, Summary } from "prom-client";
|
||||||
|
|
||||||
|
export enum AuthMetrics {
|
||||||
|
DOOR_CONFIG_NOT_FOUND = "DoorConfigNotFound",
|
||||||
|
AUTH_METHOD = "AuthMethod",
|
||||||
|
}
|
||||||
|
|
||||||
|
export const registerMetrics = (metricsRegistry: Registry) => {
|
||||||
|
metricsRegistry.registerMetric(new Counter({
|
||||||
|
name: AuthMetrics.DOOR_CONFIG_NOT_FOUND,
|
||||||
|
help: "Auth was attempted on a door that does not exist",
|
||||||
|
labelNames: ["door"],
|
||||||
|
}));
|
||||||
|
|
||||||
|
metricsRegistry.registerMetric(new Counter({
|
||||||
|
name: AuthMetrics.AUTH_METHOD,
|
||||||
|
help: "Which auth method was used",
|
||||||
|
labelNames: ["method"],
|
||||||
|
}));
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
import { EnvironmentVariables } from "@twilio-labs/serverless-runtime-types/types";
|
import { DoormanLambdaContext } from "../common/DoormanHandlerContext";
|
||||||
|
|
||||||
export interface TwilioContext extends EnvironmentVariables {
|
export interface TwilioContext extends DoormanLambdaContext {
|
||||||
AWS_ACCESS_KEY: string;
|
AWS_ACCESS_KEY: string;
|
||||||
AWS_SECRET_ACCESS_KEY: string;
|
AWS_SECRET_ACCESS_KEY: string;
|
||||||
DISCORD_BOT_TOKEN: string;
|
DISCORD_BOT_TOKEN: string;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user