|
4 | 4 | "io" |
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
| 7 | + "strings" |
7 | 8 | "sync" |
8 | 9 | "testing" |
9 | 10 | "time" |
@@ -67,4 +68,92 @@ func TestHTTPClient_MetricsIntegration(t *testing.T) { |
67 | 68 | defer metrics.lock.Unlock() |
68 | 69 | return len(metrics.pushToSeriesCalls) > 0 && len(metrics.incrCounterCalls) > 0 && len(metrics.incrCounterWithAttrsCalls) > 0 |
69 | 70 | }, time.Second, 100*time.Millisecond) |
| 71 | + |
| 72 | + metrics.lock.Lock() |
| 73 | + defer metrics.lock.Unlock() |
| 74 | + |
| 75 | + foundPush := false |
| 76 | + for _, key := range metrics.pushToSeriesCalls { |
| 77 | + if strings.HasSuffix(key, "response_time") { |
| 78 | + foundPush = true |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + assert.True(t, foundPush, "PushToSeries should register key with suffix response_time") |
| 83 | + |
| 84 | + foundStatus := false |
| 85 | + for _, key := range metrics.incrCounterCalls { |
| 86 | + if strings.HasSuffix(key, "status.200") { |
| 87 | + foundStatus = true |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + assert.True(t, foundStatus, "IncrCounter should register key with suffix status.200") |
| 92 | + |
| 93 | + foundAttrs := false |
| 94 | + for _, call := range metrics.incrCounterWithAttrsCalls { |
| 95 | + if strings.HasSuffix(call.key, "total") && |
| 96 | + call.attrs["status"] == "200" && |
| 97 | + call.attrs["host"] != "" { |
| 98 | + foundAttrs = true |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + assert.True(t, foundAttrs, "IncrCounterWithAttrs should register correct attributes and key with suffix total") |
| 103 | +} |
| 104 | + |
| 105 | +func TestHTTPClient_Metrics_Errors(t *testing.T) { |
| 106 | + t.Run("circuit open error", func(t *testing.T) { |
| 107 | + metrics := &mockMetrics{} |
| 108 | + client := httpclient.NewHTTPClient( |
| 109 | + &httpclient.LoggerAdapter{Writer: io.Discard}, |
| 110 | + httpclient.WithMetrics(metrics), |
| 111 | + httpclient.WithChainCallback(func(fn func() (*httpclient.Response, error)) (*httpclient.Response, error) { |
| 112 | + return nil, httpclient.ErrCircuitOpen |
| 113 | + }), |
| 114 | + ) |
| 115 | + _, _ = client.NewRequest().Get("/test-circuit-open") |
| 116 | + time.Sleep(50 * time.Millisecond) |
| 117 | + metrics.lock.Lock() |
| 118 | + defer metrics.lock.Unlock() |
| 119 | + found := false |
| 120 | + for _, call := range metrics.incrCounterCalls { |
| 121 | + if strings.HasSuffix(call, "circuit_open") { |
| 122 | + found = true |
| 123 | + break |
| 124 | + } |
| 125 | + } |
| 126 | + assert.True(t, found, "Should increment circuit_open counter on ErrCircuitOpen") |
| 127 | + }) |
| 128 | + |
| 129 | + t.Run("generic error", func(t *testing.T) { |
| 130 | + metrics := &mockMetrics{} |
| 131 | + client := httpclient.NewHTTPClient( |
| 132 | + &httpclient.LoggerAdapter{Writer: io.Discard}, |
| 133 | + httpclient.WithMetrics(metrics), |
| 134 | + httpclient.WithChainCallback(func(fn func() (*httpclient.Response, error)) (*httpclient.Response, error) { |
| 135 | + return nil, assert.AnError |
| 136 | + }), |
| 137 | + ) |
| 138 | + _, _ = client.NewRequest().Get("/test-generic-error") |
| 139 | + time.Sleep(50 * time.Millisecond) |
| 140 | + metrics.lock.Lock() |
| 141 | + defer metrics.lock.Unlock() |
| 142 | + foundError := false |
| 143 | + foundAttr := false |
| 144 | + for _, call := range metrics.incrCounterCalls { |
| 145 | + if strings.HasSuffix(call, "errors") { |
| 146 | + foundError = true |
| 147 | + break |
| 148 | + } |
| 149 | + } |
| 150 | + for _, call := range metrics.incrCounterWithAttrsCalls { |
| 151 | + if val, ok := call.attrs["error"]; ok && val == assert.AnError.Error() { |
| 152 | + foundAttr = true |
| 153 | + break |
| 154 | + } |
| 155 | + } |
| 156 | + assert.True(t, foundError, "Should increment errors counter on generic error") |
| 157 | + assert.True(t, foundAttr, "Should add error attribute on generic error") |
| 158 | + }) |
70 | 159 | } |
0 commit comments