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

feat: Added instrumentation for chat completion streams #1884

Merged
merged 3 commits into from
Nov 27, 2023

Conversation

bizob2828
Copy link
Member

@bizob2828 bizob2828 commented Nov 21, 2023

Description

This PR adds instrumentation for chat completion streams. It intercepts the stream generator and proxies the iterations through chunks. It will then update the segment time and record LLM events when it finishes. Errors are funky and thanks to @jsumners-nr for helping create a test for it. I'm convinced the openai library has a bug but didn't spend time completely tracking down. It's worth noting that we only instrument streams in 4.12.2+. The interface was different in past versions and not worth supporting both. In that case the completion span + llm events will not occur. We still will get child spans for the external http call.

I also refactored the versioned tests as 1 file was getting unwieldy. Lastly, I fixed the span duration and manually end segments. We typically end segments implicitly and at transaction finalization we will calculate the durations. However since we're using the segment duration in the LLM events they must be the same so we end the segment right before recording events. This will be fine but a little different from what we typically do.

Related Issues

Closes #1844

Copy link

codecov bot commented Nov 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4ddfd81) 96.87% compared to head (a20183e) 96.87%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1884   +/-   ##
=======================================
  Coverage   96.87%   96.87%           
=======================================
  Files         209      209           
  Lines       39699    39759   +60     
=======================================
+ Hits        38458    38518   +60     
  Misses       1241     1241           
Flag Coverage Δ
integration-tests-16.x 78.75% <50.00%> (?)
integration-tests-18.x 79.03% <50.00%> (ø)
integration-tests-20.x 79.02% <50.00%> (-0.02%) ⬇️
unit-tests-16.x 90.99% <24.44%> (-0.10%) ⬇️
unit-tests-18.x 90.97% <24.44%> (-0.10%) ⬇️
unit-tests-20.x 90.97% <24.44%> (-0.10%) ⬇️
versioned-tests-16.x 73.71% <94.44%> (+0.02%) ⬆️
versioned-tests-18.x 73.71% <94.44%> (+0.02%) ⬆️
versioned-tests-20.x 73.72% <94.44%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bizob2828 bizob2828 force-pushed the instrument-completion-stream branch 2 times, most recently from 0b3fdb1 to 963dc88 Compare November 22, 2023 17:26
@bizob2828 bizob2828 changed the title Instrument completion stream feat: Added instrumentation for chat completion streams Nov 22, 2023
@bizob2828 bizob2828 force-pushed the instrument-completion-stream branch from 963dc88 to 4878711 Compare November 22, 2023 18:47
@bizob2828 bizob2828 marked this pull request as ready for review November 22, 2023 18:57
@bizob2828 bizob2828 force-pushed the instrument-completion-stream branch from 4878711 to 55be332 Compare November 22, 2023 19:01
@jsumners-nr
Copy link
Contributor

I'm convinced the openai library has a bug but didn't spend time completely tracking down.

I am also convinced of this. It seems to silently swallow any errors thrown by the stream. Not knowing TS, it's really difficult to figure out all of their nested wrappers around their stream handling.

jsumners-nr
jsumners-nr previously approved these changes Nov 27, 2023
Copy link
Contributor

@jsumners-nr jsumners-nr left a comment

Choose a reason for hiding this comment

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

Just a couple minor comments, but otherwise looks good to me.

lib/instrumentation/openai.js Outdated Show resolved Hide resolved
lib/instrumentation/openai.js Outdated Show resolved Hide resolved
@jsumners-nr jsumners-nr merged commit 404e317 into newrelic:main Nov 27, 2023
22 checks passed
@github-actions github-actions bot mentioned this pull request Nov 28, 2023
@bizob2828 bizob2828 deleted the instrument-completion-stream branch August 28, 2024 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Instrument chat creation - stream
2 participants