|
4 | 4 | import { Mutex } from '@livekit/mutex'; |
5 | 5 | import type { AudioFrame } from '@livekit/rtc-node'; |
6 | 6 | import type { Span } from '@opentelemetry/api'; |
7 | | -import { context as otelContext, trace } from '@opentelemetry/api'; |
| 7 | +import { ROOT_CONTEXT, trace } from '@opentelemetry/api'; |
8 | 8 | import { Heap } from 'heap-js'; |
9 | 9 | import { AsyncLocalStorage } from 'node:async_hooks'; |
10 | 10 | import { ReadableStream } from 'node:stream/web'; |
@@ -206,10 +206,11 @@ export class AgentActivity implements RecognitionHooks { |
206 | 206 | async start(): Promise<void> { |
207 | 207 | const unlock = await this.lock.lock(); |
208 | 208 | try { |
| 209 | + // Create start_agent_activity as a ROOT span (new trace) to match Python behavior |
209 | 210 | const startSpan = tracer.startSpan({ |
210 | 211 | name: 'start_agent_activity', |
211 | 212 | attributes: { [traceTypes.ATTR_AGENT_LABEL]: this.agent.id }, |
212 | | - context: this.agentSession.rootSpanContext, |
| 213 | + context: ROOT_CONTEXT, |
213 | 214 | }); |
214 | 215 |
|
215 | 216 | this.agent._agentActivity = this; |
@@ -299,12 +300,10 @@ export class AgentActivity implements RecognitionHooks { |
299 | 300 |
|
300 | 301 | this._mainTask = Task.from(({ signal }) => this.mainTask(signal)); |
301 | 302 |
|
| 303 | + // Create on_enter as a child of start_agent_activity in the new trace |
302 | 304 | const onEnterTask = tracer.startActiveSpan(async () => this.agent.onEnter(), { |
303 | 305 | name: 'on_enter', |
304 | | - context: trace.setSpan( |
305 | | - this.agentSession.rootSpanContext || otelContext.active(), |
306 | | - startSpan, |
307 | | - ), |
| 306 | + context: trace.setSpan(ROOT_CONTEXT, startSpan), |
308 | 307 | attributes: { [traceTypes.ATTR_AGENT_LABEL]: this.agent.id }, |
309 | 308 | }); |
310 | 309 |
|
@@ -2191,9 +2190,10 @@ export class AgentActivity implements RecognitionHooks { |
2191 | 2190 | } |
2192 | 2191 |
|
2193 | 2192 | async drain(): Promise<void> { |
| 2193 | + // Create drain_agent_activity as a ROOT span (new trace) to match Python behavior |
2194 | 2194 | return tracer.startActiveSpan(async (span) => this._drainImpl(span), { |
2195 | 2195 | name: 'drain_agent_activity', |
2196 | | - context: this.agentSession.rootSpanContext, |
| 2196 | + context: ROOT_CONTEXT, |
2197 | 2197 | }); |
2198 | 2198 | } |
2199 | 2199 |
|
|
0 commit comments