Skip to content

Commit 18aae68

Browse files
committed
use root context
1 parent e5b400e commit 18aae68

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

agents/src/voice/agent_activity.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Mutex } from '@livekit/mutex';
55
import type { AudioFrame } from '@livekit/rtc-node';
66
import type { Span } from '@opentelemetry/api';
7-
import { context as otelContext, trace } from '@opentelemetry/api';
7+
import { ROOT_CONTEXT, trace } from '@opentelemetry/api';
88
import { Heap } from 'heap-js';
99
import { AsyncLocalStorage } from 'node:async_hooks';
1010
import { ReadableStream } from 'node:stream/web';
@@ -206,10 +206,11 @@ export class AgentActivity implements RecognitionHooks {
206206
async start(): Promise<void> {
207207
const unlock = await this.lock.lock();
208208
try {
209+
// Create start_agent_activity as a ROOT span (new trace) to match Python behavior
209210
const startSpan = tracer.startSpan({
210211
name: 'start_agent_activity',
211212
attributes: { [traceTypes.ATTR_AGENT_LABEL]: this.agent.id },
212-
context: this.agentSession.rootSpanContext,
213+
context: ROOT_CONTEXT,
213214
});
214215

215216
this.agent._agentActivity = this;
@@ -299,12 +300,10 @@ export class AgentActivity implements RecognitionHooks {
299300

300301
this._mainTask = Task.from(({ signal }) => this.mainTask(signal));
301302

303+
// Create on_enter as a child of start_agent_activity in the new trace
302304
const onEnterTask = tracer.startActiveSpan(async () => this.agent.onEnter(), {
303305
name: 'on_enter',
304-
context: trace.setSpan(
305-
this.agentSession.rootSpanContext || otelContext.active(),
306-
startSpan,
307-
),
306+
context: trace.setSpan(ROOT_CONTEXT, startSpan),
308307
attributes: { [traceTypes.ATTR_AGENT_LABEL]: this.agent.id },
309308
});
310309

@@ -2191,9 +2190,10 @@ export class AgentActivity implements RecognitionHooks {
21912190
}
21922191

21932192
async drain(): Promise<void> {
2193+
// Create drain_agent_activity as a ROOT span (new trace) to match Python behavior
21942194
return tracer.startActiveSpan(async (span) => this._drainImpl(span), {
21952195
name: 'drain_agent_activity',
2196-
context: this.agentSession.rootSpanContext,
2196+
context: ROOT_CONTEXT,
21972197
});
21982198
}
21992199

agents/src/voice/agent_session.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import type { AudioFrame, Room } from '@livekit/rtc-node';
55
import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';
66
import type { Context, Span } from '@opentelemetry/api';
7-
import { context as otelContext, trace } from '@opentelemetry/api';
7+
import { ROOT_CONTEXT, context as otelContext, trace } from '@opentelemetry/api';
88
import { EventEmitter } from 'node:events';
99
import type { ReadableStream } from 'node:stream/web';
1010
import {
@@ -336,11 +336,16 @@ export class AgentSession<
336336
await ctx.initRecording();
337337
}
338338

339-
this.sessionSpan = tracer.startSpan({ name: 'agent_session' });
339+
// Create agent_session as a ROOT span (new trace) to match Python behavior
340+
// This creates a separate trace for better cloud dashboard organization
341+
this.sessionSpan = tracer.startSpan({
342+
name: 'agent_session',
343+
context: ROOT_CONTEXT,
344+
});
340345

341346
// Set the session span as the active span in the context
342347
// This ensures all child spans (agent_turn, user_turn, etc.) are parented to it
343-
this.rootSpanContext = trace.setSpan(otelContext.active(), this.sessionSpan);
348+
this.rootSpanContext = trace.setSpan(ROOT_CONTEXT, this.sessionSpan);
344349

345350
await this._startImpl({
346351
agent,

0 commit comments

Comments
 (0)