@@ -30,6 +30,7 @@ import (
30
30
"net/http"
31
31
"net/url"
32
32
"strings"
33
+ "time"
33
34
34
35
"github.com/dlintw/goconf"
35
36
)
42
43
ErrThrottledResponse = errors .New ("throttled OCS response" )
43
44
)
44
45
46
+ func init () {
47
+ RegisterBackendClientStats ()
48
+ }
49
+
45
50
type BackendClient struct {
46
51
hub * Hub
47
52
version string
@@ -117,9 +122,9 @@ func (b *BackendClient) PerformJSONRequest(ctx context.Context, u *url.URL, requ
117
122
return fmt .Errorf ("no url passed to perform JSON request %+v" , request )
118
123
}
119
124
120
- secret := b .backends .GetSecret (u )
121
- if secret == nil {
122
- return fmt .Errorf ("no backend secret configured for for %s" , u )
125
+ backend := b .backends .GetBackend (u )
126
+ if backend == nil {
127
+ return fmt .Errorf ("no backend configured for %s" , u )
123
128
}
124
129
125
130
var requestUrl * url.URL
@@ -160,10 +165,22 @@ func (b *BackendClient) PerformJSONRequest(ctx context.Context, u *url.URL, requ
160
165
}
161
166
162
167
// Add checksum so the backend can validate the request.
163
- AddBackendChecksum (req , data .Bytes (), secret )
168
+ AddBackendChecksum (req , data .Bytes (), backend . Secret () )
164
169
170
+ start := time .Now ()
165
171
resp , err := c .Do (req )
172
+ end := time .Now ()
173
+ duration := end .Sub (start )
174
+ statsBackendClientRequests .WithLabelValues (backend .Url ()).Inc ()
175
+ statsBackendClientDuration .WithLabelValues (backend .Url ()).Observe (duration .Seconds ())
166
176
if err != nil {
177
+ if errors .Is (err , context .DeadlineExceeded ) {
178
+ statsBackendClientError .WithLabelValues (backend .Url (), "timeout" ).Inc ()
179
+ } else if errors .Is (err , context .Canceled ) {
180
+ statsBackendClientError .WithLabelValues (backend .Url (), "canceled" ).Inc ()
181
+ } else {
182
+ statsBackendClientError .WithLabelValues (backend .Url (), "unknown" ).Inc ()
183
+ }
167
184
log .Printf ("Could not send request %s to %s: %s" , data .String (), req .URL , err )
168
185
return err
169
186
}
@@ -172,12 +189,14 @@ func (b *BackendClient) PerformJSONRequest(ctx context.Context, u *url.URL, requ
172
189
ct := resp .Header .Get ("Content-Type" )
173
190
if ! strings .HasPrefix (ct , "application/json" ) {
174
191
log .Printf ("Received unsupported content-type from %s: %s (%s)" , req .URL , ct , resp .Status )
192
+ statsBackendClientError .WithLabelValues (backend .Url (), "invalid_content_type" ).Inc ()
175
193
return ErrUnsupportedContentType
176
194
}
177
195
178
196
body , err := b .buffers .ReadAll (resp .Body )
179
197
if err != nil {
180
198
log .Printf ("Could not read response body from %s: %s" , req .URL , err )
199
+ statsBackendClientError .WithLabelValues (backend .Url (), "error_reading_body" ).Inc ()
181
200
return err
182
201
}
183
202
@@ -195,24 +214,29 @@ func (b *BackendClient) PerformJSONRequest(ctx context.Context, u *url.URL, requ
195
214
var ocs OcsResponse
196
215
if err := json .Unmarshal (body .Bytes (), & ocs ); err != nil {
197
216
log .Printf ("Could not decode OCS response %s from %s: %s" , body .String (), req .URL , err )
217
+ statsBackendClientError .WithLabelValues (backend .Url (), "error_decoding_ocs" ).Inc ()
198
218
return err
199
219
} else if ocs .Ocs == nil || len (ocs .Ocs .Data ) == 0 {
200
220
log .Printf ("Incomplete OCS response %s from %s" , body .String (), req .URL )
221
+ statsBackendClientError .WithLabelValues (backend .Url (), "error_incomplete_ocs" ).Inc ()
201
222
return ErrIncompleteResponse
202
223
}
203
224
204
225
switch ocs .Ocs .Meta .StatusCode {
205
226
case http .StatusTooManyRequests :
206
227
log .Printf ("Throttled OCS response %s from %s" , body .String (), req .URL )
228
+ statsBackendClientError .WithLabelValues (backend .Url (), "throttled" ).Inc ()
207
229
return ErrThrottledResponse
208
230
}
209
231
210
232
if err := json .Unmarshal (ocs .Ocs .Data , response ); err != nil {
211
233
log .Printf ("Could not decode OCS response body %s from %s: %s" , string (ocs .Ocs .Data ), req .URL , err )
234
+ statsBackendClientError .WithLabelValues (backend .Url (), "error_decoding_ocs_data" ).Inc ()
212
235
return err
213
236
}
214
237
} else if err := json .Unmarshal (body .Bytes (), response ); err != nil {
215
238
log .Printf ("Could not decode response body %s from %s: %s" , body .String (), req .URL , err )
239
+ statsBackendClientError .WithLabelValues (backend .Url (), "error_decoding_body" ).Inc ()
216
240
return err
217
241
}
218
242
return nil
0 commit comments