Skip to content

Commit

Permalink
fix: code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mszekiel committed Sep 14, 2023
1 parent 47c8114 commit 2f44f73
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
18 changes: 5 additions & 13 deletions httpx/client_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,10 @@ func ClientIP(r *http.Request) string {
}
}

func ClientGeoLocation(r *http.Request) GeoLocation {
var clientGeoLocation GeoLocation

if r.Header.Get("Cf-Ipcity") != "" {
clientGeoLocation.City = r.Header.Get("Cf-Ipcity")
}
if r.Header.Get("Cf-Region-Code") != "" {
clientGeoLocation.Region = r.Header.Get("Cf-Region-Code")
func ClientGeoLocation(r *http.Request) *GeoLocation {
return &GeoLocation{
City: r.Header.Get("Cf-Ipcity"),
Region: r.Header.Get("Cf-Region-Code"),
Country: r.Header.Get("Cf-Ipcountry"),
}
if r.Header.Get("Cf-Ipcountry") != "" {
clientGeoLocation.Country = r.Header.Get("Cf-Ipcountry")
}

return clientGeoLocation
}
3 changes: 1 addition & 2 deletions otelx/semconv/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ func AttributesFromContext(ctx context.Context) []attribute.KeyValue {
}

func Middleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

ctx := ContextWithAttributes(r.Context(),
append(
AttrGeoLocation(httpx.ClientGeoLocation(r)),
AttrGeoLocation(*httpx.ClientGeoLocation(r)),
AttrClientIP(httpx.ClientIP(r)),
)...,
)
Expand Down
2 changes: 1 addition & 1 deletion otelx/semconv/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func AttrClientIP(val string) otelattr.KeyValue {
}

func AttrGeoLocation(val httpx.GeoLocation) []otelattr.KeyValue {
var geoLocationAttributes []otelattr.KeyValue
geoLocationAttributes := make([]otelattr.KeyValue, 0, 3)

if val.City != "" {
geoLocationAttributes = append(geoLocationAttributes, otelattr.String(AttributeKeyGeoLocationCity.String(), val.City))
Expand Down

0 comments on commit 2f44f73

Please sign in to comment.