Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

feat: add metrics for sub status

Mohamed Bassem3b3eb7546e2d
diff --git a/packages/trpc/stats.ts b/packages/trpc/stats.ts
index 5ee49a43..b252af46 100644
--- a/packages/trpc/stats.ts
+++ b/packages/trpc/stats.ts
@@ -2,7 +2,7 @@ import { count, sum } from "drizzle-orm";
import { Counter, Gauge, Histogram, register } from "prom-client";
import { db } from "@karakeep/db";
-import { assets, bookmarks, users } from "@karakeep/db/schema";
+import { assets, bookmarks, subscriptions, users } from "@karakeep/db/schema";
import {
AdminMaintenanceQueue,
AssetPreprocessingQueue,
@@ -16,6 +16,7 @@ import {
VideoWorkerQueue,
WebhookQueue,
} from "@karakeep/shared-server";
+import serverConfig from "@karakeep/shared/config";
// Queue metrics
const queuePendingJobsGauge = new Gauge({
@@ -75,6 +76,35 @@ const totalUsersGauge = new Gauge({
},
});
+const subscriptionStatusGauge = serverConfig.stripe.isConfigured
+ ? new Gauge({
+ name: "karakeep_subscription_status",
+ help: "Total number of subscriptions per status",
+ labelNames: ["status", "tier"],
+ async collect() {
+ this.reset();
+ try {
+ const results = await db
+ .select({
+ status: subscriptions.status,
+ tier: subscriptions.tier,
+ count: count(),
+ })
+ .from(subscriptions)
+ .groupBy(subscriptions.status, subscriptions.tier);
+ for (const result of results) {
+ this.set(
+ { status: result.status, tier: result.tier },
+ result.count,
+ );
+ }
+ } catch (error) {
+ console.error("Failed to get subscription status:", error);
+ }
+ },
+ })
+ : null;
+
// Asset metrics
const totalAssetSizeGauge = new Gauge({
name: "karakeep_total_asset_size_bytes",
@@ -139,6 +169,9 @@ const apiRequestDurationSummary = new Histogram({
// Register all metrics
register.registerMetric(queuePendingJobsGauge);
register.registerMetric(totalUsersGauge);
+if (subscriptionStatusGauge) {
+ register.registerMetric(subscriptionStatusGauge);
+}
register.registerMetric(totalAssetSizeGauge);
register.registerMetric(totalBookmarksGauge);
register.registerMetric(bookmarkCreationCounter);