@townco/agent 0.1.51 → 0.1.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/acp-server/adapter.d.ts +2 -0
- package/dist/acp-server/adapter.js +28 -3
- package/dist/acp-server/index.js +5 -0
- package/dist/acp-server/session-storage.d.ts +2 -0
- package/dist/acp-server/session-storage.js +2 -0
- package/dist/runner/agent-runner.d.ts +4 -0
- package/dist/runner/langchain/index.d.ts +0 -1
- package/dist/runner/langchain/index.js +88 -20
- package/dist/runner/langchain/otel-callbacks.js +67 -1
- package/dist/telemetry/setup.d.ts +3 -1
- package/dist/telemetry/setup.js +33 -3
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/__tests__/tool-overhead-calculator.test.d.ts +1 -0
- package/dist/utils/__tests__/tool-overhead-calculator.test.js +153 -0
- package/dist/utils/context-size-calculator.d.ts +9 -4
- package/dist/utils/context-size-calculator.js +23 -6
- package/dist/utils/tool-overhead-calculator.d.ts +30 -0
- package/dist/utils/tool-overhead-calculator.js +54 -0
- package/package.json +6 -6
|
@@ -24,6 +24,8 @@ export declare class AgentAcpAdapter implements acp.Agent {
|
|
|
24
24
|
private agentVersion;
|
|
25
25
|
private agentDescription;
|
|
26
26
|
private agentSuggestedPrompts;
|
|
27
|
+
private currentToolOverheadTokens;
|
|
28
|
+
private currentMcpOverheadTokens;
|
|
27
29
|
constructor(agent: AgentRunner, connection: acp.AgentSideConnection, agentDir?: string, agentName?: string);
|
|
28
30
|
/**
|
|
29
31
|
* Helper to save session to disk
|
|
@@ -103,6 +103,8 @@ export class AgentAcpAdapter {
|
|
|
103
103
|
agentVersion;
|
|
104
104
|
agentDescription;
|
|
105
105
|
agentSuggestedPrompts;
|
|
106
|
+
currentToolOverheadTokens = 0; // Track tool overhead for current turn
|
|
107
|
+
currentMcpOverheadTokens = 0; // Track MCP overhead for current turn
|
|
106
108
|
constructor(agent, connection, agentDir, agentName) {
|
|
107
109
|
this.connection = connection;
|
|
108
110
|
this.sessions = new Map();
|
|
@@ -338,6 +340,9 @@ export class AgentAcpAdapter {
|
|
|
338
340
|
}
|
|
339
341
|
session.pendingPrompt?.abort();
|
|
340
342
|
session.pendingPrompt = new AbortController();
|
|
343
|
+
// Reset tool overhead for new turn (will be set by harness)
|
|
344
|
+
this.currentToolOverheadTokens = 0;
|
|
345
|
+
this.currentMcpOverheadTokens = 0;
|
|
341
346
|
// Generate a unique messageId for this assistant response
|
|
342
347
|
const messageId = Math.random().toString(36).substring(2);
|
|
343
348
|
// Extract and store the user message
|
|
@@ -397,7 +402,9 @@ export class AgentAcpAdapter {
|
|
|
397
402
|
}
|
|
398
403
|
}
|
|
399
404
|
// Calculate context size - no LLM call yet, so only estimated values
|
|
400
|
-
const context_size = calculateContextSize(contextMessages, this.agent.definition.systemPrompt ?? undefined, undefined
|
|
405
|
+
const context_size = calculateContextSize(contextMessages, this.agent.definition.systemPrompt ?? undefined, undefined, // No LLM-reported tokens yet
|
|
406
|
+
this.currentToolOverheadTokens, // Include tool overhead
|
|
407
|
+
this.currentMcpOverheadTokens);
|
|
401
408
|
const contextSnapshot = createContextSnapshot(session.messages.length, new Date().toISOString(), previousContext, context_size);
|
|
402
409
|
session.context.push(contextSnapshot);
|
|
403
410
|
await this.saveSessionToDisk(params.sessionId, session);
|
|
@@ -461,6 +468,20 @@ export class AgentAcpAdapter {
|
|
|
461
468
|
let iterResult = await generator.next();
|
|
462
469
|
while (!iterResult.done) {
|
|
463
470
|
const msg = iterResult.value;
|
|
471
|
+
// Capture tool overhead info if provided by harness
|
|
472
|
+
if ("sessionUpdate" in msg &&
|
|
473
|
+
msg.sessionUpdate === "tool_overhead_info") {
|
|
474
|
+
const overheadInfo = msg;
|
|
475
|
+
this.currentToolOverheadTokens = overheadInfo.toolOverheadTokens;
|
|
476
|
+
this.currentMcpOverheadTokens = overheadInfo.mcpOverheadTokens;
|
|
477
|
+
logger.debug("Received tool overhead info from harness", {
|
|
478
|
+
toolOverheadTokens: this.currentToolOverheadTokens,
|
|
479
|
+
mcpOverheadTokens: this.currentMcpOverheadTokens,
|
|
480
|
+
});
|
|
481
|
+
// Don't send this update to client, it's internal metadata
|
|
482
|
+
iterResult = await generator.next();
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
464
485
|
// Extract and accumulate token usage from message chunks
|
|
465
486
|
if ("sessionUpdate" in msg &&
|
|
466
487
|
msg.sessionUpdate === "agent_message_chunk" &&
|
|
@@ -659,7 +680,9 @@ export class AgentAcpAdapter {
|
|
|
659
680
|
}
|
|
660
681
|
}
|
|
661
682
|
// Calculate context size - tool result is now in the message, but hasn't been sent to LLM yet
|
|
662
|
-
const context_size = calculateContextSize(contextMessages, this.agent.definition.systemPrompt ?? undefined, undefined
|
|
683
|
+
const context_size = calculateContextSize(contextMessages, this.agent.definition.systemPrompt ?? undefined, undefined, // Tool result hasn't been sent to LLM yet, so no new LLM-reported tokens
|
|
684
|
+
this.currentToolOverheadTokens, // Include tool overhead
|
|
685
|
+
this.currentMcpOverheadTokens);
|
|
663
686
|
// Create snapshot with a pointer to the partial message (not a full copy!)
|
|
664
687
|
const midTurnSnapshot = {
|
|
665
688
|
timestamp: new Date().toISOString(),
|
|
@@ -779,7 +802,9 @@ export class AgentAcpAdapter {
|
|
|
779
802
|
}
|
|
780
803
|
}
|
|
781
804
|
// Calculate context size with LLM-reported tokens from this turn
|
|
782
|
-
const context_size = calculateContextSize(contextMessages, this.agent.definition.systemPrompt ?? undefined, turnTokenUsage.inputTokens
|
|
805
|
+
const context_size = calculateContextSize(contextMessages, this.agent.definition.systemPrompt ?? undefined, turnTokenUsage.inputTokens, // Final LLM-reported tokens from this turn
|
|
806
|
+
this.currentToolOverheadTokens, // Include tool overhead
|
|
807
|
+
this.currentMcpOverheadTokens);
|
|
783
808
|
const contextSnapshot = createContextSnapshot(session.messages.length, new Date().toISOString(), previousContext, context_size);
|
|
784
809
|
session.context.push(contextSnapshot);
|
|
785
810
|
await this.saveSessionToDisk(params.sessionId, session);
|
package/dist/acp-server/index.js
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
+
import { initializeOpenTelemetryFromEnv } from "../telemetry/setup.js";
|
|
2
|
+
// Initialize OpenTelemetry when this module is imported (if enabled)
|
|
3
|
+
if (process.env.ENABLE_TELEMETRY === "true") {
|
|
4
|
+
initializeOpenTelemetryFromEnv();
|
|
5
|
+
}
|
|
1
6
|
export { makeStdioTransport } from "./cli";
|
|
2
7
|
export { makeHttpTransport } from "./http";
|
|
@@ -62,6 +62,8 @@ export interface ContextEntry {
|
|
|
62
62
|
*/
|
|
63
63
|
context_size: {
|
|
64
64
|
systemPromptTokens: number;
|
|
65
|
+
toolOverheadTokens?: number | undefined;
|
|
66
|
+
mcpOverheadTokens?: number | undefined;
|
|
65
67
|
userMessagesTokens: number;
|
|
66
68
|
assistantMessagesTokens: number;
|
|
67
69
|
toolInputTokens: number;
|
|
@@ -58,6 +58,8 @@ const contextEntrySchema = z.object({
|
|
|
58
58
|
compactedUpTo: z.number().optional(),
|
|
59
59
|
context_size: z.object({
|
|
60
60
|
systemPromptTokens: z.number(),
|
|
61
|
+
toolOverheadTokens: z.number().optional(),
|
|
62
|
+
mcpOverheadTokens: z.number().optional(),
|
|
61
63
|
userMessagesTokens: z.number(),
|
|
62
64
|
assistantMessagesTokens: z.number(),
|
|
63
65
|
toolInputTokens: z.number(),
|
|
@@ -109,6 +109,10 @@ export type ExtendedSessionUpdate = (SessionNotification["update"] & {
|
|
|
109
109
|
contextInputTokens?: number;
|
|
110
110
|
[key: string]: unknown;
|
|
111
111
|
};
|
|
112
|
+
} | {
|
|
113
|
+
sessionUpdate: "tool_overhead_info";
|
|
114
|
+
toolOverheadTokens: number;
|
|
115
|
+
mcpOverheadTokens: number;
|
|
112
116
|
} | AgentMessageChunkWithTokens | HookNotificationUpdate;
|
|
113
117
|
/** Describes an object that can run an agent definition */
|
|
114
118
|
export interface AgentRunner {
|
|
@@ -10,7 +10,6 @@ type MakeLazy<T> = T extends LangchainTool ? () => T : never;
|
|
|
10
10
|
export declare const TOOL_REGISTRY: Record<BuiltInToolType, LangchainTool | LazyLangchainTool | LazyLangchainTools>;
|
|
11
11
|
export declare class LangchainAgent implements AgentRunner {
|
|
12
12
|
definition: CreateAgentRunnerParams;
|
|
13
|
-
private toolSpans;
|
|
14
13
|
constructor(params: CreateAgentRunnerParams);
|
|
15
14
|
invoke(req: InvokeRequest): AsyncGenerator<ExtendedSessionUpdate, PromptResponse, undefined>;
|
|
16
15
|
}
|
|
@@ -54,7 +54,6 @@ async function loadCustomTools(modulePaths) {
|
|
|
54
54
|
}
|
|
55
55
|
export class LangchainAgent {
|
|
56
56
|
definition;
|
|
57
|
-
toolSpans = new Map();
|
|
58
57
|
constructor(params) {
|
|
59
58
|
this.definition = params;
|
|
60
59
|
}
|
|
@@ -157,10 +156,41 @@ export class LangchainAgent {
|
|
|
157
156
|
const customTools = await loadCustomTools(customToolPaths);
|
|
158
157
|
enabledTools.push(...customTools);
|
|
159
158
|
}
|
|
160
|
-
// MCP tools
|
|
159
|
+
// Calculate tool overhead tokens for non-MCP tools
|
|
160
|
+
const { countTokens } = await import("../../utils/token-counter.js");
|
|
161
|
+
const { extractToolMetadata, estimateAllToolsOverhead } = await import("../../utils/tool-overhead-calculator.js");
|
|
162
|
+
// Calculate overhead for non-MCP tools (built-in, custom, filesystem)
|
|
163
|
+
const nonMcpToolMetadata = enabledTools.map(extractToolMetadata);
|
|
164
|
+
const nonMcpToolDefinitionsTokens = estimateAllToolsOverhead(nonMcpToolMetadata);
|
|
165
|
+
// Calculate TODO_WRITE_INSTRUCTIONS overhead if applicable
|
|
166
|
+
const hasTodoWriteTool = builtInNames.includes("todo_write");
|
|
167
|
+
const todoInstructionsTokens = hasTodoWriteTool
|
|
168
|
+
? countTokens(TODO_WRITE_INSTRUCTIONS)
|
|
169
|
+
: 0;
|
|
170
|
+
// Total non-MCP tool overhead: tool definitions + TODO instructions
|
|
171
|
+
const toolOverheadTokens = nonMcpToolDefinitionsTokens + todoInstructionsTokens;
|
|
172
|
+
// MCP tools - calculate overhead separately
|
|
173
|
+
let mcpOverheadTokens = 0;
|
|
161
174
|
if ((this.definition.mcps?.length ?? 0) > 0) {
|
|
162
|
-
|
|
175
|
+
const mcpTools = await makeMcpToolsClient(this.definition.mcps).getTools();
|
|
176
|
+
const mcpToolMetadata = mcpTools.map(extractToolMetadata);
|
|
177
|
+
mcpOverheadTokens = estimateAllToolsOverhead(mcpToolMetadata);
|
|
178
|
+
enabledTools.push(...mcpTools);
|
|
163
179
|
}
|
|
180
|
+
_logger.debug("Calculated tool overhead for context sizing", {
|
|
181
|
+
enabledToolCount: enabledTools.length,
|
|
182
|
+
nonMcpToolDefinitionsTokens,
|
|
183
|
+
mcpToolDefinitionsTokens: mcpOverheadTokens,
|
|
184
|
+
todoInstructionsTokens,
|
|
185
|
+
totalNonMcpOverheadTokens: toolOverheadTokens,
|
|
186
|
+
totalMcpOverheadTokens: mcpOverheadTokens,
|
|
187
|
+
});
|
|
188
|
+
// Yield tool overhead info to adapter early in the turn
|
|
189
|
+
yield {
|
|
190
|
+
sessionUpdate: "tool_overhead_info",
|
|
191
|
+
toolOverheadTokens,
|
|
192
|
+
mcpOverheadTokens,
|
|
193
|
+
};
|
|
164
194
|
// Wrap tools with response compaction if hook is configured
|
|
165
195
|
const hooks = this.definition.hooks ?? [];
|
|
166
196
|
const hasToolResponseHook = hooks.some((h) => h.type === "tool_response");
|
|
@@ -255,9 +285,12 @@ export class LangchainAgent {
|
|
|
255
285
|
}
|
|
256
286
|
// Filter tools if running in subagent mode
|
|
257
287
|
const isSubagent = req.sessionMeta?.[SUBAGENT_MODE_KEY] === true;
|
|
258
|
-
const
|
|
288
|
+
const filteredTools = isSubagent
|
|
259
289
|
? wrappedTools.filter((t) => t.name !== TODO_WRITE_TOOL_NAME && t.name !== TASK_TOOL_NAME)
|
|
260
290
|
: wrappedTools;
|
|
291
|
+
// Wrap tools with tracing so each tool executes within its own span context.
|
|
292
|
+
// This ensures subagent spans are children of the Task tool span.
|
|
293
|
+
const finalTools = filteredTools.map((t) => wrapToolWithTracing(t, req.sessionId));
|
|
261
294
|
// Create the model instance using the factory
|
|
262
295
|
// This detects the provider from the model string:
|
|
263
296
|
// - "gemini-2.0-flash" → Google Generative AI
|
|
@@ -362,13 +395,6 @@ export class LangchainAgent {
|
|
|
362
395
|
if (toolCall.id == null) {
|
|
363
396
|
throw new Error(`Tool call is missing id: ${JSON.stringify(toolCall)}`);
|
|
364
397
|
}
|
|
365
|
-
// Create tool span within the invocation context
|
|
366
|
-
// This makes the tool span a child of the invocation span
|
|
367
|
-
const toolSpan = context.with(invocationContext, () => telemetry.startSpan("agent.tool_call", {
|
|
368
|
-
"tool.name": toolCall.name,
|
|
369
|
-
"tool.id": toolCall.id,
|
|
370
|
-
}));
|
|
371
|
-
this.toolSpans.set(toolCall.id, toolSpan);
|
|
372
398
|
telemetry.log("info", `Tool call started: ${toolCall.name}`, {
|
|
373
399
|
toolCallId: toolCall.id,
|
|
374
400
|
toolName: toolCall.name,
|
|
@@ -550,15 +576,9 @@ export class LangchainAgent {
|
|
|
550
576
|
// Skip tool_call_update for todo_write tools
|
|
551
577
|
continue;
|
|
552
578
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
telemetry.log("info", "Tool call completed", {
|
|
557
|
-
toolCallId: aiMessage.tool_call_id,
|
|
558
|
-
});
|
|
559
|
-
telemetry.endSpan(toolSpan);
|
|
560
|
-
this.toolSpans.delete(aiMessage.tool_call_id);
|
|
561
|
-
}
|
|
579
|
+
telemetry.log("info", "Tool call completed", {
|
|
580
|
+
toolCallId: aiMessage.tool_call_id,
|
|
581
|
+
});
|
|
562
582
|
// Send status update (metadata only, no content)
|
|
563
583
|
yield {
|
|
564
584
|
sessionUpdate: "tool_call_update",
|
|
@@ -708,3 +728,51 @@ I've found some existing telemetry code. Let me mark the first todo as in_progre
|
|
|
708
728
|
`.trim();
|
|
709
729
|
// Re-export subagent tool utility
|
|
710
730
|
export { makeSubagentsTool } from "./tools/subagent.js";
|
|
731
|
+
/**
|
|
732
|
+
* Wraps a LangChain tool with OpenTelemetry tracing.
|
|
733
|
+
* This ensures the tool executes within its own span context,
|
|
734
|
+
* so any child operations (like subagent spawning) become children
|
|
735
|
+
* of the tool span rather than the parent invocation span.
|
|
736
|
+
*/
|
|
737
|
+
function wrapToolWithTracing(originalTool, sessionId) {
|
|
738
|
+
const wrappedFunc = async (input) => {
|
|
739
|
+
const toolInputJson = JSON.stringify(input);
|
|
740
|
+
const toolSpan = telemetry.startSpan("agent.tool_call", {
|
|
741
|
+
"tool.name": originalTool.name,
|
|
742
|
+
"tool.input": toolInputJson,
|
|
743
|
+
"agent.session_id": sessionId,
|
|
744
|
+
});
|
|
745
|
+
// Create a context with the tool span as active
|
|
746
|
+
const spanContext = toolSpan
|
|
747
|
+
? trace.setSpan(context.active(), toolSpan)
|
|
748
|
+
: context.active();
|
|
749
|
+
try {
|
|
750
|
+
// Execute within the tool span's context
|
|
751
|
+
const result = await context.with(spanContext, () => originalTool.invoke(input));
|
|
752
|
+
const resultStr = typeof result === "string" ? result : JSON.stringify(result);
|
|
753
|
+
if (toolSpan) {
|
|
754
|
+
telemetry.setSpanAttributes(toolSpan, {
|
|
755
|
+
"tool.output": resultStr,
|
|
756
|
+
});
|
|
757
|
+
telemetry.endSpan(toolSpan);
|
|
758
|
+
}
|
|
759
|
+
return result;
|
|
760
|
+
}
|
|
761
|
+
catch (error) {
|
|
762
|
+
if (toolSpan) {
|
|
763
|
+
telemetry.endSpan(toolSpan, error);
|
|
764
|
+
}
|
|
765
|
+
throw error;
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
// Create new tool with wrapped function
|
|
769
|
+
const wrappedTool = tool(wrappedFunc, {
|
|
770
|
+
name: originalTool.name,
|
|
771
|
+
description: originalTool.description,
|
|
772
|
+
schema: originalTool.schema,
|
|
773
|
+
});
|
|
774
|
+
// Preserve metadata
|
|
775
|
+
wrappedTool.prettyName = originalTool.prettyName;
|
|
776
|
+
wrappedTool.icon = originalTool.icon;
|
|
777
|
+
return wrappedTool;
|
|
778
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { context } from "@opentelemetry/api";
|
|
1
|
+
import { context, trace } from "@opentelemetry/api";
|
|
2
2
|
import { telemetry } from "../../telemetry/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* OpenTelemetry callback handler for LangChain LLM calls.
|
|
@@ -45,6 +45,38 @@ function extractSystemPrompt(messages) {
|
|
|
45
45
|
return undefined;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Serializes LLM output to a string for logging.
|
|
50
|
+
* Preserves the raw provider format (content blocks, tool_calls, etc.)
|
|
51
|
+
*/
|
|
52
|
+
function serializeOutput(output) {
|
|
53
|
+
try {
|
|
54
|
+
const generations = output.generations.flat();
|
|
55
|
+
const serialized = generations.map((gen) => {
|
|
56
|
+
// ChatGeneration has a message property with the full AIMessage
|
|
57
|
+
const chatGen = gen;
|
|
58
|
+
if (chatGen.message) {
|
|
59
|
+
const msg = chatGen.message;
|
|
60
|
+
const result = {
|
|
61
|
+
role: msg._getType?.() ?? "assistant",
|
|
62
|
+
content: msg.content, // Keep as-is: string or ContentBlock[]
|
|
63
|
+
};
|
|
64
|
+
// Include tool_calls if present (LangChain's normalized format)
|
|
65
|
+
const aiMsg = msg;
|
|
66
|
+
if (aiMsg.tool_calls && aiMsg.tool_calls.length > 0) {
|
|
67
|
+
result.tool_calls = aiMsg.tool_calls;
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
// Fallback for non-chat generations
|
|
72
|
+
return { text: gen.text };
|
|
73
|
+
});
|
|
74
|
+
return JSON.stringify(serialized);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return `[Error serializing output: ${error}]`;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
48
80
|
/**
|
|
49
81
|
* Creates OpenTelemetry callback handlers for LangChain LLM calls.
|
|
50
82
|
* These handlers instrument model invocations with OTEL spans and record token usage.
|
|
@@ -84,6 +116,18 @@ export function makeOtelCallbacks(opts) {
|
|
|
84
116
|
}));
|
|
85
117
|
if (span) {
|
|
86
118
|
spansByRunId.set(runId, span);
|
|
119
|
+
// Emit log for LLM request with trace context
|
|
120
|
+
const spanContext = span.spanContext();
|
|
121
|
+
telemetry.log("info", "LLM Request", {
|
|
122
|
+
"gen_ai.operation.name": "chat",
|
|
123
|
+
"gen_ai.provider.name": opts.provider,
|
|
124
|
+
"gen_ai.request.model": opts.model,
|
|
125
|
+
"gen_ai.input.messages": serializedMessages,
|
|
126
|
+
"langchain.run_id": runId,
|
|
127
|
+
// Include trace context for correlation
|
|
128
|
+
trace_id: spanContext.traceId,
|
|
129
|
+
span_id: spanContext.spanId,
|
|
130
|
+
});
|
|
87
131
|
}
|
|
88
132
|
},
|
|
89
133
|
/**
|
|
@@ -105,6 +149,28 @@ export function makeOtelCallbacks(opts) {
|
|
|
105
149
|
: 0);
|
|
106
150
|
telemetry.recordTokenUsage(inputTokens, outputTokens, span);
|
|
107
151
|
}
|
|
152
|
+
// Serialize output and attach to span
|
|
153
|
+
const serializedOutput = serializeOutput(output);
|
|
154
|
+
telemetry.setSpanAttributes(span, {
|
|
155
|
+
"gen_ai.output.messages": serializedOutput,
|
|
156
|
+
});
|
|
157
|
+
// Emit log for LLM response with trace context
|
|
158
|
+
const spanContext = span.spanContext();
|
|
159
|
+
telemetry.log("info", "LLM Response", {
|
|
160
|
+
"gen_ai.operation.name": "chat",
|
|
161
|
+
"gen_ai.output.messages": serializedOutput,
|
|
162
|
+
"langchain.run_id": runId,
|
|
163
|
+
// Include token usage in log
|
|
164
|
+
...(tokenUsage
|
|
165
|
+
? {
|
|
166
|
+
"gen_ai.usage.input_tokens": tokenUsage.inputTokens ?? 0,
|
|
167
|
+
"gen_ai.usage.output_tokens": tokenUsage.outputTokens ?? 0,
|
|
168
|
+
}
|
|
169
|
+
: {}),
|
|
170
|
+
// Include trace context for correlation
|
|
171
|
+
trace_id: spanContext.traceId,
|
|
172
|
+
span_id: spanContext.spanId,
|
|
173
|
+
});
|
|
108
174
|
telemetry.endSpan(span);
|
|
109
175
|
spansByRunId.delete(runId);
|
|
110
176
|
},
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OpenTelemetry provider setup for @townco/agent
|
|
3
|
-
* Initializes the trace provider,
|
|
3
|
+
* Initializes the trace provider, log provider, exporters, and propagator
|
|
4
4
|
*/
|
|
5
|
+
import { LoggerProvider } from "@opentelemetry/sdk-logs";
|
|
5
6
|
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
6
7
|
export interface TelemetrySetupOptions {
|
|
7
8
|
serviceName?: string;
|
|
@@ -14,6 +15,7 @@ export interface TelemetrySetupOptions {
|
|
|
14
15
|
*/
|
|
15
16
|
export declare function initializeOpenTelemetry(options?: TelemetrySetupOptions): {
|
|
16
17
|
provider: NodeTracerProvider;
|
|
18
|
+
loggerProvider: LoggerProvider;
|
|
17
19
|
shutdown: () => Promise<void>;
|
|
18
20
|
};
|
|
19
21
|
/**
|
package/dist/telemetry/setup.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OpenTelemetry provider setup for @townco/agent
|
|
3
|
-
* Initializes the trace provider,
|
|
3
|
+
* Initializes the trace provider, log provider, exporters, and propagator
|
|
4
4
|
*/
|
|
5
5
|
import { propagation } from "@opentelemetry/api";
|
|
6
|
+
import { logs } from "@opentelemetry/api-logs";
|
|
6
7
|
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
8
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
7
9
|
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
8
10
|
import { Resource } from "@opentelemetry/resources";
|
|
11
|
+
import { BatchLogRecordProcessor, LoggerProvider, } from "@opentelemetry/sdk-logs";
|
|
9
12
|
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
10
13
|
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
11
14
|
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
@@ -83,13 +86,38 @@ export function initializeOpenTelemetry(options = {}) {
|
|
|
83
86
|
const batchProcessor = new BatchSpanProcessor(loggingExporter, {
|
|
84
87
|
maxQueueSize: 100,
|
|
85
88
|
maxExportBatchSize: 10,
|
|
86
|
-
scheduledDelayMillis:
|
|
89
|
+
scheduledDelayMillis: 2000, // Export every 2 seconds
|
|
87
90
|
});
|
|
88
91
|
provider.addSpanProcessor(batchProcessor);
|
|
89
92
|
// Register the provider globally
|
|
90
93
|
provider.register();
|
|
91
94
|
// Configure W3C Trace Context propagator for cross-process traces
|
|
92
95
|
propagation.setGlobalPropagator(new W3CTraceContextPropagator());
|
|
96
|
+
// Set up LoggerProvider for OTLP log export
|
|
97
|
+
const logUrl = otlpEndpoint.endsWith("/")
|
|
98
|
+
? `${otlpEndpoint}v1/logs`
|
|
99
|
+
: `${otlpEndpoint}/v1/logs`;
|
|
100
|
+
if (debug) {
|
|
101
|
+
console.log(`OTLP log URL: ${logUrl}`);
|
|
102
|
+
}
|
|
103
|
+
const logExporter = new OTLPLogExporter({
|
|
104
|
+
url: logUrl,
|
|
105
|
+
});
|
|
106
|
+
const loggerProvider = new LoggerProvider({
|
|
107
|
+
resource: new Resource({
|
|
108
|
+
[ATTR_SERVICE_NAME]: serviceName,
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
loggerProvider.addLogRecordProcessor(new BatchLogRecordProcessor(logExporter, {
|
|
112
|
+
maxQueueSize: 100,
|
|
113
|
+
maxExportBatchSize: 10,
|
|
114
|
+
scheduledDelayMillis: 2000,
|
|
115
|
+
}));
|
|
116
|
+
// Register the logger provider globally
|
|
117
|
+
logs.setGlobalLoggerProvider(loggerProvider);
|
|
118
|
+
if (debug) {
|
|
119
|
+
console.log("✓ Log exporter configured");
|
|
120
|
+
}
|
|
93
121
|
// Now configure our telemetry wrapper
|
|
94
122
|
configureTelemetry({
|
|
95
123
|
enabled: true,
|
|
@@ -106,6 +134,8 @@ export function initializeOpenTelemetry(options = {}) {
|
|
|
106
134
|
try {
|
|
107
135
|
await provider.forceFlush();
|
|
108
136
|
await provider.shutdown();
|
|
137
|
+
await loggerProvider.forceFlush();
|
|
138
|
+
await loggerProvider.shutdown();
|
|
109
139
|
if (debug) {
|
|
110
140
|
console.log("✓ Telemetry flushed");
|
|
111
141
|
}
|
|
@@ -114,7 +144,7 @@ export function initializeOpenTelemetry(options = {}) {
|
|
|
114
144
|
console.error("Error flushing telemetry:", error);
|
|
115
145
|
}
|
|
116
146
|
};
|
|
117
|
-
return { provider, shutdown };
|
|
147
|
+
return { provider, loggerProvider, shutdown };
|
|
118
148
|
}
|
|
119
149
|
/**
|
|
120
150
|
* Initialize OpenTelemetry from environment variables and register shutdown handlers
|