Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ DEFAULT_BACKEND=azure
RERANK_TIMEOUT_MS=30000
RERANK_MAX_REQUEST_BYTES=10485760
RERANK_MAX_RESPONSE_BYTES=10485760
REQUEST_HEALTH_MODEL_CAPTURE_LIMIT_BYTES=3145728

DATABASE_PATH=postgresql://user:password@server:5432/db?sslmode=disable
DATABASE_PATH=postgresql://user:password@server:5432/db?sslmode=disable
8 changes: 7 additions & 1 deletion apiproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type AzureConfig struct {
RerankTimeout time.Duration
RerankMaxRequestBytes int64
RerankMaxResponseBytes int64
ModelCaptureLimitBytes int64
}

var (
Expand All @@ -49,6 +50,7 @@ func Init(mux *http.ServeMux, db *db.Database) *requestHealthRecorder {
RerankTimeout: rerankTimeoutFromEnv(),
RerankMaxRequestBytes: rerankMaxRequestBytesFromEnv(),
RerankMaxResponseBytes: rerankMaxResponseBytesFromEnv(),
ModelCaptureLimitBytes: requestHealthModelCaptureLimitFromEnv(),
}
defaultBackend = os.Getenv("DEFAULT_BACKEND")
rc := &ResponseConf{
Expand Down Expand Up @@ -152,7 +154,11 @@ func (h *baseHandle) HandleAzure(w http.ResponseWriter, r *http.Request, backend
// `/openai` base will produce `/openai/v1/responses` as desired.
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/api")
ensureStreamUsageForChatCompletions(r)
model, streaming := captureRequestHealthRequest(r)
captureLimit := h.az.ModelCaptureLimitBytes
if captureLimit <= 0 {
captureLimit = requestHealthModelCaptureLimit
}
model, streaming := captureRequestHealthRequestWithLimit(r, captureLimit)
r = withRequestHealthMetadata(r, requestHealthMetadata{
Endpoint: originalEndpoint,
Model: model,
Expand Down
24 changes: 18 additions & 6 deletions apiproxy/request_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (t *requestHealthTransport) RoundTrip(req *http.Request) (*http.Response, e
RequestID: requestID,
Endpoint: endpoint,
Upstream: req.URL.Host,
Model: metadata.Model,
Model: normalizeTelemetryModel(metadata.Model),
Method: req.Method,
StartedAt: started,
Streaming: metadata.Streaming || requestAcceptsEventStream(req),
Expand Down Expand Up @@ -303,19 +303,31 @@ func (t *requestHealthTransport) RoundTrip(req *http.Request) (*http.Response, e
return response, nil
}

const requestHealthModelCaptureLimit = 1024 * 1024
const requestHealthModelCaptureLimit = 3 * 1024 * 1024

func requestHealthModelCaptureLimitFromEnv() int64 {
return positiveInt64Env("REQUEST_HEALTH_MODEL_CAPTURE_LIMIT_BYTES", requestHealthModelCaptureLimit)
}

func normalizeTelemetryModel(model string) string {
return strings.ToLower(strings.TrimSpace(model))
}

func captureRequestHealthModel(req *http.Request) string {
model, _ := captureRequestHealthRequest(req)
return model
}

func captureRequestHealthRequest(req *http.Request) (string, bool) {
if req == nil || req.Body == nil || (req.ContentLength >= 0 && req.ContentLength > requestHealthModelCaptureLimit) {
return captureRequestHealthRequestWithLimit(req, requestHealthModelCaptureLimit)
}

func captureRequestHealthRequestWithLimit(req *http.Request, limit int64) (string, bool) {
if limit <= 0 || req == nil || req.Body == nil || (req.ContentLength >= 0 && req.ContentLength > limit) {
return "", false
}
body, err := io.ReadAll(io.LimitReader(req.Body, requestHealthModelCaptureLimit+1))
if err != nil || int64(len(body)) > requestHealthModelCaptureLimit {
body, err := io.ReadAll(io.LimitReader(req.Body, limit+1))
if err != nil || int64(len(body)) > limit {
req.Body = io.NopCloser(io.MultiReader(bytes.NewReader(body), req.Body))
return "", false
}
Expand All @@ -327,7 +339,7 @@ func captureRequestHealthRequest(req *http.Request) (string, bool) {
if err := json.Unmarshal(body, &payload); err != nil {
return "", false
}
return strings.TrimSpace(payload.Model), payload.Stream != nil && *payload.Stream
return normalizeTelemetryModel(payload.Model), payload.Stream != nil && *payload.Stream
}

type requestHealthBody struct {
Expand Down
22 changes: 22 additions & 0 deletions apiproxy/request_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -287,3 +288,24 @@ func TestCaptureRequestHealthRequestUsesStreamField(t *testing.T) {
})
}
}

func TestCaptureRequestHealthRequestNormalizesModel(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "https://example.test", strings.NewReader(`{"model":" GPT-5-Mini "}`))

model, _ := captureRequestHealthRequest(req)
if model != "gpt-5-mini" {
t.Fatalf("got model %q, want %q", model, "gpt-5-mini")
}
}

func TestRequestHealthModelCaptureLimitFromEnv(t *testing.T) {
t.Setenv("REQUEST_HEALTH_MODEL_CAPTURE_LIMIT_BYTES", "1234")
if got := requestHealthModelCaptureLimitFromEnv(); got != 1234 {
t.Fatalf("got limit %d, want 1234", got)
}

t.Setenv("REQUEST_HEALTH_MODEL_CAPTURE_LIMIT_BYTES", "0")
if got := requestHealthModelCaptureLimitFromEnv(); got != requestHealthModelCaptureLimit {
t.Fatalf("got invalid-value fallback %d, want %d", got, requestHealthModelCaptureLimit)
}
}
4 changes: 2 additions & 2 deletions apiproxy/rerank.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (h *baseHandle) HandleRerank(w http.ResponseWriter, r *http.Request) {
upstreamRequest.Header.Set("X-Request-ID", requestID)
upstreamRequest = withRequestHealthMetadata(upstreamRequest, requestHealthMetadata{
Endpoint: r.URL.Path,
Model: request.Model,
Model: normalizeTelemetryModel(request.Model),
})

client := h.rerankClient
Expand Down Expand Up @@ -287,7 +287,7 @@ func (h *baseHandle) HandleRerank(w http.ResponseWriter, r *http.Request) {
ApiKeyID: apiKeyID,
RequestType: db.RequestTypeRerank,
SearchUnits: &searchUnits,
Model: request.Model,
Model: normalizeTelemetryModel(request.Model),
IsApproximated: false,
}
if err := h.db.WriteRerankRequest(&rq); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions app/src/server/api/routers/service-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,15 @@ export const serviceHealthRouter = createTRPCRouter({
.limit(maxBreakdownRows),
ctx.db
.select({
model: sql<string>`coalesce(${requestHealthAttempts.model}, 'Unknown')`,
model: sql<string>`coalesce(nullif(lower(trim(${requestHealthAttempts.model})), ''), 'Unknown')`,
count: sql<number>`count(*)::int`,
unsuccessful: sql<number>`count(*) filter (where ${unsuccessfulCondition})::int`,
})
.from(requestHealthAttempts)
.where(filter)
.groupBy(sql`coalesce(${requestHealthAttempts.model}, 'Unknown')`)
.groupBy(
sql`coalesce(nullif(lower(trim(${requestHealthAttempts.model})), ''), 'Unknown')`,
)
.orderBy(desc(sql`count(*)`))
.limit(maxBreakdownRows),
(() => {
Expand Down
1 change: 1 addition & 0 deletions local-dev/app.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ DEFAULT_BACKEND=azure
RERANK_TIMEOUT_MS=30000
RERANK_MAX_REQUEST_BYTES=10485760
RERANK_MAX_RESPONSE_BYTES=10485760
REQUEST_HEALTH_MODEL_CAPTURE_LIMIT_BYTES=3145728

# Optional: override the generated DATABASE_PATH
# DATABASE_PATH=postgresql://openai:openai_pass@db/openai_proxy
Expand Down
Loading