Skip to content

Commit

Permalink
Fix: http.client_ip vs multiple addresses #2282 (#2284)
Browse files Browse the repository at this point in the history
* Fix: http.client_ip vs multiple addresses #2282

* Split only the necessary number of values.

As suggested by @pellared. Good suggestion, that.

Co-authored-by: Robert Pająk <pellared@hotmail.com>

Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 18, 2021
1 parent e9db047 commit 2e6211e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

- json stdout exporter no longer crashes due to concurrency bug. (#2265)
- `http.client_ip` no longer inhales multiple addresses from the `X-Forwarded-For` header. (#2282)

## [Metrics 0.24.0] - 2021-10-01

Expand Down
4 changes: 3 additions & 1 deletion semconv/v1.4.0/http.go
Expand Up @@ -225,7 +225,9 @@ func HTTPServerAttributesFromHTTPRequest(serverName, route string, request *http
attrs = append(attrs, HTTPRouteKey.String(route))
}
if values, ok := request.Header["X-Forwarded-For"]; ok && len(values) > 0 {
attrs = append(attrs, HTTPClientIPKey.String(values[0]))
if addresses := strings.SplitN(values[0], ",", 2); len(addresses) > 0 {
attrs = append(attrs, HTTPClientIPKey.String(addresses[0]))
}
}

return append(attrs, httpCommonAttributesFromHTTPRequest(request)...)
Expand Down
4 changes: 2 additions & 2 deletions semconv/v1.4.0/http_test.go
Expand Up @@ -585,7 +585,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) {
},
header: http.Header{
"User-Agent": []string{"foodownloader"},
"X-Forwarded-For": []string{"1.2.3.4"},
"X-Forwarded-For": []string{"203.0.113.195, 70.41.3.18, 150.172.238.178"},
},
tls: withTLS,
expected: []attribute.KeyValue{
Expand All @@ -597,7 +597,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) {
attribute.String("http.route", "/user/:id"),
attribute.String("http.host", "example.com"),
attribute.String("http.user_agent", "foodownloader"),
attribute.String("http.client_ip", "1.2.3.4"),
attribute.String("http.client_ip", "203.0.113.195"),
},
},
{
Expand Down

0 comments on commit 2e6211e

Please sign in to comment.