Skip to content

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

feat: add worker logs to betterstack (#2902)

Max Deichmann65f797095e89
diff --git a/.env.prod.example b/.env.prod.example
index 832f758194..c08d83d307 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -131,6 +131,7 @@ ENCRYPTION_KEY="0000000000000000000000000000000000000000000000000000000000000000
# SENTRY_AUTH_TOKEN=
# Used to determine the Sentry sample rate
# LANGFUSE_TRACING_SAMPLE_RATE=
+# LANGFUSE_WORKER_BETTERSTACK_TOKEN=
# Cloudflare Turnstile
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index c4de3c3c1f..01305dfa9d 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -686,6 +686,9 @@ importers:
'@langfuse/shared':
specifier: workspace:*
version: link:../packages/shared
+ '@logtail/pino':
+ specifier: ^0.5.0
+ version: 0.5.0([email protected])
'@sentry/node':
specifier: 8.13.0
version: 8.13.0
@@ -2020,6 +2023,23 @@ packages:
resolution: {integrity: sha512-/gKJun8NNiWGZJkGzI/Ragc53cOdcLNdzjLaIa+GEjguQs0ulsurx8WN0jijdK9yPqDvziX995sMRLyLt1uZMQ==}
engines: {node: '>= 0.4'}
+ '@logtail/[email protected]':
+ resolution: {integrity: sha512-zHl1wx5fqqICVOjM+IVzJ6vqlDobcQu0AK2TL7yPTxCNa8C3gTCtOKsc5sX/yMnSFqCK4KcYpY7DPUAYYGzezg==}
+
+ '@logtail/[email protected]':
+ resolution: {integrity: sha512-nTyEpdei/qaXJT55OXkiUE6RuMgmRfQ5eMkExHCQe4MB/SAbMbXd+1ULv6cHLAj+S5xMvPGdpMyaydfHtPAkXA==}
+
+ '@logtail/[email protected]':
+ resolution: {integrity: sha512-yNeVha1G+b1riN6VYH+171Q122HIWxyM0Xg5lSWOH2sJcKvyrLExJ3BQeU4xEZWhYmODkvzUFvXlJuLL2MMuuA==}
+ peerDependencies:
+ pino: ^7.0.0 || ^8.0.0 || ^9.0.0
+
+ '@logtail/[email protected]':
+ resolution: {integrity: sha512-xCN7Cvq9DJ8loJkaPdihQImsLPAUVYlDK3qJiMneuIgIBJm5awVJ6BUZhycLmtz5NlLG1M2SJbuxEYU7WU3sDg==}
+
+ '@logtail/[email protected]':
+ resolution: {integrity: sha512-wO+uw142K5WFS1lD3bmx2u8U3bMRVtaW48j6AlkMfq+yJfQlNzLL4EP5wR2b3QmcFN6LC240dJ3Aq7phR250Ig==}
+
'@marsidev/[email protected]':
resolution: {integrity: sha512-cloUDkEcJm+G7p3J8DwPtRNNB2GZqVi/nlIbgu9o3VzNyV5K/bWcSfOyWouRiR3umAQZmsFpR3OFYa4mCmy4AQ==}
peerDependencies:
@@ -2041,6 +2061,10 @@ packages:
resolution: {integrity: sha512-GTPkYf1meO2UXXIrz/SIDFWz+P4kXo2PTt36LYh/oNxV1PieYi7ZgenQk4IV0ut71Je3Z8ZoNZ8Tr7v2c1X1pg==}
engines: {node: '>=16'}
+ '@msgpack/[email protected]':
+ resolution: {integrity: sha512-h9u4u/jiIRKbq25PM+zymTyW6bhTzELvOoUd+AvYriWOAKpLGnIamaET3pnHYoI5iYphAHBI4ayx0MehR+VVPQ==}
+ engines: {node: '>= 10'}
+
'@msgpackr-extract/[email protected]':
resolution: {integrity: sha512-QZHtlVgbAdy2zAqNA9Gu1UpIuI8Xvsd1v8ic6B2pZmeFnFcMWiPLfWXh7TVw4eGEZ/C9TH281KwhVoeQUKbyjw==}
cpu: [arm64]
@@ -4136,6 +4160,9 @@ packages:
resolution: {integrity: sha512-9Hp0ObzwwO57DpLFF0InUjUm/II8GmKAvzbefxQTihCb7KI6yc9yzf0nLc4mVdby5N4DRCgQM2wCup9KTieeww==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-O7in6531Bbvlb2KEsJ0dq0CHZvc3iWSR5ZYMtvGgnHA56VgriAN/AU2LorfmcvAl2xc9N5fbCTRyMRRl8nd74g==}
+
resolution: {integrity: sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==}
@@ -5265,6 +5292,9 @@ packages:
resolution: {integrity: sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==}
+ resolution: {integrity: sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==}
+
resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==}
engines: {node: '>= 8'}
@@ -9335,6 +9365,10 @@ packages:
resolution: {integrity: sha512-qqWzuOjSFOuqPjFe4NOsMLafToQQwBSOEpS+FwEt3A2V3vKubTquT3vmLTQpFgMXp8AlFWFuP1qKaJZOtPpVXg==}
engines: {node: '>= 0.8.0'}
+ resolution: {integrity: sha512-3NnuWfM6vBYoy5gZFvHiYsVbafvI9vZv/+jlIigFn4oP4zjNPK3LhcY0xSCgeb1a5L8jO71Mit9LlNoi2UfDDQ==}
+ engines: {node: '>=10'}
+
resolution: {integrity: sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==}
@@ -9528,6 +9562,9 @@ packages:
peerDependencies:
svelte: ^4.0.0
+ resolution: {integrity: sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==}
+
resolution: {integrity: sha512-XlkWvfIm6RmsWtNJx+uqtKLS8eqFbxUg0ZzLXqY0caEy9l7hruX8IpiDnjsLavoBgqCCR71TqWO8MaXYheJ3RQ==}
engines: {node: '>=10'}
@@ -12471,6 +12508,45 @@ snapshots:
dependencies:
call-bind: 1.0.7
+ '@logtail/[email protected]':
+ dependencies:
+ '@logtail/tools': 0.5.0
+ '@logtail/types': 0.5.0
+ serialize-error: 8.1.0
+ transitivePeerDependencies:
+ - encoding
+
+ '@logtail/[email protected]':
+ dependencies:
+ '@logtail/core': 0.5.0
+ '@logtail/types': 0.5.0
+ '@msgpack/msgpack': 2.8.0
+ '@types/stack-trace': 0.0.33
+ cross-fetch: 4.0.0
+ minimatch: 9.0.5
+ serialize-error: 8.1.0
+ stack-trace: 0.0.10
+ transitivePeerDependencies:
+ - encoding
+
+ dependencies:
+ '@logtail/node': 0.5.0
+ '@logtail/types': 0.5.0
+ pino: 9.2.0
+ pino-abstract-transport: 1.2.0
+ transitivePeerDependencies:
+ - encoding
+
+ '@logtail/[email protected]':
+ dependencies:
+ '@logtail/types': 0.5.0
+ cross-fetch: 4.0.0
+ transitivePeerDependencies:
+ - encoding
+
+ '@logtail/[email protected]': {}
+
dependencies:
react: 18.2.0
@@ -12502,6 +12578,8 @@ snapshots:
chevrotain: 10.5.0
lilconfig: 2.1.0
+ '@msgpack/[email protected]': {}
+
'@msgpackr-extract/[email protected]':
optional: true
@@ -15031,6 +15109,8 @@ snapshots:
'@types/[email protected]': {}
+ '@types/[email protected]': {}
+
'@types/[email protected]': {}
'@types/[email protected]': {}
@@ -16468,6 +16548,12 @@ snapshots:
transitivePeerDependencies:
- encoding
+ dependencies:
+ node-fetch: 2.7.0
+ transitivePeerDependencies:
+ - encoding
+
dependencies:
path-key: 3.1.1
@@ -21455,6 +21541,10 @@ snapshots:
transitivePeerDependencies:
- supports-color
+ dependencies:
+ type-fest: 0.20.2
+
dependencies:
randombytes: 2.1.0
@@ -21647,6 +21737,8 @@ snapshots:
svelte: 4.2.18
swrev: 4.0.0
+
dependencies:
escape-string-regexp: 2.0.0
diff --git a/worker/package.json b/worker/package.json
index 7d3413e594..e4507bc898 100644
--- a/worker/package.json
+++ b/worker/package.json
@@ -21,6 +21,7 @@
"@anthropic-ai/tokenizer": "^0.0.4",
"@clickhouse/client": "^1.4.0",
"@langfuse/shared": "workspace:*",
+ "@logtail/pino": "^0.5.0",
"@sentry/node": "8.13.0",
"@sentry/profiling-node": "8.13.0",
"backoff": "^2.5.0",
diff --git a/worker/src/env.ts b/worker/src/env.ts
index fbe94817f9..8ae062baa8 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -71,6 +71,7 @@ const EnvSchema = z.object({
CLICKHOUSE_URL: z.string().url().optional(),
CLICKHOUSE_USER: z.string().optional(),
CLICKHOUSE_PASSWORD: z.string().optional(),
+ LANGFUSE_WORKER_BETTERSTACK_TOKEN: z.string().optional(),
});
export const env = EnvSchema.parse(process.env);
diff --git a/worker/src/logger.ts b/worker/src/logger.ts
index 83429acfa1..8e63127321 100644
--- a/worker/src/logger.ts
+++ b/worker/src/logger.ts
@@ -1,15 +1,14 @@
import pino from "pino";
import { env } from "./env";
-// Map Pino levels to Google Cloud Logging severity levels
-// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
-const levelToSeverity = {
- trace: "DEBUG",
- debug: "DEBUG",
- info: "INFO",
- warn: "WARNING",
- error: "ERROR",
- fatal: "CRITICAL",
+const getBetterstackLogginTransport = (minLevel: string) => {
+ return env.LANGFUSE_WORKER_BETTERSTACK_TOKEN
+ ? pino.transport({
+ target: "@logtail/pino",
+ options: { sourceToken: env.LANGFUSE_WORKER_BETTERSTACK_TOKEN },
+ level: minLevel,
+ })
+ : { level: minLevel };
};
export const getLogger = (
@@ -17,37 +16,7 @@ export const getLogger = (
minLevel = "info"
) => {
if (env === "production") {
- return pino({
- base: { serviceContext: { service: "langfuse-worker" } },
- formatters: {
- level(label) {
- const pinoLevel = label as pino.Level;
- // `@type` property tells Error Reporting to track even if there is no `stack_trace`
- const typeProp =
- label === "error" || label === "fatal"
- ? {
- "@type":
- "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
- }
- : {};
- return {
- level: pinoLevel,
- severity: levelToSeverity[pinoLevel],
- ...typeProp,
- };
- },
- log(object: object & { err?: Error }) {
- const stackTrace = object.err?.stack;
- const stackProp = stackTrace ? { stack_trace: stackTrace } : {};
- return {
- ...object,
- ...stackProp,
- };
- },
- },
- messageKey: "message",
- timestamp: () => `,"eventTime":${Date.now() / 1000.0}`,
- });
+ return pino(getBetterstackLogginTransport(minLevel));
}
return pino({
level: minLevel,
@@ -60,7 +29,5 @@ export const getLogger = (
},
});
};
-
const logger = getLogger(env.NODE_ENV, env.LANGFUSE_LOG_LEVEL);
-
export default logger;