Skip to content
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

add ChatModelSpanContributor #1260

Merged
merged 1 commit into from
Feb 5, 2025
Merged

add ChatModelSpanContributor #1260

merged 1 commit into from
Feb 5, 2025

Conversation

flyinfish
Copy link

enabling adding more/custom attributes to span, namely before span is closed

for onRequest this would already be possible just using another ChatModelListener. not so unfortunately for onResponse as span is ended in SpanChatModelListener.onResponse() and so additional attributes land on parent-span overwriting each other with every new request.

this change is just missing some Processor Magic producing a default-ExtendedSpanChatModelListener and some docs may-be.

@flyinfish flyinfish requested a review from a team as a code owner February 3, 2025 07:20
@flyinfish flyinfish marked this pull request as draft February 3, 2025 07:20
@geoand
Copy link
Collaborator

geoand commented Feb 3, 2025

Interesting!

I will have a closer look in a few days as I am traveling

@flyinfish flyinfish force-pushed the 1256 branch 3 times, most recently from 4b3e886 to d98170f Compare February 4, 2025 07:21
@flyinfish
Copy link
Author

@geoand @brunobat
code should be ok like this!?, still missing docs but let's agree first if you like to have it merged

.setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(AiService.class, EchoChatLanguageModelSupplier.class))
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-opentelemetry", "3.15.2")));
Copy link
Author

Choose a reason for hiding this comment

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

@geoand
"3.15.2" any idea where this is supposed to come from?
or is there a simpler solution to it adding otel?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll need to externalize the version, but just go ahead and copy it for now if you need to add a new test.

@flyinfish flyinfish changed the title draft: add extended-span-chat-model-listener draft: add ChatModelSpanContributor Feb 4, 2025
@geoand
Copy link
Collaborator

geoand commented Feb 4, 2025

How about using @All ListChatModelSpanContributor> as the injection point.

so we can support multiple contributors? That way we also won't need the default bean.

@brunobat
Copy link

brunobat commented Feb 4, 2025

By principal, we should own the list of attributes to be placed in a span, to make sure we have all the important attributes covered.
Custom ad-hock attribute names, out of conventions, will never be considered by APM tools and users will have to handle their schema themselves.
Saying this, if ppl really want the feature, we should allow it with a warning.

@geoand
Copy link
Collaborator

geoand commented Feb 4, 2025

if ppl really want the feature, we should allow it with a warning.

I don't think this is necessary. We can add the proper documentation and Javadoc warning about this.

@flyinfish flyinfish force-pushed the 1256 branch 3 times, most recently from 4f9203a to 8d7bc45 Compare February 4, 2025 17:02
aiService.test();

await().untilAsserted(() -> assertThat(exporter.getFinishedSpanItems()).hasSize(1));
assertThat(exporter.getFinishedSpanItems()).extracting(SpanData::getName)
Copy link
Author

Choose a reason for hiding this comment

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

@geoand this will get nasty
i have yet to understand how spans are built in test - do you wanna go this road?
or may be a more stable / Mockito approach

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess for the purposes of this test, we can go with a mock approach (although I'm not a huge fan)

Copy link
Author

Choose a reason for hiding this comment

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

code should be ready, still missing version to be externalized
there is one "bug" i think i fixed that span was not ended in onError

Copy link
Collaborator

Choose a reason for hiding this comment

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

code should be ready, still missing version to be externalized

I'll do that after the PR is merged

@flyinfish
Copy link
Author

By principal, we should own the list of attributes to be placed in a span, to make sure we have all the important attributes covered. Custom ad-hock attribute names, out of conventions, will never be considered by APM tools and users will have to handle their schema themselves. Saying this, if ppl really want the feature, we should allow it with a warning.

@brunobat this solution enables different attribute-sets for every "APM worth having" to be collected. they could be enabled by config

Copy link
Collaborator

@geoand geoand left a comment

Choose a reason for hiding this comment

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

Very nice! I only added a few very minor comments

@@ -53,6 +58,7 @@ public void onRequest(ChatModelRequestContext requestContext) {
var attributes = requestContext.attributes();
attributes.put(OTEL_SCOPE_KEY_NAME, scope);
attributes.put(OTEL_SPAN_KEY_NAME, span);
chatModelSpanContributors.forEach(c -> invokeSafeWithRecordException(span, () -> c.onRequest(requestContext, span)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's please avoid lambdas in this code. We generally strive for this principle in code that is executed at runtime

Copy link
Author

Choose a reason for hiding this comment

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

np... - but how comes?


== AI method declaration
=== AI method declaration
Copy link
Author

Choose a reason for hiding this comment

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

@geoand current doc broken here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like it!

@flyinfish
Copy link
Author

hope i am done!?

@flyinfish flyinfish changed the title draft: add ChatModelSpanContributor add ChatModelSpanContributor Feb 5, 2025
@flyinfish flyinfish marked this pull request as ready for review February 5, 2025 13:30

This comment has been minimized.

This comment has been minimized.

enabling users adding more/custom attributes/data to span
enabling us to add pre-built attribute-sets for diffrent APMs

Closes quarkiverse#1256

This comment has been minimized.

Copy link
Collaborator

@geoand geoand left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

This comment has been minimized.

Copy link

quarkus-bot bot commented Feb 5, 2025

Status for workflow Build (on pull request)

This is the status report for running Build (on pull request) on commit 73af979.

✅ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

@geoand geoand merged commit 97cb976 into quarkiverse:main Feb 5, 2025
72 checks passed
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.

3 participants