chore(llmobs): minimize number of span/eval encoding #13273
Closed
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.
This PR does an experimental refactor to minimize the number of JSON encoding attempts we make per span/eval event.
Previously, we call
safe_json()
(which is just a wrapper around json.dumps()) the following times per span/eval event:enqueue()
to calculate the event size to determine if we should truncate the event due to size limitsencoder.put()
to again... calculate the event size to increase the encoder's buffer size accordingyencoder.encode()
to finally JSON encode the payload before flushing.Thanks to #12966 we got rid of 2), but we still encode the same event 2 times before sending. Considering the average size of span event payloads, this is definitely redundancy that needs to go.
This PR attempts to solve this issue by encoding every span/eval metric only one time at
enqueue()
, and stores the encoded events as byte/strings in the writer buffer. At flush time, we create the encoded payload by concatenating the buffer strings together. We need to move encoding to enqueue time because we have some logic relying on encoded event sizes.The tradeoffs here are:
Checklist
Reviewer Checklist