Skip to content

Commit

Permalink
Working on metrics, corrected widget set bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Mar 30, 2024
1 parent decedaa commit 35809da
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
33 changes: 30 additions & 3 deletions src/app/extensions/fastify.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { TServiceParams } from "@digital-alchemy/core";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import plugin from "fastify-plugin";
import { hrtime } from "process";
import { collectDefaultMetrics } from "prom-client";

import { REQUEST_DURATION_HISTOGRAM } from "../helpers/metrics";
import {
HTTP_REQUEST_ERROR,
REQUEST_DURATION_HISTOGRAM,
} from "../helpers/metrics";

const LATE_CONFIG = -1;
const SECONDS_TO_MILLISECONDS = 1e3;
Expand All @@ -14,9 +18,29 @@ export function FastifyPlugins({ logger, lifecycle, fastify }: TServiceParams) {
logger.info({ name: "onPostConfig" }, `attaching matrix fastify plugin`);
const requests = new Map<FastifyRequest, ReturnType<typeof hrtime>>();

collectDefaultMetrics();

fastify.bindings.httpServer.register(
plugin(
async (server: FastifyInstance) => {
// ## onError
// emit log
// add to counter
server.addHook(
"onError",
(request: FastifyRequest, _: FastifyReply, error, done) => {
const url = request.routeOptions.url;
logger.error({ error, name: request.method }, `[%s]`, url);
HTTP_REQUEST_ERROR.labels({
method: request.method,
route: url,
}).inc();
done();
},
);

// ## onRequest
// register a start time for a request
server.addHook(
"onRequest",
(request: FastifyRequest, _: FastifyReply, done) => {
Expand All @@ -25,6 +49,9 @@ export function FastifyPlugins({ logger, lifecycle, fastify }: TServiceParams) {
},
);

// ## onResponse
// emit a log containing execution time of route
// add to histogram
server.addHook(
"onResponse",
(request: FastifyRequest, _: FastifyReply, done) => {
Expand All @@ -35,10 +62,10 @@ export function FastifyPlugins({ logger, lifecycle, fastify }: TServiceParams) {
seconds * SECONDS_TO_MILLISECONDS +
nano * NANOSECONDS_TO_MILLISECONDS;

const path = request.routeOptions.url;
const path = request.routeOptions.url || request.raw.url;
logger.debug(
{ name: request.method },
`{%s} - %sms`,
`[%s] - {%s}ms`,
path,
durationInMilliseconds,
);
Expand Down
2 changes: 1 addition & 1 deletion src/app/extensions/pixel.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function Pixel({ pi_matrix_app, pi_matrix }: TServiceParams) {
),
);
pi_matrix_app.instance.instance.sync();
MATRIX_RENDER.inc();
MATRIX_RENDER.labels({ type: "pixel" }).inc();
},
};
}
22 changes: 16 additions & 6 deletions src/app/extensions/widget.extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eachSeries, EMPTY, TServiceParams } from "@digital-alchemy/core";
import { eachSeries, EMPTY, NONE, TServiceParams } from "@digital-alchemy/core";
import dayjs from "dayjs";
import {
HorizontalAlignment,
Expand All @@ -20,7 +20,7 @@ import {
TextWidgetDTO,
UNLOAD_WIDGETS,
} from "../..";
import { MATRIX_RENDER } from "../helpers/metrics";
import { MATRIX_RENDER, MATRIX_RENDER_WIDGET_COUNT } from "../helpers/metrics";

export function Widget({ event, pi_matrix_app, logger }: TServiceParams) {
function renderCircle({
Expand All @@ -35,6 +35,7 @@ export function Widget({ event, pi_matrix_app, logger }: TServiceParams) {
.brightness(brightness)
.drawCircle(x, y, r);
}
const KNOWN_WIDGET_TYPES = new Set<string>();

function renderCountdown({
overflow = false,
Expand Down Expand Up @@ -180,7 +181,7 @@ export function Widget({ event, pi_matrix_app, logger }: TServiceParams) {
list,
async widget => await pi_matrix_app.widget.renderWidget(widget),
);
MATRIX_RENDER.inc();
MATRIX_RENDER.labels({ type: "widget" }).inc();
pi_matrix_app.instance.instance.sync();
} catch (error) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -227,11 +228,20 @@ export function Widget({ event, pi_matrix_app, logger }: TServiceParams) {
}
},

setWidgets(widgets: GenericWidgetDTO[]) {
setWidgets(incoming: GenericWidgetDTO[]) {
pi_matrix_app.render.renderMode = "widget";
event.emit(UNLOAD_WIDGETS);
widgets = widgets;
widget.initWidgets(widgets);
widget.widgets = incoming;
const counts = {} as Record<string, number>;
incoming.forEach(i => {
KNOWN_WIDGET_TYPES.add(i.type);
counts[i.type] ??= NONE;
counts[i.type]++;
});
KNOWN_WIDGET_TYPES.forEach(type =>
MATRIX_RENDER_WIDGET_COUNT.labels({ type }).set(counts[type] ?? NONE),
);
widget.initWidgets(incoming);
},

widgets: [
Expand Down
27 changes: 24 additions & 3 deletions src/app/helpers/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Counter, Histogram } from "prom-client";
import { Counter, Gauge, Histogram } from "prom-client";

/**
* Counter for tracking rejected authentication requests.
*/
export const MATRIX_RENDER = new Counter({
help: "Number of times the .sync method was used",
labelNames: ["type"] as const,
name: "pi_matrix_render_sync",
});
/**
* Counter for tracking rejected authentication requests.
*/
export const MATRIX_RENDER_WIDGET_COUNT = new Gauge({
help: "Widget counts the matrix is attempting to render",
labelNames: ["type"] as const,
name: "pi_matrix_widget_count",
});

/**
* Counter for tracking rejected authentication requests.
Expand All @@ -16,8 +25,20 @@ export const MATRIX_RENDER_IMMEDIATE = new Counter({
name: "pi_matrix_render_immediate",
});

/**
* Duration of HTTP requests in milliseconds
*/
export const REQUEST_DURATION_HISTOGRAM = new Histogram({
help: "Duration of HTTP requests in seconds",
help: "Duration of HTTP requests in milliseconds",
labelNames: ["method", "route"] as const,
name: "http_request_duration_milliseconds",
});

/**
* Quantity of errors by route
*/
export const HTTP_REQUEST_ERROR = new Counter({
help: "Quantity of errors by route",
labelNames: ["method", "route"] as const,
name: "http_request_duration_seconds",
name: "http_request_error",
});

0 comments on commit 35809da

Please sign in to comment.