-
Notifications
You must be signed in to change notification settings - Fork 2
TRACY-111: Parse providers' server-sent events and feed them to tracing adapters #227
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
Open
Vladislav0Art
wants to merge
67
commits into
main
Choose a base branch
from
vartiukhov/refactoring/change-tracing-of-event-stream-responses
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
91d4e2e
feat: generate SSE parser
Vladislav0Art a7a0068
feat: create tests for SSE parser (spec's example 3 is incorrect)
Vladislav0Art ace712a
fix: remove example 3 from sse parser tests
Vladislav0Art 17c8897
feat: add comments to implementation of `SseParser`
Vladislav0Art 1970d53
refactor
Vladislav0Art 89a7f73
feat: add TODO in `SseParserTest`
Vladislav0Art a1fa4d9
feat: introduce `LLMTracingAdapter.handleStreamingEvent` method
Vladislav0Art aca23ba
feat: remove Tracy request param from adapter's `getSpanName`
Vladislav0Art 836378c
feat: use `getSpanName` in Ktor and OkHttp interceptors
Vladislav0Art aedbba2
feat: identify streaming response by response body content type
Vladislav0Art 4552ceb
feat: write tests with OpenAI SSE events
Vladislav0Art 6dbfa47
feat: add explanation comments in `OpenTelemetryOkHttpInterceptor`
Vladislav0Art fb191a2
fix: correctly trace completion event of OpenAI Responses API
Vladislav0Art cb063cb
feat: rename Tracy SSE events count attribute key
Vladislav0Art d14c800
refactor: remove commented code
Vladislav0Art dd3afe3
refactor: imports
Vladislav0Art 3c9cd9c
refactor: return `Result<Unit>` from SSE event handling methods
Vladislav0Art 7a56f47
fix: avoid allocations of `AttributeKey`
Vladislav0Art 70f9ea4
feat: unconditionally set content type in `registerResponse`
Vladislav0Art 6cf0068
refactor: make `SseCapturingSource` private in `OpenTelemetryOkHttpIn…
Vladislav0Art 677fa3d
fix: close SSE parser in Ktor interceptor
Vladislav0Art e18977f
fix: return index 0 in Chat Completions SSE event handling
Vladislav0Art 94ec8d2
feat(`LLMTracingAdapter`): warn on SSE event parsing failure
Vladislav0Art c659c39
feat: remove TODO
Vladislav0Art 5b719b3
fix: correctly trace assistant content for chat completions SSE events
Vladislav0Art 7e4389e
feat: rename `gen_ai.response.streaming` -> `gen_ai.response.sse.stre…
Vladislav0Art e266704
feat: update KDoc of `registerResponseStreamEvent`
Vladislav0Art e45d61e
feat: call logger's debug instead of warn
Vladislav0Art a485d3d
refactor: fix mime type name in comments
Vladislav0Art 69fbbe1
refactor: comment (color -> colon) in SseParser.kt
Vladislav0Art edf5f27
refactor: mention variable type `Long`
Vladislav0Art d7d5a83
refactor(`OpenAIApiUtils`): convert to `jsonPrimitive` and extract va…
Vladislav0Art 4457403
refactor: fix usage example in `LLMTracingAdapter`'s KDoc
Vladislav0Art a4fc174
fix: prefix attribute `gen_ai.response.sse.streaming` with `tracy` in…
Vladislav0Art 7a175f5
refactor(`AGENTS.md`): update comment about `LLMTracingAdapter`
Vladislav0Art 6c15c2f
feat: change plugin name in `createClientPlugin` call
Vladislav0Art 9cd1142
refactor: move SSE tracing in Ktor plugin into separate function
Vladislav0Art 1829acd
feat: update Anthropic span name in accordance with GenAI
Vladislav0Art 692a997
feat: add `asString` to `TracyHttpUrl`
Vladislav0Art 18b34a9
feat: create `sseHandlingUnsupported` function
Vladislav0Art 3b99009
refactor: remove redundant `?.`
Vladislav0Art fb90bc3
feat: use `sseHandlingUnsupported` in Anthropic adapter and Gemini ha…
Vladislav0Art c31dd93
feat: print warning about unsupported SSE event handling only once pe…
Vladislav0Art 9264178
feat: call `getResponseBodyAttributes` for any non-SSE response type
Vladislav0Art 85e9321
feat: re-implement `TracyHttpUrl.asString`
Vladislav0Art ed1625a
refactor: imports
Vladislav0Art 9c762af
fix: check for nullness of `mimeType` in `LLMTracingAdapter`
Vladislav0Art 023618b
feat(`VideosOpenAIApiEndpointHandler`): mark stream tracing as unsupp…
Vladislav0Art aff96b1
refactor: comment wording
Vladislav0Art d8ca934
refactor: comment wording
Vladislav0Art e5f1724
fix: set `retryValue` to `null` when dispatching
Vladislav0Art fd2636b
fix: use streaming UTF-8 CharsetDecoder in Ktor SSE tracing to handle…
Copilot 95c4664
feat(`SseParser`): dispatch final event if any on close
Vladislav0Art 5c140ef
fix: set `endOfInput` as `originalBody.isClosedForRead`
Vladislav0Art 630ab43
refactor: imports
Vladislav0Art ff430c3
refactor: move call of `traceServerSentEvents` into `transformRespons…
Vladislav0Art c4c3d9a
feat: don't swallow exceptions in sse parsing
Vladislav0Art 52e0bc0
feat: create stateful `UTF8Decoder`
Vladislav0Art f0ad2e0
feat(Ktor): use `UTF8Decoder` to parse SSE events
Vladislav0Art db91929
feat(`OpenTelemetryOkHttpInterceptor`): use `UTF8Decoder` to parse SS…
Vladislav0Art 655486a
feat: make `sseHandlingUnsupportedWarningPrinted` an `AtomicBoolean`
Vladislav0Art 5687846
feat: do the final decoding and flush remaining bytes
Vladislav0Art 41e1184
feat: create a test suite for `UTF8Decoder` (WIP)
Vladislav0Art fdfebda
feat: add comment about `sseHandlingUnsupportedWarningPrinted`
Vladislav0Art f0befc2
feat(Ktor): generate comment on why `peek` should come before `awaitC…
Vladislav0Art 0c9fe5e
feat: generate tests for UTF-8 decoder
Vladislav0Art a010de8
feat: close span at the end of `finally` block
Vladislav0Art File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
.../core/src/jvmMain/kotlin/org/jetbrains/ai/tracy/core/adapters/handlers/sse/SseHandling.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright © 2026 JetBrains s.r.o. and contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package org.jetbrains.ai.tracy.core.adapters.handlers.sse | ||
|
|
||
| /** | ||
| * Wraps [SseEventHandlingException] into a [Result] indicating failure. | ||
| * Should be used when handling of SSE events failed. | ||
| * | ||
| * @see org.jetbrains.ai.tracy.core.adapters.handlers.EndpointApiHandler.handleStreamingEvent | ||
| * @see sseHandlingUnsupported | ||
| */ | ||
| fun sseHandlingFailure(message: String): Result<Unit> { | ||
| return Result.failure(SseEventHandlingException(message)) | ||
| } | ||
|
|
||
| /** | ||
| * Wraps [SseEventHandlingUnsupported] into a [Result] indicating | ||
| * that SSE handling is not supported in this call site. | ||
| * | ||
| * @see sseHandlingFailure | ||
| */ | ||
| fun sseHandlingUnsupported(): Result<Unit> { | ||
| return Result.failure(SseEventHandlingUnsupported()) | ||
| } | ||
|
|
||
| class SseEventHandlingException(message: String, cause: Throwable? = null) : RuntimeException(message, cause) | ||
|
|
||
| class SseEventHandlingUnsupported : RuntimeException("SSE event handling is not supported yet") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you, please, align span naming with GenAI conventions
Span names now follow {gen_ai.operation.name} {gen_ai.request.model}:
https://opentelemetry.io/docs/specs/semconv/gen-ai/anthropic/
Uh oh!
There was an error while loading. Please reload this page.
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.
Addressed in 9fe5749
I didn't modify this
"Anthropic-generation"name here because I have no information about the model and operation (both of which are only known at the stage of parsing the response body).Notes:
See the trace.