misc: initial working setup for metrics observabilit

This commit is contained in:
Sheen Capadngan
2024-06-05 21:46:10 +08:00
parent 4c06f134fb
commit 60895537a7
11 changed files with 1781 additions and 20 deletions
+21
View File
@@ -0,0 +1,21 @@
import opentelemetry from "@opentelemetry/api";
import fp from "fastify-plugin";
export const apiMetrics = fp(async (fastify) => {
const apiMeter = opentelemetry.metrics.getMeter("API");
const latencyHistogram = apiMeter.createHistogram("API latency", {
unit: "ms"
});
fastify.addHook("onResponse", async (request, reply) => {
const { method } = request;
const route = request.routerPath;
const { statusCode } = reply;
latencyHistogram.record(reply.elapsedTime, {
route,
method,
statusCode
});
});
});