@@ -259,16 +259,94 @@ func TestCurrentHttpClient_MetricAttributes(t *testing.T) {
259
259
}
260
260
261
261
func TestRequestTraceAttrs_HTTPRoute (t * testing.T ) {
262
- req := httptest .NewRequest ("GET" , "/high/cardinality/path/abc123" , nil )
263
- req .Pattern = "/high/cardinality/path/{id}"
262
+ tests := []struct {
263
+ name string
264
+ pattern string
265
+ wantRoute string
266
+ }{
267
+ {
268
+ name : "only path" ,
269
+ pattern : "/path/{id}" ,
270
+ wantRoute : "/path/{id}" ,
271
+ },
272
+ {
273
+ name : "with method" ,
274
+ pattern : "GET /path/{id}" ,
275
+ wantRoute : "/path/{id}" ,
276
+ },
277
+ {
278
+ name : "with domain" ,
279
+ pattern : "example.com/path/{id}" ,
280
+ wantRoute : "/path/{id}" ,
281
+ },
282
+ }
283
+
284
+ for _ , tt := range tests {
285
+ t .Run (tt .name , func (t * testing.T ) {
286
+ req := httptest .NewRequest (http .MethodGet , "/path/abc123" , nil )
287
+ req .Pattern = tt .pattern
288
+
289
+ attrs := (CurrentHTTPServer {}).RequestTraceAttrs ("" , req , RequestTraceAttrsOpts {})
290
+
291
+ var gotRoute string
292
+ for _ , attr := range attrs {
293
+ if attr .Key == "http.route" {
294
+ gotRoute = attr .Value .AsString ()
295
+ break
296
+ }
297
+ }
298
+ require .Equal (t , tt .wantRoute , gotRoute )
299
+ })
300
+ }
301
+ }
264
302
265
- var found bool
266
- for _ , attr := range (CurrentHTTPServer {}).RequestTraceAttrs ("" , req ) {
267
- if attr .Key != "http.route" {
268
- continue
269
- }
270
- found = true
271
- assert .Equal (t , req .Pattern , attr .Value .AsString ())
303
+ func TestRequestTraceAttrs_ClientIP (t * testing.T ) {
304
+ for _ , tt := range []struct {
305
+ name string
306
+ requestModifierFn func (r * http.Request )
307
+ requestTraceOpts RequestTraceAttrsOpts
308
+
309
+ wantClientIP string
310
+ }{
311
+ {
312
+ name : "with a client IP from the network" ,
313
+ wantClientIP : "1.2.3.4" ,
314
+ },
315
+ {
316
+ name : "with a client IP from x-forwarded-for header" ,
317
+ requestModifierFn : func (r * http.Request ) {
318
+ r .Header .Add ("X-Forwarded-For" , "5.6.7.8" )
319
+ },
320
+ wantClientIP : "5.6.7.8" ,
321
+ },
322
+ {
323
+ name : "with a client IP in options" ,
324
+ requestModifierFn : func (r * http.Request ) {
325
+ r .Header .Add ("X-Forwarded-For" , "5.6.7.8" )
326
+ },
327
+ requestTraceOpts : RequestTraceAttrsOpts {
328
+ HTTPClientIP : "9.8.7.6" ,
329
+ },
330
+ wantClientIP : "9.8.7.6" ,
331
+ },
332
+ } {
333
+ t .Run (tt .name , func (t * testing.T ) {
334
+ req := httptest .NewRequest ("GET" , "/example" , nil )
335
+ req .RemoteAddr = "1.2.3.4:5678"
336
+
337
+ if tt .requestModifierFn != nil {
338
+ tt .requestModifierFn (req )
339
+ }
340
+
341
+ var found bool
342
+ for _ , attr := range (CurrentHTTPServer {}).RequestTraceAttrs ("" , req , tt .requestTraceOpts ) {
343
+ if attr .Key != "client.address" {
344
+ continue
345
+ }
346
+ found = true
347
+ assert .Equal (t , tt .wantClientIP , attr .Value .AsString ())
348
+ }
349
+ require .True (t , found )
350
+ })
272
351
}
273
- require .True (t , found )
274
352
}
0 commit comments