🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(dashboards): add langfuse managed dashboards (#6877)
Steffen Schmitz6e1886055a4e
diff --git a/worker/src/constants/langfuse-dashboards.json b/worker/src/constants/langfuse-dashboards.jsonnew file mode 100644index 0000000000..721ef03584--- /dev/null+++ b/worker/src/constants/langfuse-dashboards.json@@ -0,0 +1,147 @@+{+ "widgets": [+ {+ "id": "cmawk617300iiad07zaes6h3l",+ "createdAt": "2025-05-20T13:37:04.912Z",+ "updatedAt": "2025-05-20T14:12:31.079Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "P 95 Latency by Use Case",+ "description": "Shows the p 95 latency by trace.name",+ "view": "TRACES",+ "dimensions": [{"field": "name"}],+ "metrics": [{"agg": "p95", "measure": "latency"}],+ "filters": [],+ "chartType": "LINE_TIME_SERIES",+ "chartConfig": {"type": "LINE_TIME_SERIES"},+ "owner": "LANGFUSE"+ },+ {+ "id": "cmawk6isp00kbad07t66dohjn",+ "createdAt": "2025-05-20T13:37:27.721Z",+ "updatedAt": "2025-05-20T13:37:27.721Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "P 95 Latency by Level (Observations)",+ "description": "Shows the p 95 latency of Observations by level",+ "view": "OBSERVATIONS",+ "dimensions": [{"field": "level"}],+ "metrics": [{"agg": "p95", "measure": "latency"}],+ "filters": [],+ "chartType": "LINE_TIME_SERIES",+ "chartConfig": {"type": "LINE_TIME_SERIES"},+ "owner": "LANGFUSE"+ },+ {+ "id": "cmawk94z800ldad07jjox8ugd",+ "createdAt": "2025-05-20T13:39:29.781Z",+ "updatedAt": "2025-05-20T14:00:25.968Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "Max Latency by User Id (Traces)",+ "description": "Top 50 max latency of Traces by trace.userId",+ "view": "TRACES",+ "dimensions": [{"field": "userId"}],+ "metrics": [{"agg": "max", "measure": "latency"}],+ "filters": [],+ "chartType": "HORIZONTAL_BAR",+ "chartConfig": {"type": "HORIZONTAL_BAR", "row_limit": 50},+ "owner": "LANGFUSE"+ },+ {+ "id": "cmawka1fk00kdad07vdipgz04",+ "createdAt": "2025-05-20T13:40:11.840Z",+ "updatedAt": "2025-05-20T14:14:21.419Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "Avg Time To First Token by Prompt Name (Observations)",+ "description": "Shows the avg time to first token of Observations by prompt name",+ "view": "OBSERVATIONS",+ "dimensions": [{"field": "promptName"}],+ "metrics": [{"agg": "avg", "measure": "timeToFirstToken"}],+ "filters": [],+ "chartType": "VERTICAL_BAR",+ "chartConfig": {"type": "VERTICAL_BAR", "row_limit": 100},+ "owner": "LANGFUSE"+ },+ {+ "id": "cmawksk8h00phad07s9c7v6d7",+ "createdAt": "2025-05-20T13:54:36.017Z",+ "updatedAt": "2025-05-20T14:13:09.323Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "P 95 Time To First Token by Model",+ "description": "Shows the p 95 time to first token for each model",+ "view": "OBSERVATIONS",+ "dimensions": [{"field": "providedModelName"}],+ "metrics": [{"agg": "p95", "measure": "timeToFirstToken"}],+ "filters": [],+ "chartType": "LINE_TIME_SERIES",+ "chartConfig": {"type": "LINE_TIME_SERIES"},+ "owner": "LANGFUSE"+ },+ {+ "id": "cmawktot400pkad07m8gy30vq",+ "createdAt": "2025-05-20T13:55:28.601Z",+ "updatedAt": "2025-05-20T13:55:28.601Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "P 95 Latency by Model",+ "description": "Shows the p 95 latency of Observations by provided model name",+ "view": "OBSERVATIONS",+ "dimensions": [{"field": "providedModelName"}],+ "metrics": [{"agg": "p95", "measure": "latency"}],+ "filters": [],+ "chartType": "LINE_TIME_SERIES",+ "chartConfig": {"type": "LINE_TIME_SERIES"},+ "owner": "LANGFUSE"+ },+ {+ "id": "cmawl83ks001ead076pk2wcex",+ "createdAt": "2025-05-20T14:06:40.924Z",+ "updatedAt": "2025-05-20T14:13:27.525Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "Avg Output Tokens Per Second by Model",+ "description": "Shows the avg output tokens per second by model",+ "view": "OBSERVATIONS",+ "dimensions": [{"field": "providedModelName"}],+ "metrics": [{"agg": "avg", "measure": "outputTokensPerSecond"}],+ "filters": [],+ "chartType": "LINE_TIME_SERIES",+ "chartConfig": {"type": "LINE_TIME_SERIES"},+ "owner": "LANGFUSE"+ }+ ],+ "dashboards": [+ {+ "id": "cmawk4ywj00jmad072jn7s0ru",+ "createdAt": "2025-05-20T13:36:15.283Z",+ "updatedAt": "2025-05-20T14:14:32.767Z",+ "createdBy": null,+ "updatedBy": null,+ "projectId": null,+ "name": "Langfuse Latency Dashboard",+ "description": "Track the latency and errors across your LLM data.",+ "owner": "LANGFUSE",+ "definition": {+ "widgets": [+ {"x": 0, "y": 0, "id": "87a95184-aea5-4418-9447-2fe381c69414", "type": "widget", "x_size": 6, "y_size": 5, "widgetId": "cmawk617300iiad07zaes6h3l"},+ {"x": 6, "y": 0, "id": "62055994-0ae0-47c9-bef8-25dfbbe3dcb3", "type": "widget", "x_size": 6, "y_size": 5, "widgetId": "cmawk6isp00kbad07t66dohjn"},+ {"x": 0, "y": 5, "id": "7eb6d831-3e11-49e1-9855-7f28ea16f865", "type": "widget", "x_size": 6, "y_size": 5, "widgetId": "cmawk94z800ldad07jjox8ugd"},+ {"x": 6, "y": 5, "id": "b17fb429-41b5-4681-8271-cc8674608341", "type": "widget", "x_size": 6, "y_size": 5, "widgetId": "cmawka1fk00kdad07vdipgz04"},+ {"x": 0, "y": 10, "id": "61bf13ae-2e63-482b-97d6-168ce2097d15", "type": "widget", "x_size": 4, "y_size": 5, "widgetId": "cmawksk8h00phad07s9c7v6d7"},+ {"x": 4, "y": 10, "id": "b985686a-c509-4f25-9cc5-709efeecd80e", "type": "widget", "x_size": 4, "y_size": 5, "widgetId": "cmawktot400pkad07m8gy30vq"},+ {"x": 8, "y": 10, "id": "1a7667fe-29e4-4a4b-918d-cba6fdd5c016", "type": "widget", "x_size": 4, "y_size": 5, "widgetId": "cmawl83ks001ead076pk2wcex"}+ ]+ }+ }+ ]+}\ No newline at end of filediff --git a/worker/src/initialize.ts b/worker/src/initialize.tsindex fb876d52b5..2b26419960 100644--- a/worker/src/initialize.ts+++ b/worker/src/initialize.ts@@ -1,3 +1,5 @@import { upsertDefaultModelPrices } from "./scripts/upsertDefaultModelPrices";+import { upsertLangfuseDashboards } from "./scripts/upsertLangfuseDashboards";upsertDefaultModelPrices();+upsertLangfuseDashboards();diff --git a/worker/src/scripts/upsertLangfuseDashboards.ts b/worker/src/scripts/upsertLangfuseDashboards.tsnew file mode 100644index 0000000000..2f308c8155--- /dev/null+++ b/worker/src/scripts/upsertLangfuseDashboards.ts@@ -0,0 +1,195 @@+import { z } from "zod";+import { prisma } from "@langfuse/shared/src/db";+import langfuseDashboards from "../constants/langfuse-dashboards.json";+import {+ logger,+ WidgetDomainSchema,+ DashboardDomainSchema,+} from "@langfuse/shared/src/server";++/**+ * JSON STRUCTURE & SCHEMAS+ * ------------------------+ * We expect the JSON to have the following top-level structure:+ * {+ * widgets: Widget[],+ * dashboards: Dashboard[]+ * }+ *+ * 1. A flat array of widgets which can be referenced by dashboards via `widgetId`.+ * 2. An array of dashboards that reference widgets via their definitions.+ *+ * All IDs and timestamps come from the JSON to guarantee deterministic results and stable diffs.+ */+const RawWidgetSchema = WidgetDomainSchema.extend({+ createdAt: z.string(),+ updatedAt: z.string(),+}).transform((raw) =>+ WidgetDomainSchema.parse({+ ...raw,+ createdAt: new Date(raw.createdAt),+ updatedAt: new Date(raw.updatedAt),+ }),+);++const RawDashboardSchema = DashboardDomainSchema.extend({+ createdAt: z.string(),+ updatedAt: z.string(),+}).transform((raw) =>+ DashboardDomainSchema.parse({+ ...raw,+ createdAt: new Date(raw.createdAt),+ updatedAt: new Date(raw.updatedAt),+ }),+);++const FileSchema = z.object({+ widgets: z.array(RawWidgetSchema),+ dashboards: z.array(RawDashboardSchema),+});++type ParsedWidgets = z.infer<typeof WidgetDomainSchema>[];+type ParsedDashboards = z.infer<typeof DashboardDomainSchema>[];++export const upsertLangfuseDashboards = async (force = false) => {+ const startTime = Date.now();+ try {+ logger.debug(`Starting upsert of Langfuse dashboards (force = ${force})`);++ const parsed = FileSchema.parse(langfuseDashboards);++ await upsertWidgets(parsed.widgets, force);+ await upsertDashboards(parsed.dashboards, force);++ logger.info(+ `Finished upserting Langfuse dashboards and widgets in ${Date.now() - startTime}ms`,+ );+ } catch (error) {+ logger.error(+ `Error upserting Langfuse dashboards after ${Date.now() - startTime}ms: ${+ error instanceof Error ? error.message : ""+ }`,+ { error },+ );+ }+};++async function upsertWidgets(widgets: ParsedWidgets, force: boolean) {+ // Build map of existing widgets by id (only projectId null)+ const existing = await prisma.dashboardWidget.findMany({+ where: {+ projectId: null,+ id: { in: widgets.map((w) => w.id) },+ },+ select: {+ id: true,+ updatedAt: true,+ },+ });+ const existingMap = new Map(existing.map((w) => [w.id, w.updatedAt]));++ const upsertPromises = widgets.map((widget) => {+ const existingUpdatedAt = existingMap.get(widget.id);+ if (+ !force &&+ existingUpdatedAt &&+ existingUpdatedAt.getTime() === widget.updatedAt.getTime()+ ) {+ logger.debug(`Widget ${widget.name} already up to date. Skipping.`);+ return Promise.resolve();+ }++ const baseWidget = {+ name: widget.name,+ description: widget.description,+ view: widget.view,+ dimensions: widget.dimensions,+ metrics: widget.metrics,+ filters: widget.filters,+ chartType: widget.chartType,+ chartConfig: widget.chartConfig,+ updatedAt: widget.updatedAt,+ };++ return prisma.dashboardWidget+ .upsert({+ where: { id: widget.id },+ update: {+ ...baseWidget,+ },+ create: {+ id: widget.id,+ projectId: null,+ createdAt: widget.createdAt,+ ...baseWidget,+ },+ })+ .then(() => logger.info(`Upserted widget ${widget.name} (${widget.id})`))+ .catch((error) => {+ logger.error(+ `Error upserting widget ${widget.name} (${widget.id}): ${error.message}`,+ { error },+ );+ });+ });++ await Promise.all(upsertPromises);+}++async function upsertDashboards(dashboards: ParsedDashboards, force: boolean) {+ const existing = await prisma.dashboard.findMany({+ where: {+ projectId: null,+ id: { in: dashboards.map((d) => d.id) },+ },+ select: {+ id: true,+ updatedAt: true,+ },+ });+ const existingMap = new Map(existing.map((d) => [d.id, d.updatedAt]));++ const promises = dashboards.map((dashboard) => {+ const existingUpdatedAt = existingMap.get(dashboard.id);+ if (+ !force &&+ existingUpdatedAt &&+ existingUpdatedAt.getTime() === dashboard.updatedAt.getTime()+ ) {+ logger.debug(`Dashboard ${dashboard.name} already up to date. Skipping.`);+ return Promise.resolve();+ }++ const baseDashboard = {+ name: dashboard.name,+ description: dashboard.description,+ definition: dashboard.definition,+ updatedAt: dashboard.updatedAt,+ };++ return prisma.dashboard+ .upsert({+ where: { id: dashboard.id },+ update: {+ ...baseDashboard,+ },+ create: {+ id: dashboard.id,+ projectId: null,+ createdAt: dashboard.createdAt,+ ...baseDashboard,+ },+ })+ .then(() =>+ logger.info(`Upserted dashboard ${dashboard.name} (${dashboard.id})`),+ )+ .catch((error) => {+ logger.error(+ `Error upserting dashboard ${dashboard.name} (${dashboard.id}): ${error.message}`,+ { error },+ );+ });+ });++ await Promise.all(promises);+}