-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added health endpoint and fixed headers
- Loading branch information
Showing
5 changed files
with
50 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { IncomingMessage, ServerResponse } from "http"; | ||
|
||
import * as prometheus from "./prometheus.js"; | ||
import * as http from "./http.js"; | ||
|
||
export function health() { | ||
http.server.on("request", async (req, res) => { | ||
if (!req.url) return; | ||
try { | ||
if (req.method == "GET") { | ||
if (req.url === "/health") { | ||
const messages = await getSingleMetric("substreams_sink_data_message") | ||
if (messages) return toText(res, "OK"); | ||
return toText(res, "no messages received yet", 503); | ||
} | ||
} | ||
} catch (err: any) { | ||
res.statusCode = 400; | ||
return res.end(err.message); | ||
} | ||
}); | ||
} | ||
|
||
async function getSingleMetric(name: string) { | ||
const metric = prometheus.registry.getSingleMetric(name); | ||
const get = await metric?.get(); | ||
return get?.values[0].value; | ||
} | ||
|
||
function toText(res: ServerResponse<IncomingMessage>, data: any, status = 200) { | ||
res.writeHead(status, { 'Content-Type': 'text/plain; charset=utf-8' }); | ||
return res.end(data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters