MCPcopy
hub / github.com/caddyserver/caddy / TestMetricsInstrumentedRoute

Function TestMetricsInstrumentedRoute

modules/caddyhttp/metrics_test.go:483–524  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

481}
482
483func TestMetricsInstrumentedRoute(t *testing.T) {
484 ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})
485 m := &Metrics{
486 init: sync.Once{},
487 httpMetrics: &httpMetrics{},
488 }
489
490 handlerErr := errors.New("oh noes")
491 response := []byte("hello world!")
492 innerHandler := HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
493 if actual := testutil.ToFloat64(m.httpMetrics.requestInFlight); actual != 1.0 {
494 t.Errorf("Expected requestInFlight to be 1.0, got %v", actual)
495 }
496 if handlerErr == nil {
497 w.Write(response)
498 }
499 return handlerErr
500 })
501
502 ih := newMetricsInstrumentedRoute(ctx, "test_handler", innerHandler, m)
503
504 r := httptest.NewRequest("GET", "/", nil)
505 w := httptest.NewRecorder()
506
507 // Test with error
508 if actual := ih.ServeHTTP(w, r); actual != handlerErr {
509 t.Errorf("Expected error %v, got %v", handlerErr, actual)
510 }
511 if actual := testutil.ToFloat64(m.httpMetrics.requestInFlight); actual != 0.0 {
512 t.Errorf("Expected requestInFlight to be 0.0 after request, got %v", actual)
513 }
514 if actual := testutil.ToFloat64(m.httpMetrics.requestErrors); actual != 1.0 {
515 t.Errorf("Expected requestErrors to be 1.0, got %v", actual)
516 }
517
518 // Test without error
519 handlerErr = nil
520 w = httptest.NewRecorder()
521 if err := ih.ServeHTTP(w, r); err != nil {
522 t.Errorf("Unexpected error: %v", err)
523 }
524}
525
526func TestMetricsProvisionOTLPDisabled(t *testing.T) {
527 ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})

Callers

nothing calls this directly

Calls 4

HandlerFuncFuncType · 0.85
ServeHTTPMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected