Skip to content

Commit

Permalink
chore: add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeraa committed Jan 3, 2025
1 parent b813a55 commit 50a799a
Show file tree
Hide file tree
Showing 3 changed files with 3,068 additions and 1,988 deletions.
4 changes: 4 additions & 0 deletions apps/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "tsc && devScript --copyOnly"
},
"dependencies": {
"@bmatei/apollo-prometheus-exporter": "^3.0.0",
"@sentry/node": "6.19.7",
"@sentry/tracing": "6.19.7",
"apollo-server-cache-redis": "3.3.1",
Expand All @@ -18,6 +19,7 @@
"archiver": "7.0.1",
"axios": "0.27.0",
"discord-api-types": "0.37.100",
"express": "^4.21.2",
"fastify": "3.29.5",
"graphql": "16.9.0",
"ioredis": "5.4.1",
Expand All @@ -28,11 +30,13 @@
"mongodb": "4.17.2",
"mongodb-caching": "1.1.0",
"p-event": "6.0.1",
"prom-client": "^15.1.3",
"source-map-support": "0.5.21",
"validator": "13.12.0"
},
"devDependencies": {
"@types/archiver": "6.0.2",
"@types/express": "^5.0.0",
"@types/lodash": "4.17.7",
"@types/node": "22.5.5",
"@types/validator": "13.12.2",
Expand Down
16 changes: 16 additions & 0 deletions apps/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import { resolvers as v3Resolvers } from "./v3/resolvers";
import { typeDefs as v3TypeDefs } from "./v3/typeDefinition";
import { resolvers as v4Resolvers } from "./v4/resolvers";
import { typeDefs as v4TypeDefs } from "./v4/typeDefinition";
import { createPrometheusExporterPlugin } from "@bmatei/apollo-prometheus-exporter";
import express from "express";
import { Counter, Registry } from "prom-client";

if (process.env.NODE_ENV !== "production")
require("dotenv").config({ path: "../../../.env" });
Expand Down Expand Up @@ -70,7 +73,16 @@ export interface Context {
transaction: Transaction;
}

const pathMetric = new Counter({
name: "premid_old_api_path",
help: "The path of the request",
labelNames: ["path"]
});

async function run() {
const registry = new Registry();
registry.registerMetric(pathMetric);
const expressApp = express();
redis.setMaxListeners(12);
redis.on("error", error => {
console.log(error);
Expand Down Expand Up @@ -103,6 +115,7 @@ async function run() {
introspection: true,
cache: baseRedisCache,
plugins: [
createPrometheusExporterPlugin({ app: expressApp, register: registry }),
sentryPlugin,
fastifyAppClosePlugin(app),
ApolloServerPluginDrainHttpServer({ httpServer: app.server }),
Expand Down Expand Up @@ -134,6 +147,7 @@ async function run() {
});

app.addHook("onRequest", async (req, reply) => {
pathMetric.labels(req.url).inc();
//@ts-ignore
req.responseTimeCalc = process.hrtime();
reply.headers({
Expand Down Expand Up @@ -248,6 +262,8 @@ async function run() {
.then(url => {
console.log(`🚀 API Listening on ${url}`);
});

expressApp.listen(2112, "0.0.0.0");
}

run();
Loading

0 comments on commit 50a799a

Please sign in to comment.