Skip to content

Conversation

@mingjourney
Copy link
Collaborator

No description provided.

@mingjourney mingjourney force-pushed the feature/telemetry-trace branch from 2db6fcd to 7337f4e Compare November 6, 2025 09:47
for await (const llmResponse of this.callLlmAsync(
const span = tracer.startSpan('call_llm');
const ctx = trace.setSpan(context.active(), span);
yield* bindAsyncGenerator(ctx, (async function* (this: LlmAgent) {
Copy link
Collaborator

@kalenkevich kalenkevich Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be confusing to be me as here few things happens:

  1. OTEL context is bidding to the generator function
  2. with .call(this) you call generatorFuction with specified function context

2 context binding happening here for 2 different purposes and this is tricky to understand
I think we can improve that a bit, final function will look like a wrapper:

Suggested change
yield* bindAsyncGenerator(ctx, (async function* (this: LlmAgent) {
yield* runAsyncGeneratorWithOtelContext(otelCtx, this, async function* () {
...
});

then in tracing.ts

Suggested change
yield* bindAsyncGenerator(ctx, (async function* (this: LlmAgent) {
function bindOtelContextToAsyncGenerator<T = unknown, TReturn = any, TNext = unknown>(
ctx: Context,
generator: AsyncGenerator<T, TReturn, TNext>,
): AsyncGenerator<T, TReturn, TNext> {
return {
next: context.bind(ctx, generator.next.bind(generator)),
return: context.bind(ctx, generator.return.bind(generator)),
throw: context.bind(ctx, generator.throw.bind(generator)),
[Symbol.asyncIterator]() {
return bindOtelContextToAsyncGenerator(ctx, generator[Symbol.asyncIterator]());
},
};
}
export function runAsyncGeneratorWithOtelContext<T = unknown, TReturn = any, TNext = unknown>(
otelContext: Context,
generatorFnContext: unknown,
generatorFn: () => AsyncGenerator<T, TReturn, TNext>
): AsyncGenerator<T, TReturn, TNext> {
// call generator function with 'this' context available inside.
const generator = generatorFn().call(generatorFnContext);
return bindOtelContextToAsyncGenerator(otelContext, generator);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks like a good improvement. I'll test it out.

@mingjourney mingjourney force-pushed the feature/telemetry-trace branch from 7337f4e to 600b064 Compare November 13, 2025 03:22
@mingjourney
Copy link
Collaborator Author

sync this branch with main to clean up the diff

… the code

add runAsyncGeneratorWithOtelContext
@mingjourney mingjourney force-pushed the feature/telemetry-trace branch from 600b064 to fc8eafc Compare November 13, 2025 06:14
@mingjourney
Copy link
Collaborator Author

The changes are complete, and this has been successfully bound.

*
* @returns A new async generator that executes within both contexts
*/
export function runAsyncGeneratorWithOtelContext<TThis, T>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this TThis type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this TThis type?

Yes, TThis is necessary here. It ensures type safety for the this context inside the generator function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants