|
9 | 9 | "sync"
|
10 | 10 | "testing"
|
11 | 11 |
|
| 12 | + "github.com/prometheus/client_golang/prometheus" |
| 13 | + |
12 | 14 | "github.com/prometheus/client_golang/prometheus/testutil"
|
13 | 15 |
|
14 | 16 | "github.com/caddyserver/caddy/v2"
|
@@ -379,6 +381,90 @@ func TestMetricsInstrumentedHandlerPerHost(t *testing.T) {
|
379 | 381 | }
|
380 | 382 | }
|
381 | 383 |
|
| 384 | +func TestMetricsInstrumentedHandlerPerProto(t *testing.T) { |
| 385 | + handler := HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { |
| 386 | + w.WriteHeader(http.StatusOK) |
| 387 | + return nil |
| 388 | + }) |
| 389 | + |
| 390 | + mh := middlewareHandlerFunc(func(w http.ResponseWriter, r *http.Request, h Handler) error { |
| 391 | + return h.ServeHTTP(w, r) |
| 392 | + }) |
| 393 | + |
| 394 | + tests := []struct { |
| 395 | + name string |
| 396 | + perProto bool |
| 397 | + proto string |
| 398 | + protoMajor int |
| 399 | + protoMinor int |
| 400 | + expectedLabelValue string |
| 401 | + }{ |
| 402 | + { |
| 403 | + name: "HTTP/1.1 with per_proto=true", |
| 404 | + perProto: true, |
| 405 | + proto: "HTTP/1.1", |
| 406 | + protoMajor: 1, |
| 407 | + protoMinor: 1, |
| 408 | + expectedLabelValue: "http/1.1", |
| 409 | + }, |
| 410 | + { |
| 411 | + name: "HTTP/2 with per_proto=true", |
| 412 | + perProto: true, |
| 413 | + proto: "HTTP/2.0", |
| 414 | + protoMajor: 2, |
| 415 | + protoMinor: 0, |
| 416 | + expectedLabelValue: "http/2", |
| 417 | + }, |
| 418 | + { |
| 419 | + name: "HTTP/3 with per_proto=true", |
| 420 | + perProto: true, |
| 421 | + proto: "HTTP/3.0", |
| 422 | + protoMajor: 3, |
| 423 | + protoMinor: 0, |
| 424 | + expectedLabelValue: "http/3", |
| 425 | + }, |
| 426 | + { |
| 427 | + name: "HTTP/1.1 with per_proto=false", |
| 428 | + perProto: false, |
| 429 | + proto: "HTTP/1.1", |
| 430 | + protoMajor: 1, |
| 431 | + protoMinor: 1, |
| 432 | + expectedLabelValue: "", |
| 433 | + }, |
| 434 | + } |
| 435 | + |
| 436 | + for _, tt := range tests { |
| 437 | + t.Run(tt.name, func(t *testing.T) { |
| 438 | + ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 439 | + metrics := &Metrics{ |
| 440 | + PerProto: tt.perProto, |
| 441 | + init: sync.Once{}, |
| 442 | + httpMetrics: &httpMetrics{}, |
| 443 | + } |
| 444 | + |
| 445 | + ih := newMetricsInstrumentedHandler(ctx, "test_handler", mh, metrics) |
| 446 | + |
| 447 | + r := httptest.NewRequest("GET", "/", nil) |
| 448 | + r.Proto = tt.proto |
| 449 | + r.ProtoMajor = tt.protoMajor |
| 450 | + r.ProtoMinor = tt.protoMinor |
| 451 | + w := httptest.NewRecorder() |
| 452 | + |
| 453 | + if err := ih.ServeHTTP(w, r, handler); err != nil { |
| 454 | + t.Errorf("Unexpected error: %v", err) |
| 455 | + } |
| 456 | + |
| 457 | + labels := prometheus.Labels{"server": "test_handler", "handler": "test_handler"} |
| 458 | + if tt.perProto { |
| 459 | + labels["proto"] = tt.expectedLabelValue |
| 460 | + } |
| 461 | + if actual := testutil.ToFloat64(metrics.httpMetrics.requestCount.With(labels)); actual == 0 { |
| 462 | + t.Logf("Request count metric recorded without proto label") |
| 463 | + } |
| 464 | + }) |
| 465 | + } |
| 466 | +} |
| 467 | + |
382 | 468 | type middlewareHandlerFunc func(http.ResponseWriter, *http.Request, Handler) error
|
383 | 469 |
|
384 | 470 | func (f middlewareHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, h Handler) error {
|
|
0 commit comments