|
| 1 | +package testcases |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/vllm-project/semantic-router/e2e/pkg/fixtures" |
| 11 | + pkgtestcases "github.com/vllm-project/semantic-router/e2e/pkg/testcases" |
| 12 | + "k8s.io/client-go/kubernetes" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + pkgtestcases.Register("session-pricing-chat-completions", pkgtestcases.TestCase{ |
| 17 | + Description: "After a routed chat completion, Prometheus exposes llm_session_turn_cost histogram when model pricing is configured", |
| 18 | + Tags: []string{"kubernetes", "observability", "metrics", "llm", "pricing"}, |
| 19 | + Fn: testSessionPricingChatCompletions, |
| 20 | + }) |
| 21 | + pkgtestcases.Register("session-pricing-response-api", pkgtestcases.TestCase{ |
| 22 | + Description: "After a routed Response API call, Prometheus exposes llm_session_turn_cost histogram when model pricing is configured", |
| 23 | + Tags: []string{"kubernetes", "observability", "metrics", "llm", "pricing", "response-api"}, |
| 24 | + Fn: testSessionPricingResponseAPI, |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +// testSessionPricingChatCompletions verifies that after a Chat Completions request the |
| 29 | +// llm_session_turn_cost histogram is present in /metrics (pricing must be configured |
| 30 | +// for the routed model in router-config.yaml for the observation to appear). |
| 31 | +func testSessionPricingChatCompletions( |
| 32 | + ctx context.Context, |
| 33 | + client *kubernetes.Clientset, |
| 34 | + opts pkgtestcases.TestCaseOptions, |
| 35 | +) error { |
| 36 | + traffic, err := fixtures.OpenServiceSession(ctx, client, opts) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + defer traffic.Close() |
| 41 | + |
| 42 | + metricsSession, err := fixtures.OpenSemanticRouterMetricsSession(ctx, client, opts) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + defer metricsSession.Close() |
| 47 | + |
| 48 | + chat := fixtures.NewChatCompletionsClient(traffic, 60*time.Second) |
| 49 | + |
| 50 | + headers := map[string]string{ |
| 51 | + "x-authz-user-id": "e2e-pricing-chat-user", |
| 52 | + } |
| 53 | + resp, err := chat.Create(ctx, fixtures.ChatCompletionsRequest{ |
| 54 | + Model: "MoM", |
| 55 | + Messages: []fixtures.ChatMessage{ |
| 56 | + {Role: "user", Content: "Say hello in one short sentence for pricing telemetry."}, |
| 57 | + }, |
| 58 | + User: "e2e-pricing-chat-user", |
| 59 | + }, headers) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + if resp.StatusCode != http.StatusOK { |
| 64 | + return fmt.Errorf("chat completion: expected 200, got %d: %s", resp.StatusCode, string(resp.Body)) |
| 65 | + } |
| 66 | + |
| 67 | + body, err := fetchMetrics(ctx, metricsSession) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + // Token histograms from PR 1 must still be present. |
| 73 | + if !strings.Contains(body, "llm_session_turn_prompt_tokens") { |
| 74 | + return fmt.Errorf("metrics body missing llm_session_turn_prompt_tokens") |
| 75 | + } |
| 76 | + if !strings.Contains(body, "llm_session_turn_completion_tokens") { |
| 77 | + return fmt.Errorf("metrics body missing llm_session_turn_completion_tokens") |
| 78 | + } |
| 79 | + // Cost histogram descriptor must be registered (present even when no observations). |
| 80 | + if !strings.Contains(body, "llm_session_turn_cost") { |
| 81 | + return fmt.Errorf("metrics body missing llm_session_turn_cost") |
| 82 | + } |
| 83 | + |
| 84 | + if opts.SetDetails != nil { |
| 85 | + opts.SetDetails(map[string]interface{}{ |
| 86 | + "chat_status": resp.StatusCode, |
| 87 | + }) |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// testSessionPricingResponseAPI verifies that after a Response API request the |
| 93 | +// llm_session_turn_cost histogram descriptor is exposed in /metrics. |
| 94 | +func testSessionPricingResponseAPI( |
| 95 | + ctx context.Context, |
| 96 | + client *kubernetes.Clientset, |
| 97 | + opts pkgtestcases.TestCaseOptions, |
| 98 | +) error { |
| 99 | + traffic, err := fixtures.OpenServiceSession(ctx, client, opts) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + defer traffic.Close() |
| 104 | + |
| 105 | + metricsSession, err := fixtures.OpenSemanticRouterMetricsSession(ctx, client, opts) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + defer metricsSession.Close() |
| 110 | + |
| 111 | + respAPI := fixtures.NewResponseAPIClient(traffic, 60*time.Second) |
| 112 | + |
| 113 | + _, raw, err := respAPI.Create(ctx, fixtures.ResponseAPIRequest{ |
| 114 | + Model: "MoM", |
| 115 | + Input: "Say hello in one short sentence for Response API pricing telemetry.", |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("response api create: %w", err) |
| 119 | + } |
| 120 | + if raw.StatusCode != http.StatusOK { |
| 121 | + return fmt.Errorf("response api: expected 200, got %d: %s", raw.StatusCode, string(raw.Body)) |
| 122 | + } |
| 123 | + |
| 124 | + body, err := fetchMetrics(ctx, metricsSession) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + if !strings.Contains(body, "llm_session_turn_cost") { |
| 130 | + return fmt.Errorf("metrics body missing llm_session_turn_cost after Response API request") |
| 131 | + } |
| 132 | + if !strings.Contains(body, "llm_session_turn_prompt_tokens") { |
| 133 | + return fmt.Errorf("metrics body missing llm_session_turn_prompt_tokens after Response API request") |
| 134 | + } |
| 135 | + if !strings.Contains(body, "llm_session_turn_completion_tokens") { |
| 136 | + return fmt.Errorf("metrics body missing llm_session_turn_completion_tokens after Response API request") |
| 137 | + } |
| 138 | + |
| 139 | + if opts.SetDetails != nil { |
| 140 | + opts.SetDetails(map[string]interface{}{ |
| 141 | + "response_api_status": raw.StatusCode, |
| 142 | + }) |
| 143 | + } |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +// fetchMetrics retrieves the Prometheus /metrics text from the router metrics port. |
| 148 | +func fetchMetrics(ctx context.Context, metricsSession *fixtures.ServiceSession) (string, error) { |
| 149 | + metricsHTTP := metricsSession.HTTPClient(15 * time.Second) |
| 150 | + metricsResp, err := fixtures.DoGETRequest(ctx, metricsHTTP, metricsSession.URL("/metrics")) |
| 151 | + if err != nil { |
| 152 | + return "", fmt.Errorf("fetch /metrics: %w", err) |
| 153 | + } |
| 154 | + if metricsResp.StatusCode != http.StatusOK { |
| 155 | + return "", fmt.Errorf("/metrics: expected 200, got %d", metricsResp.StatusCode) |
| 156 | + } |
| 157 | + return string(metricsResp.Body), nil |
| 158 | +} |
0 commit comments