Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom metrics for messages and conversations #1137

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/lib/server/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { collectDefaultMetrics, Registry } from "prom-client";
import { collectDefaultMetrics, Registry, Counter } from "prom-client";
import express from "express";
import { logger } from "$lib/server/logger";
import { env } from "$env/dynamic/private";

export class MetricsServer {
private static instance: MetricsServer;
private conversationsTotal: Counter<string>;
private messagesTotal: Counter<string>;

private constructor() {
const app = express();
Expand All @@ -17,6 +19,20 @@ export class MetricsServer {
const register = new Registry();
collectDefaultMetrics({ register });

this.conversationsTotal = new Counter({
name: "conversations_total",
help: "Total number of conversations",
labelNames: ["model"],
registers: [register],
});

this.messagesTotal = new Counter({
name: "messages_total",
help: "Total number of messages",
labelNames: ["model"],
registers: [register],
});

app.get("/metrics", (req, res) => {
register.metrics().then((metrics) => {
res.set("Content-Type", "text/plain");
Expand All @@ -40,4 +56,12 @@ export class MetricsServer {

return MetricsServer.instance;
}

public incrementConversationsTotal(model: string) {
this.conversationsTotal.labels(model).inc();
}

public incrementMessagesTotal(model: string) {
this.messagesTotal.labels(model).inc();
}
}
4 changes: 4 additions & 0 deletions src/routes/conversation/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { defaultEmbeddingModel } from "$lib/server/embeddingModels";
import { v4 } from "uuid";
import { authCondition } from "$lib/server/auth";
import { usageLimits } from "$lib/server/usageLimits";
import { MetricsServer } from "$lib/server/metrics";

export const POST: RequestHandler = async ({ locals, request }) => {
const body = await request.text();
Expand Down Expand Up @@ -115,6 +116,9 @@ export const POST: RequestHandler = async ({ locals, request }) => {
...(values.fromShare ? { meta: { fromShareId: values.fromShare } } : {}),
});

const metricsServer = MetricsServer.getInstance();
metricsServer.incrementConversationsTotal(values.model);

return new Response(
JSON.stringify({
conversationId: res.insertedId.toString(),
Expand Down
4 changes: 4 additions & 0 deletions src/routes/conversation/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { buildSubtree } from "$lib/utils/tree/buildSubtree.js";
import { addChildren } from "$lib/utils/tree/addChildren.js";
import { addSibling } from "$lib/utils/tree/addSibling.js";
import { usageLimits } from "$lib/server/usageLimits";
import { MetricsServer } from "$lib/server/metrics";
import { textGeneration } from "$lib/server/textGeneration";
import type { TextGenerationContext } from "$lib/server/textGeneration/types";

Expand Down Expand Up @@ -428,6 +429,9 @@ export async function POST({ request, locals, params, getClientAddress }) {
);
}

const metricsServer = MetricsServer.getInstance();
metricsServer.incrementMessagesTotal(conv.model);

// Todo: maybe we should wait for the message to be saved before ending the response - in case of errors
return new Response(stream, {
headers: {
Expand Down
Loading