-
Notifications
You must be signed in to change notification settings - Fork 179
o11y: Add TTFT and TPOT histograms for SLOs #126
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
116 changes: 116 additions & 0 deletions
116
src/semantic-router/pkg/extproc/metrics_integration_test.go
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,116 @@ | ||
package extproc | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" | ||
ext_proc "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" | ||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func getHistogramSampleCount(metricName, model string) uint64 { | ||
mf, _ := prometheus.DefaultGatherer.Gather() | ||
for _, fam := range mf { | ||
if fam.GetName() != metricName || fam.GetType() != dto.MetricType_HISTOGRAM { | ||
continue | ||
} | ||
for _, m := range fam.GetMetric() { | ||
labels := m.GetLabel() | ||
match := false | ||
for _, l := range labels { | ||
if l.GetName() == "model" && l.GetValue() == model { | ||
match = true | ||
break | ||
} | ||
} | ||
if match { | ||
h := m.GetHistogram() | ||
if h != nil && h.SampleCount != nil { | ||
return h.GetSampleCount() | ||
} | ||
} | ||
} | ||
} | ||
return 0 | ||
} | ||
|
||
var _ = Describe("Metrics recording", func() { | ||
var router *OpenAIRouter | ||
|
||
BeforeEach(func() { | ||
// Use a minimal router that doesn't require external models | ||
router = &OpenAIRouter{} | ||
// Initialize internal maps used by handlers | ||
router.InitializeForTesting() | ||
}) | ||
|
||
It("records TTFT on response headers", func() { | ||
ctx := &RequestContext{ | ||
RequestModel: "model-a", | ||
ProcessingStartTime: time.Now().Add(-75 * time.Millisecond), | ||
} | ||
|
||
before := getHistogramSampleCount("llm_model_ttft_seconds", ctx.RequestModel) | ||
|
||
respHeaders := &ext_proc.ProcessingRequest_ResponseHeaders{ | ||
ResponseHeaders: &ext_proc.HttpHeaders{ | ||
Headers: &core.HeaderMap{Headers: []*core.HeaderValue{{Key: "content-type", Value: "application/json"}}}, | ||
}, | ||
} | ||
|
||
response, err := router.handleResponseHeaders(respHeaders, ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(response.GetResponseHeaders()).NotTo(BeNil()) | ||
|
||
after := getHistogramSampleCount("llm_model_ttft_seconds", ctx.RequestModel) | ||
Expect(after).To(BeNumerically(">", before)) | ||
Expect(ctx.TTFTRecorded).To(BeTrue()) | ||
Expect(ctx.TTFTSeconds).To(BeNumerically(">", 0)) | ||
}) | ||
|
||
It("records TPOT on response body", func() { | ||
ctx := &RequestContext{ | ||
RequestID: "tpot-test-1", | ||
RequestModel: "model-a", | ||
StartTime: time.Now().Add(-1 * time.Second), | ||
} | ||
|
||
before := getHistogramSampleCount("llm_model_tpot_seconds", ctx.RequestModel) | ||
|
||
openAIResponse := map[string]interface{}{ | ||
"id": "chatcmpl-xyz", | ||
"object": "chat.completion", | ||
"created": time.Now().Unix(), | ||
"model": ctx.RequestModel, | ||
"usage": map[string]interface{}{ | ||
"prompt_tokens": 10, | ||
"completion_tokens": 5, | ||
"total_tokens": 15, | ||
}, | ||
"choices": []map[string]interface{}{ | ||
{ | ||
"message": map[string]interface{}{"role": "assistant", "content": "Hello"}, | ||
"finish_reason": "stop", | ||
}, | ||
}, | ||
} | ||
respBodyJSON, err := json.Marshal(openAIResponse) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
respBody := &ext_proc.ProcessingRequest_ResponseBody{ | ||
ResponseBody: &ext_proc.HttpBody{Body: respBodyJSON}, | ||
} | ||
|
||
response, err := router.handleResponseBody(respBody, ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(response.GetResponseBody()).NotTo(BeNil()) | ||
|
||
after := getHistogramSampleCount("llm_model_tpot_seconds", ctx.RequestModel) | ||
Expect(after).To(BeNumerically(">", before)) | ||
}) | ||
}) |
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
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.
for now we haven't tried streaming mode. In the buffered mode, the response from LLM has to be fully received before the response is received. If you can add an issue to track TTFT in streaming mode, that'll be great.
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.
Sure
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.
#128 for tracking