feat(langfuse): enhance trace output structure with encapsulated result data

This commit is contained in:
2026-04-27 19:03:49 +05:30
parent d71726205d
commit fa31c3f66e
2 changed files with 22 additions and 8 deletions
+19 -5
View File
@@ -38,8 +38,10 @@ import { createEmbeddings } from "./lib/embeddings-factory.js";
import { LogOperation, logger } from "./lib/logger.js"; import { LogOperation, logger } from "./lib/logger.js";
import { trace } from "@opentelemetry/api"; import { trace } from "@opentelemetry/api";
import { import {
extractMcpToolInfo,
LANGFUSE_OBSERVATION_INPUT_ATTR, LANGFUSE_OBSERVATION_INPUT_ATTR,
LANGFUSE_OBSERVATION_OUTPUT_ATTR, LANGFUSE_OBSERVATION_OUTPUT_ATTR,
LANGFUSE_TRACE_INPUT_ATTR,
LANGFUSE_TRACE_NAME_ATTR, LANGFUSE_TRACE_NAME_ATTR,
LANGFUSE_TRACE_OUTPUT_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR,
TRACE_NAME_HTTP, TRACE_NAME_HTTP,
@@ -111,7 +113,7 @@ async function createMcpServer() {
.getActiveSpan() .getActiveSpan()
?.setAttribute( ?.setAttribute(
LANGFUSE_TRACE_OUTPUT_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR,
JSON.stringify(result), JSON.stringify({ output: result }),
); );
trace trace
.getActiveSpan() .getActiveSpan()
@@ -159,7 +161,7 @@ async function createMcpServer() {
.getActiveSpan() .getActiveSpan()
?.setAttribute( ?.setAttribute(
LANGFUSE_TRACE_OUTPUT_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR,
JSON.stringify(result), JSON.stringify({ output: result }),
); );
trace trace
.getActiveSpan() .getActiveSpan()
@@ -204,7 +206,7 @@ async function createMcpServer() {
.getActiveSpan() .getActiveSpan()
?.setAttribute( ?.setAttribute(
LANGFUSE_TRACE_OUTPUT_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR,
JSON.stringify(result), JSON.stringify({ output: result }),
); );
trace trace
.getActiveSpan() .getActiveSpan()
@@ -298,15 +300,27 @@ export async function main() {
} }
} }
// Extract MCP tool info for trace-level input
const mcpInfo = extractMcpToolInfo(parsedBody);
// Handle request within trace context (passing trace ID from header if available) // Handle request within trace context (passing trace ID from header if available)
const sessionMetadata = getSessionMetadata("http"); const sessionMetadata = getSessionMetadata("http");
return await withSpan( return await withSpan(
"mcp_http_request", "mcp_http_request",
{ {
[LANGFUSE_TRACE_NAME_ATTR]: TRACE_NAME_HTTP, [LANGFUSE_TRACE_NAME_ATTR]: TRACE_NAME_HTTP,
[LANGFUSE_TRACE_INPUT_ATTR]: JSON.stringify({
http_method: c.req.method,
mcp_method: mcpInfo.method,
tool: mcpInfo.tool,
input: mcpInfo.input,
client_ip: clientIp,
}),
[LANGFUSE_OBSERVATION_INPUT_ATTR]: JSON.stringify({ [LANGFUSE_OBSERVATION_INPUT_ATTR]: JSON.stringify({
method: c.req.method, http_method: c.req.method,
endpoint: "/mcp", mcp_method: mcpInfo.method,
tool: mcpInfo.tool,
input: mcpInfo.input,
client_ip: clientIp, client_ip: clientIp,
}), }),
method: c.req.method, method: c.req.method,
+3 -3
View File
@@ -179,7 +179,7 @@ export async function main() {
.getActiveSpan() .getActiveSpan()
?.setAttribute( ?.setAttribute(
LANGFUSE_TRACE_OUTPUT_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR,
JSON.stringify(result), JSON.stringify({ output: result }),
); );
return { return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }], content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
@@ -237,7 +237,7 @@ export async function main() {
.getActiveSpan() .getActiveSpan()
?.setAttribute( ?.setAttribute(
LANGFUSE_TRACE_OUTPUT_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR,
JSON.stringify(result), JSON.stringify({ output: result }),
); );
return { return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }], content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
@@ -289,7 +289,7 @@ export async function main() {
.getActiveSpan() .getActiveSpan()
?.setAttribute( ?.setAttribute(
LANGFUSE_TRACE_OUTPUT_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR,
JSON.stringify(result), JSON.stringify({ output: result }),
); );
const isError = result.error !== undefined; const isError = result.error !== undefined;
const text = isError ? result.error : JSON.stringify(result, null, 2); const text = isError ? result.error : JSON.stringify(result, null, 2);