| 17 | ) |
| 18 | |
| 19 | func TestThroughputMetricHistogram(t *testing.T) { |
| 20 | tests := []struct { |
| 21 | testName string |
| 22 | sleep bool |
| 23 | header string |
| 24 | observed bool |
| 25 | }{ |
| 26 | { |
| 27 | testName: "WithSleep", |
| 28 | sleep: true, |
| 29 | header: "unit;val=0, other_unit;val=2", |
| 30 | observed: true, |
| 31 | }, |
| 32 | { |
| 33 | testName: "WithoutSleep", |
| 34 | sleep: false, |
| 35 | header: "unit;val=0, other_unit;val=2", |
| 36 | observed: false, |
| 37 | }, |
| 38 | { |
| 39 | testName: "WithSleepEmptyHeader", |
| 40 | sleep: true, |
| 41 | header: "", |
| 42 | observed: false, |
| 43 | }, |
| 44 | { |
| 45 | testName: "WithoutSleepEmptyHeader", |
| 46 | sleep: false, |
| 47 | header: "", |
| 48 | observed: false, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for _, tt := range tests { |
| 53 | t.Run(tt.testName, func(t *testing.T) { |
| 54 | |
| 55 | reg := prometheus.NewPedanticRegistry() |
| 56 | i := newInstrument(reg) |
| 57 | |
| 58 | wrap := i.Wrap(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 59 | if tt.sleep { |
| 60 | time.Sleep(i.LatencyCutoff) |
| 61 | } |
| 62 | w.Header().Set("Server-Timing", tt.header) |
| 63 | })) |
| 64 | |
| 65 | req := httptest.NewRequest("GET", "/", nil) |
| 66 | res := httptest.NewRecorder() |
| 67 | |
| 68 | wrap.ServeHTTP(res, req) |
| 69 | |
| 70 | output := `` |
| 71 | if tt.observed { |
| 72 | output = ` |
| 73 | # HELP request_throughput_unit Server throughput running requests. |
| 74 | # TYPE request_throughput_unit histogram |
| 75 | request_throughput_unit_bucket{cutoff_ms="100",method="GET",route="other",le="1"} 1 |
| 76 | request_throughput_unit_bucket{cutoff_ms="100",method="GET",route="other",le="5"} 1 |