Skip to content

Commit

Permalink
add metrics on api functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiasAurel committed Sep 25, 2023
1 parent b8d2b65 commit d602014
Show file tree
Hide file tree
Showing 5 changed files with 937 additions and 368 deletions.
19 changes: 19 additions & 0 deletions metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { StatsD } from "node-statsd";
import { config } from "dotenv";

config(); // load env vars

const environment = process.env.NODE_ENV;
const graphite = process.env.GRAPHITE_HOST;

if (graphite === null) throw new Error("Graphite host is not configured");

const options = {
host: graphite,
port: 8125,
prefix: `${environment}.sprig.`
};

const metrics = new StatsD(options);

export default metrics;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"@lezer/common": "^1.0.2",
"@preact/signals": "^1.1.3",
"@sendgrid/mail": "^7.7.0",
"astro": "^2.0.4",
"astro": "2.7.0",
"dotenv": "^16.3.1",
"esprima": "^4.0.1",
"firebase-admin": "^11.5.0",
"firebase-admin": "^11.10.1",
"ms": "^2.1.3",
"nanoid": "^4.0.1",
"node-statsd": "^0.1.1",
"preact": "^10.6.5",
"react-icons": "^4.7.1",
"rehype-external-links": "^2.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />

interface ImportMetaEnv {
Expand Down
14 changes: 14 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import metrics from "../metrics";

export async function onRequest({ request }: any, next: () => any) {
const url = request.url;

if (!url.includes("api")) return next();

const metricName = url.split("/").slice(3).join("_");
const response = await next();

const metricKey = `${response.status}.${metricName}`;
console.log(metricKey);
metrics.increment(metricKey, 1);
}
Loading

0 comments on commit d602014

Please sign in to comment.