-
Notifications
You must be signed in to change notification settings - Fork 18
Integrate OpenTelemetry (OTel) support: 2. use telemetry functions in the code #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
2db6fcd to
7337f4e
Compare
core/src/agents/llm_agent.ts
Outdated
| 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) { |
There was a problem hiding this comment.
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:
- OTEL context is bidding to the generator function
- 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:
| yield* bindAsyncGenerator(ctx, (async function* (this: LlmAgent) { | |
| yield* runAsyncGeneratorWithOtelContext(otelCtx, this, async function* () { | |
| ... | |
| }); |
then in tracing.ts
| 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); | |
| } |
There was a problem hiding this comment.
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.
7337f4e to
600b064
Compare
|
sync this branch with main to clean up the diff |
… the code add runAsyncGeneratorWithOtelContext
600b064 to
fc8eafc
Compare
|
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>( |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this
TThistype?
Yes, TThis is necessary here. It ensures type safety for the this context inside the generator function.
No description provided.