Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 790bfb8

Browse files
committed
feat: add prom metrics for webhook lister http requests
1 parent 4fd8338 commit 790bfb8

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

prometheus/prometheus.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ scrape_configs:
77
honor_timestamps: true
88
scrape_interval: 3s
99
scrape_timeout: 1s
10-
metrics_path: /v1/metrics
10+
metrics_path: /metrics
1111
scheme: http
1212
static_configs:
1313
- targets:
14-
- host.docker.internal:4040
14+
- host.docker.internal:10254

substream-listener/src/index.mts

+23-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { createRouter, Response } from "fets";
22
import { App } from "uWebSockets.js";
33
import { isAddress } from "viem";
44
import { sendWebhook } from "./send-webhook.mjs";
5-
import { registry } from "./prometheus.mjs";
5+
import {
6+
invalidHttpRequests,
7+
registry,
8+
successfulHttpRequests,
9+
} from "./prometheus.mjs";
610

711
// Creating a new router
812
const router = createRouter({
@@ -59,36 +63,50 @@ const router = createRouter({
5963
},
6064
},
6165
async handler(req) {
66+
const json = await req.json().catch(() => null);
67+
68+
if (!json) {
69+
invalidHttpRequests.inc();
70+
return Response.json(
71+
{ message: "Invalid JSON payload" },
72+
{ status: 400 },
73+
);
74+
}
75+
6276
// Extracting the appId and startBlock from the request
63-
const { appId, startBlock, contractAddress, substreamsToken } =
64-
await req.json();
77+
const { appId, startBlock, contractAddress, substreamsToken } = json;
6578

6679
if (!appId) {
80+
invalidHttpRequests.inc();
6781
return Response.json({ message: "appId is required" }, { status: 400 });
6882
}
6983

7084
if (startBlock == null) {
85+
invalidHttpRequests.inc();
7186
return Response.json(
7287
{ message: "startBlock is required" },
7388
{ status: 400 },
7489
);
7590
}
7691

7792
if (!contractAddress) {
93+
invalidHttpRequests.inc();
7894
return Response.json(
7995
{ message: "contractAddress is required" },
8096
{ status: 400 },
8197
);
8298
}
8399

84100
if (!substreamsToken) {
101+
invalidHttpRequests.inc();
85102
return Response.json(
86103
{ message: "substreamsToken is required" },
87104
{ status: 400 },
88105
);
89106
}
90107

91108
if (!isAddress(contractAddress)) {
109+
invalidHttpRequests.inc();
92110
return Response.json(
93111
{ message: "contractAddress is invalid" },
94112
{ status: 400 },
@@ -105,6 +123,8 @@ const router = createRouter({
105123
}),
106124
]);
107125

126+
successfulHttpRequests.inc();
127+
108128
// If the status code is not specified, it defaults to 200
109129
return Response.json({
110130
message: "Webhook registered",

substream-listener/src/prometheus.mts

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ function calculateHeadBlockTimeDrift(clock: Clock) {
2020
}
2121

2222
// Counters
23+
24+
export const invalidHttpRequests = new promClient.Counter({
25+
name: "substreams_sink_invalid_http_requests",
26+
help: "The number of invalid HTTP requests received",
27+
});
28+
29+
export const successfulHttpRequests = new promClient.Counter({
30+
name: "substreams_sink_successful_http_requests",
31+
help: "The number of successful HTTP requests received",
32+
});
33+
2334
export const substreams_sink_progress_message = new promClient.Counter({
2435
name: "substreams_sink_progress_message",
2536
help: "The number of progress message received",

0 commit comments

Comments
 (0)