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

Commit df0ec54

Browse files
committed
configure prom client
1 parent 13745bb commit df0ec54

File tree

9 files changed

+486
-48
lines changed

9 files changed

+486
-48
lines changed

docker-compose.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ services:
4242
volumes:
4343
- "redis-data:/data"
4444

45+
prometheus:
46+
image: prom/prometheus
47+
container_name: prometheus
48+
command:
49+
- "--config.file=/etc/prometheus/prometheus.yml"
50+
ports:
51+
- 9090:9090
52+
restart: unless-stopped
53+
volumes:
54+
- ./prometheus:/etc/prometheus
55+
- prom_data:/prometheus
56+
grafana:
57+
image: grafana/grafana
58+
container_name: grafana
59+
ports:
60+
- 3000:3000
61+
restart: unless-stopped
62+
environment:
63+
- GF_SECURITY_ADMIN_USER=admin
64+
- GF_SECURITY_ADMIN_PASSWORD=grafana
65+
volumes:
66+
- ./grafana:/etc/grafana/provisioning/datasources
67+
4568
volumes:
4669
postgres-data:
4770
redis-data:
71+
prom_data:

grafana/datasource.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: 1
2+
3+
datasources:
4+
- name: Prometheus
5+
type: prometheus
6+
url: http://prometheus:9090
7+
isDefault: true
8+
access: proxy
9+
editable: true

pnpm-lock.yaml

+63-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prometheus/prometheus.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
global:
2+
scrape_interval: 15s
3+
scrape_timeout: 10s
4+
evaluation_interval: 15s
5+
scrape_configs:
6+
- job_name: "substream-listener"
7+
honor_timestamps: true
8+
scrape_interval: 3s
9+
scrape_timeout: 1s
10+
metrics_path: /v1/metrics
11+
scheme: http
12+
static_configs:
13+
- targets:
14+
- host.docker.internal:4040

substream-listener/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"@substreams/manifest": "^0.15.0",
1111
"@substreams/node": "^0.6.2",
1212
"fets": "^0.8.0",
13+
"prom-client": "^15.1.2",
1314
"svix": "^1.21.0",
15+
"tslog": "^4.9.3",
1416
"uWebSockets.js": "github:uNetworking/uWebSockets.js#semver:^20",
1517
"viem": "^2.9.31"
1618
},

substream-listener/src/index.mts

+26-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ 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";
6+
import { spawn } from "node:child_process";
57

68
// Creating a new router
79
const router = createRouter({
@@ -69,44 +71,58 @@ const router = createRouter({
6971
if (startBlock == null) {
7072
return Response.json(
7173
{ message: "startBlock is required" },
72-
{ status: 400 },
74+
{ status: 400 }
7375
);
7476
}
7577

7678
if (!contractAddress) {
7779
return Response.json(
7880
{ message: "contractAddress is required" },
79-
{ status: 400 },
81+
{ status: 400 }
8082
);
8183
}
8284

8385
if (!substreamsToken) {
8486
return Response.json(
8587
{ message: "substreamsToken is required" },
86-
{ status: 400 },
88+
{ status: 400 }
8789
);
8890
}
8991

9092
if (!isAddress(contractAddress)) {
9193
return Response.json(
9294
{ message: "contractAddress is invalid" },
93-
{ status: 400 },
95+
{ status: 400 }
9496
);
9597
}
9698

9799
// Sending the webhook
98-
sendWebhook({
99-
appId,
100-
startBlock,
101-
contractAddress,
102-
token: substreamsToken,
103-
});
100+
await Promise.all([
101+
sendWebhook({
102+
appId,
103+
startBlock,
104+
contractAddress: contractAddress.toLowerCase(),
105+
token: substreamsToken,
106+
}),
107+
]);
104108

105109
// If the status code is not specified, it defaults to 200
106110
return Response.json({
107111
message: "Webhook registered",
108112
});
109113
},
114+
})
115+
.route({
116+
path: "/metrics",
117+
method: "GET",
118+
async handler() {
119+
const metrics = await registry.metrics();
120+
return new Response(metrics, {
121+
headers: {
122+
"Content-Type": registry.contentType,
123+
},
124+
});
125+
},
110126
});
111127

112128
App()

0 commit comments

Comments
 (0)