Skip to content

Commit

Permalink
Added ping and trace id metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelpapineau committed Oct 2, 2023
1 parent 3f901df commit f987757
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ console.log(`Server listening on http://${HOSTNAME || "0.0.0.0"}:${PORT}`);
console.log("Verifying with PUBLIC_KEY", PUBLIC_KEY);

// Create SQLite DB
const db = new Database("./db.sqlite", {create: true}); // TO-DO as .env variable
const db = new Database("./sqlite/db.sqlite", {create: true}); // TO-DO as .env variable
sqlite.create(db, "moduleHash");
sqlite.create(db, "traceId");

Expand Down Expand Up @@ -85,8 +85,13 @@ Bun.serve<{key: string}>({
prometheus.webhook_module_hash.labels({moduleHash}).inc(1);
prometheus.webhook_messages.inc(1);

// Upsert moduleHash into SQLite DB
//Metrics for trace id
const traceId = "654b2e1fd43e8468863595baaad68627"; // TO-DO: get traceId from Substreams metadata
if (traceId) {
prometheus.trace_id.inc(1);
}

// Upsert moduleHash into SQLite DB
sqlite.replace(db, "moduleHash", moduleHash, timestamp);
sqlite.replace(db, "traceId", traceId, timestamp);

Expand All @@ -111,6 +116,7 @@ Bun.serve<{key: string}>({
// TO-DO: maybe to be removed??
// https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#pings_and_pongs_the_heartbeat_of_websockets
if ( message === "0x9" || message === "pong" ) {
prometheus.total_pings.inc(1);
console.log('ping', {key: ws.data.key, remoteAddress: ws.remoteAddress});
ws.pong();
if ( message == "0x9" ) ws.send("0xA");
Expand Down
2 changes: 2 additions & 0 deletions src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export const connected = registerCounter('connected', 'Total connected clients')
export const published_messages = registerCounter('published_messages', 'Total published messages');
export const bytes_published = registerCounter('bytes_published', 'Total bytes published');
export const disconnects = registerCounter('disconnects', 'Total disconnects');
export const total_pings = registerCounter('total_pings', 'Total pings');
export const trace_id = registerGauge('trace_id', 'Total trace ids')

0 comments on commit f987757

Please sign in to comment.