Skip to content

Commit d602014

Browse files
committed
add metrics on api functions
1 parent b8d2b65 commit d602014

File tree

5 files changed

+937
-368
lines changed

5 files changed

+937
-368
lines changed

metrics.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { StatsD } from "node-statsd";
2+
import { config } from "dotenv";
3+
4+
config(); // load env vars
5+
6+
const environment = process.env.NODE_ENV;
7+
const graphite = process.env.GRAPHITE_HOST;
8+
9+
if (graphite === null) throw new Error("Graphite host is not configured");
10+
11+
const options = {
12+
host: graphite,
13+
port: 8125,
14+
prefix: `${environment}.sprig.`
15+
};
16+
17+
const metrics = new StatsD(options);
18+
19+
export default metrics;

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
"@lezer/common": "^1.0.2",
2424
"@preact/signals": "^1.1.3",
2525
"@sendgrid/mail": "^7.7.0",
26-
"astro": "^2.0.4",
26+
"astro": "2.7.0",
27+
"dotenv": "^16.3.1",
2728
"esprima": "^4.0.1",
28-
"firebase-admin": "^11.5.0",
29+
"firebase-admin": "^11.10.1",
2930
"ms": "^2.1.3",
3031
"nanoid": "^4.0.1",
32+
"node-statsd": "^0.1.1",
3133
"preact": "^10.6.5",
3234
"react-icons": "^4.7.1",
3335
"rehype-external-links": "^2.0.1",

src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference path="../.astro/types.d.ts" />
12
/// <reference types="astro/client" />
23

34
interface ImportMetaEnv {

src/middleware.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import metrics from "../metrics";
2+
3+
export async function onRequest({ request }: any, next: () => any) {
4+
const url = request.url;
5+
6+
if (!url.includes("api")) return next();
7+
8+
const metricName = url.split("/").slice(3).join("_");
9+
const response = await next();
10+
11+
const metricKey = `${response.status}.${metricName}`;
12+
console.log(metricKey);
13+
metrics.increment(metricKey, 1);
14+
}

0 commit comments

Comments
 (0)