Skip to content

Commit 4350666

Browse files
feat: add Prometheus counter for HTTP status codes (#2117)
* feat: add Prometheus counter for crawler status codes Add a new Prometheus metric to track HTTP status codes encountered during crawling operations. This helps monitor crawler health and identify patterns in response codes (e.g., 200 OK, 404 Not Found, etc.). Changes: - Add crawlerStatusCodeCounter in metrics.ts with status_code label - Instrument crawlerWorker.ts to track status codes after page crawling - Counter increments for each crawl with the corresponding HTTP status code The metric is exposed at the /metrics endpoint and follows the naming convention: karakeep_crawler_status_codes_total * fix: update counter name to follow Prometheus conventions Change metric name from "karakeep_crawler_status_codes" to "karakeep_crawler_status_codes_total" to comply with Prometheus naming best practices for counter metrics. --------- Co-authored-by: Claude <[email protected]>
1 parent 4c6ef25 commit 4350666

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

apps/workers/metrics.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ export const workerStatsCounter = new Counter({
1515
labelNames: ["worker_name", "status"],
1616
});
1717

18+
export const crawlerStatusCodeCounter = new Counter({
19+
name: "karakeep_crawler_status_codes_total",
20+
help: "HTTP status codes encountered during crawling",
21+
labelNames: ["status_code"],
22+
});
23+
1824
registry.registerMetric(workerStatsCounter);
25+
registry.registerMetric(crawlerStatusCodeCounter);

apps/workers/workers/crawlerWorker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import metascraperPublisher from "metascraper-publisher";
2626
import metascraperTitle from "metascraper-title";
2727
import metascraperTwitter from "metascraper-twitter";
2828
import metascraperUrl from "metascraper-url";
29-
import { workerStatsCounter } from "metrics";
29+
import { crawlerStatusCodeCounter, workerStatsCounter } from "metrics";
3030
import {
3131
fetchWithProxy,
3232
getRandomProxy,
@@ -1099,6 +1099,11 @@ async function crawlAndParseUrl(
10991099

11001100
const { htmlContent, screenshot, statusCode, url: browserUrl } = result;
11011101

1102+
// Track status code in Prometheus
1103+
if (statusCode !== null) {
1104+
crawlerStatusCodeCounter.labels(statusCode.toString()).inc();
1105+
}
1106+
11021107
const meta = await Promise.race([
11031108
extractMetadata(htmlContent, browserUrl, jobId),
11041109
abortPromise(abortSignal),

0 commit comments

Comments
 (0)