TestIntegrationWithMetrics validates that Prometheus metrics are correctly incremented when requests are processed through aibridged.
(t *testing.T)
| 323 | // TestIntegrationWithMetrics validates that Prometheus metrics are correctly incremented |
| 324 | // when requests are processed through aibridged. |
| 325 | func TestIntegrationWithMetrics(t *testing.T) { |
| 326 | t.Parallel() |
| 327 | |
| 328 | ctx := testutil.Context(t, testutil.WaitLong) |
| 329 | |
| 330 | // Create prometheus registry and metrics. |
| 331 | registry := prometheus.NewRegistry() |
| 332 | metrics := aibridge.NewMetrics(registry) |
| 333 | |
| 334 | // Set up mock OpenAI server. |
| 335 | mockOpenAI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 336 | w.Header().Set("Content-Type", "application/json") |
| 337 | w.WriteHeader(http.StatusOK) |
| 338 | _, _ = w.Write([]byte(`{ |
| 339 | "id": "chatcmpl-test", |
| 340 | "object": "chat.completion", |
| 341 | "created": 1753343279, |
| 342 | "model": "gpt-4.1", |
| 343 | "choices": [ |
| 344 | { |
| 345 | "index": 0, |
| 346 | "message": { |
| 347 | "role": "assistant", |
| 348 | "content": "test response" |
| 349 | }, |
| 350 | "finish_reason": "stop" |
| 351 | } |
| 352 | ], |
| 353 | "usage": { |
| 354 | "prompt_tokens": 10, |
| 355 | "completion_tokens": 5, |
| 356 | "total_tokens": 15 |
| 357 | } |
| 358 | }`)) |
| 359 | })) |
| 360 | t.Cleanup(mockOpenAI.Close) |
| 361 | |
| 362 | // Database and coderd setup. |
| 363 | db, ps := dbtestutil.NewDB(t) |
| 364 | client, _, api, firstUser := coderdenttest.NewWithAPI(t, &coderdenttest.Options{ |
| 365 | Options: &coderdtest.Options{ |
| 366 | Database: db, |
| 367 | Pubsub: ps, |
| 368 | }, |
| 369 | }) |
| 370 | |
| 371 | userClient, _ := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID) |
| 372 | |
| 373 | // Create an API token for the user. |
| 374 | apiKey, err := userClient.CreateToken(ctx, "me", codersdk.CreateTokenRequest{ |
| 375 | TokenName: fmt.Sprintf("test-key-%d", time.Now().UnixNano()), |
| 376 | Lifetime: time.Hour, |
| 377 | Scope: codersdk.APIKeyScopeCoderAll, |
| 378 | }) |
| 379 | require.NoError(t, err) |
| 380 | |
| 381 | // Create aibridge client. |
| 382 | aiBridgeClient, err := api.AGPL.CreateInMemoryAIBridgeServer(ctx) |
nothing calls this directly
no test coverage detected