MCPcopy Index your code
hub / github.com/coder/coder / TestIntegrationWithMetrics

Function TestIntegrationWithMetrics

enterprise/aibridged_integration_test.go:325–427  ·  view source on GitHub ↗

TestIntegrationWithMetrics validates that Prometheus metrics are correctly incremented when requests are processed through aibridged.

(t *testing.T)

Source from the content-addressed store, hash-verified

323// TestIntegrationWithMetrics validates that Prometheus metrics are correctly incremented
324// when requests are processed through aibridged.
325func 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)

Callers

nothing calls this directly

Calls 15

ContextFunction · 0.92
NewMetricsFunction · 0.92
NewDBFunction · 0.92
NewWithAPIFunction · 0.92
CreateAnotherUserFunction · 0.92
LoggerFunction · 0.92
NewOpenAIProviderFunction · 0.92
NewCachedBridgePoolFunction · 0.92
NewFunction · 0.92
CreateTokenMethod · 0.80
SetMethod · 0.65

Tested by

no test coverage detected