Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(shared): set Postgres application_name to langfuse/<version> (#18421)

Kaushik Iska55a9ec80bd05
diff --git a/packages/shared/src/db.ts b/packages/shared/src/db.ts
index cd39b4f3c0..ac005400da 100644
--- a/packages/shared/src/db.ts
+++ b/packages/shared/src/db.ts
@@ -4,6 +4,7 @@
import { Prisma, PrismaClient } from "@prisma/client";
import { env } from "process";
import { logger } from "./server";
+import { withPostgresApplicationName } from "./server/postgresApplicationName";
export class PrismaClientSingleton {
private static instance: PrismaClient;
@@ -24,6 +25,7 @@ const createPrismaInstance = () => {
Prisma.PrismaClientOptions,
"warn" | "error" | "query"
>({
+ datasourceUrl: withPostgresApplicationName(env.DATABASE_URL),
log: [
{ emit: "event", level: "query" },
{ emit: "event", level: "error" },
diff --git a/packages/shared/src/server/postgresApplicationName.test.ts b/packages/shared/src/server/postgresApplicationName.test.ts
new file mode 100644
index 0000000000..780a9cf750
--- /dev/null
+++ b/packages/shared/src/server/postgresApplicationName.test.ts
@@ -0,0 +1,38 @@
+import { describe, expect, it } from "vitest";
+
+import { VERSION } from "../constants/VERSION";
+import { withPostgresApplicationName } from "./postgresApplicationName";
+
+const applicationName = `application_name=${encodeURIComponent(`langfuse/${VERSION.replace("v", "")}`)}`;
+
+describe("withPostgresApplicationName", () => {
+ it("adds application_name to a URL without query parameters", () => {
+ expect(
+ withPostgresApplicationName(
+ "postgresql://postgres:postgres@localhost:5432/postgres",
+ ),
+ ).toBe(
+ `postgresql://postgres:postgres@localhost:5432/postgres?${applicationName}`,
+ );
+ });
+
+ it("appends application_name after existing query parameters", () => {
+ expect(
+ withPostgresApplicationName(
+ "postgresql://u:p%40ss@db:5432/langfuse?schema=public&sslmode=require",
+ ),
+ ).toBe(
+ `postgresql://u:p%40ss@db:5432/langfuse?schema=public&sslmode=require&${applicationName}`,
+ );
+ });
+
+ it("keeps an application_name already set in the URL", () => {
+ const url =
+ "postgresql://postgres:postgres@localhost:5432/postgres?application_name=custom";
+ expect(withPostgresApplicationName(url)).toBe(url);
+ });
+
+ it("returns an unset URL unchanged", () => {
+ expect(withPostgresApplicationName(undefined)).toBeUndefined();
+ });
+});
diff --git a/packages/shared/src/server/postgresApplicationName.ts b/packages/shared/src/server/postgresApplicationName.ts
new file mode 100644
index 0000000000..09ed26e374
--- /dev/null
+++ b/packages/shared/src/server/postgresApplicationName.ts
@@ -0,0 +1,19 @@
+import { VERSION } from "../constants/VERSION";
+
+const APPLICATION_NAME = `langfuse/${VERSION.replace("v", "")}`;
+
+/**
+ * Adds `application_name=langfuse/<version>` to a Postgres connection URL so
+ * Langfuse sessions are identifiable in `pg_stat_activity`, server logs, and
+ * managed Postgres query insights. An `application_name` already present in
+ * the URL is left untouched.
+ */
+export const withPostgresApplicationName = (
+ url: string | undefined,
+): string | undefined => {
+ if (!url || /[?&]application_name=/.test(url)) {
+ return url;
+ }
+ const separator = url.includes("?") ? "&" : "?";
+ return `${url}${separator}application_name=${encodeURIComponent(APPLICATION_NAME)}`;
+};