Skip to content

Commit

Permalink
Changed health endpoint for future internal metrics and added traceId
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelpapineau committed Oct 4, 2023
1 parent 37add49 commit 32844cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
10 changes: 6 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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
Expand All @@ -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");
}
Expand Down
11 changes: 4 additions & 7 deletions src/health.ts
Original file line number Diff line number Diff line change
@@ -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 }};

};

0 comments on commit 32844cb

Please sign in to comment.