Skip to content

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

feat: send sdk logs to betterstack (#1125)

Max Deichmann85737221d96d
diff --git a/src/env.mjs b/src/env.mjs
index 7955868b36..42c121af78 100644
--- a/src/env.mjs
+++ b/src/env.mjs
@@ -26,6 +26,7 @@ export const env = createEnv({
),
NEXTAUTH_COOKIE_DOMAIN: z.string().optional(),
LANGFUSE_TEAM_SLACK_WEBHOOK: z.string().url().optional(),
+ LANGFUSE_TEAM_BETTERSTACK_TOKEN: z.string().optional(),
LANGFUSE_NEW_USER_SIGNUP_WEBHOOK: z.string().url().optional(),
// Add `.min(1) on ID and SECRET if you want to make sure they're not empty
LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES: z.enum(["true", "false"]).optional(),
@@ -91,6 +92,8 @@ export const env = createEnv({
LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES:
process.env.LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES,
LANGFUSE_TEAM_SLACK_WEBHOOK: process.env.LANGFUSE_TEAM_SLACK_WEBHOOK,
+ LANGFUSE_TEAM_BETTERSTACK_TOKEN:
+ process.env.LANGFUSE_TEAM_BETTERSTACK_TOKEN,
LANGFUSE_NEW_USER_SIGNUP_WEBHOOK:
process.env.LANGFUSE_NEW_USER_SIGNUP_WEBHOOK,
SALT: process.env.SALT,
diff --git a/src/features/betterstack/server/betterstack-webhook.ts b/src/features/betterstack/server/betterstack-webhook.ts
new file mode 100644
index 0000000000..a0e8f3580a
--- /dev/null
+++ b/src/features/betterstack/server/betterstack-webhook.ts
@@ -0,0 +1,28 @@
+import { env } from "@/src/env.mjs";
+
+export const sendToBetterstack = async (message: unknown) => {
+ if (!env.LANGFUSE_TEAM_BETTERSTACK_TOKEN)
+ throw new Error("LANGFUSE_TEAM_BETTERSTACK_TOKEN is not set");
+
+ const url = "https://in.logs.betterstack.com";
+
+ const headers = new Headers({
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${env.LANGFUSE_TEAM_BETTERSTACK_TOKEN}`,
+ });
+
+ const payload = {
+ dt: new Date().toISOString(), // Gets the current date in ISO format (UTC)
+ message: JSON.stringify(message, null, 2),
+ };
+
+ const response = await fetch(url, {
+ method: "POST",
+ headers: headers,
+ body: JSON.stringify(payload),
+ });
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+};
diff --git a/src/features/feedback/server/feedbackHandler.ts b/src/features/feedback/server/feedbackHandler.ts
index e235af4ee0..ff3da35e6f 100644
--- a/src/features/feedback/server/feedbackHandler.ts
+++ b/src/features/feedback/server/feedbackHandler.ts
@@ -1,5 +1,5 @@
-import { env } from "@/src/env.mjs";
import { runFeedbackCorsMiddleware } from "@/src/features/feedback/server/corsMiddleware";
+import { sendToSlack } from "@/src/features/slack/server/slack-webhook";
import { type NextApiRequest, type NextApiResponse } from "next";
// Collects feedack from users that do not use the cloud version of the app
@@ -10,17 +10,7 @@ export default async function feedbackApiHandler(
await runFeedbackCorsMiddleware(req, res);
try {
- if (!env.LANGFUSE_TEAM_SLACK_WEBHOOK)
- throw new Error("LANGFUSE_TEAM_SLACK_WEBHOOK is not set");
-
- const slackResponse = await fetch(env.LANGFUSE_TEAM_SLACK_WEBHOOK, {
- method: "POST",
- body: JSON.stringify({ rawBody: JSON.stringify(req.body, null, 2) }),
- headers: {
- "Content-Type": "application/json",
- },
- });
-
+ const slackResponse = await sendToSlack(req);
if (slackResponse.status === 200) {
res.status(200).json({ status: "OK" });
} else {
diff --git a/src/features/slack/server/slack-webhook.ts b/src/features/slack/server/slack-webhook.ts
new file mode 100644
index 0000000000..6199bd4e38
--- /dev/null
+++ b/src/features/slack/server/slack-webhook.ts
@@ -0,0 +1,14 @@
+import { env } from "@/src/env.mjs";
+
+export const sendToSlack = async (message: unknown) => {
+ if (!env.LANGFUSE_TEAM_SLACK_WEBHOOK)
+ throw new Error("LANGFUSE_TEAM_SLACK_WEBHOOK is not set");
+
+ return await fetch(env.LANGFUSE_TEAM_SLACK_WEBHOOK, {
+ method: "POST",
+ body: JSON.stringify({ rawBody: JSON.stringify(message, null, 2) }),
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+};
diff --git a/src/server/api/services/EventProcessor.ts b/src/server/api/services/EventProcessor.ts
index 013896f4b6..80ca62381a 100644
--- a/src/server/api/services/EventProcessor.ts
+++ b/src/server/api/services/EventProcessor.ts
@@ -28,6 +28,7 @@ import {
import { v4 } from "uuid";
import { type z } from "zod";
import { jsonSchema } from "@/src/utils/zod";
+import { sendToBetterstack } from "@/src/features/betterstack/server/betterstack-webhook";
export interface EventProcessor {
process(
@@ -562,7 +563,16 @@ export class SdkLogProcessor implements EventProcessor {
this.event = event;
}
- process() {
- return undefined;
+ process(apiScope: ApiAccessScope) {
+ try {
+ void sendToBetterstack({
+ type: "sdk-log",
+ event: this.event,
+ projectId: apiScope.projectId,
+ });
+ return undefined;
+ } catch (error) {
+ return undefined;
+ }
}
}