From 32844cb3cf52814d985704a3a2caacce0cc2ee12 Mon Sep 17 00:00:00 2001 From: Samuel Papineau Date: Tue, 3 Oct 2023 22:12:04 -0400 Subject: [PATCH] Changed health endpoint for future internal metrics and added traceId --- index.ts | 10 ++++++---- src/health.ts | 11 ++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/index.ts b/index.ts index 11f2a1f..19e635a 100644 --- a/index.ts +++ b/index.ts @@ -31,7 +31,7 @@ Bun.serve<{key: string}>({ if ( req.method == "GET" ) { const { pathname } = new URL(req.url); if ( pathname === "/") return new Response(banner()) - if ( pathname === "/health") return toJSON(await checkHealth(db)); + if ( pathname === "/health") return toJSON(await checkHealth(true)); if ( pathname === "/metrics") return new Response(await prometheus.registry.metrics()); if ( pathname === "/moduleHash") return toJSON(sqlite.selectAll(db, "moduleHash")); if ( pathname === "/traceId") return toJSON(sqlite.selectAll(db, "traceId")); @@ -64,7 +64,7 @@ Bun.serve<{key: string}>({ return new Response("OK"); } // Get data from Substreams metadata - const { clock, manifest } = json; + const { clock, manifest, session } = json; const moduleHash = manifest?.moduleHash; // publish message to subscribers @@ -85,14 +85,16 @@ Bun.serve<{key: string}>({ prometheus.webhook_messages.inc(1); //Metrics for trace id - const traceId = "654b2e1fd43e8468863595baaad68627"; // TO-DO: get traceId from Substreams metadata + const traceId = session.traceId; + const traceTimestamp = session.timestamp; + console.log(traceId, traceTimestamp) if (traceId) { prometheus.trace_id.inc(1); } // Upsert moduleHash into SQLite DB sqlite.replace(db, "moduleHash", moduleHash, timestamp); - sqlite.replace(db, "traceId", traceId, timestamp); + sqlite.replace(db, "traceId", traceId, traceTimestamp); return new Response("OK"); } diff --git a/src/health.ts b/src/health.ts index bf1cd9a..ea2eaee 100644 --- a/src/health.ts +++ b/src/health.ts @@ -1,10 +1,7 @@ -import { Database } from "bun:sqlite"; -import * as sqlite from "./sqlite.js"; - -export async function checkHealth(db: Database) { - const total_trace_id = sqlite.selectAll(db, "traceId").length; - console.log('total_trace_id', total_trace_id) - if (total_trace_id) return {data: "OK", status: { status: 200 }}; +export async function checkHealth(internalData: boolean) { + if (internalData == true){ + return {data: "OK", status: { status: 200 }}; + } return {data: "Error: No connected webhooks", status: { status: 400 }}; }; \ No newline at end of file