-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(http): use route name from 'r.Pattern' instead (#875)
* fix(http): use route name from 'r.Pattern' instead * fix(http): bump build version to 1.23 * ref(internal): move GetHTTPSpanName to internal package * chore: changelog entry --------- Co-authored-by: Michi Hoffmann <cleptric@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build !go1.23 | ||
|
||
package traceutils | ||
|
||
import "net/http" | ||
|
||
// GetHTTPSpanName grab needed fields from *http.Request to generate a span name for `http.server` span op. | ||
func GetHTTPSpanName(r *http.Request) string { | ||
return r.Method + " " + r.URL.Path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build !go1.23 | ||
|
||
package traceutils | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestGetHTTPSpanName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
got string | ||
want string | ||
}{ | ||
{ | ||
name: "Without Pattern", | ||
got: GetHTTPSpanName(&http.Request{Method: "GET", URL: &url.URL{Path: "/"}}), | ||
want: "GET /", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.got != tt.want { | ||
t.Errorf("got %q; want %q", tt.got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build go1.23 | ||
|
||
package traceutils | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// GetHTTPSpanName grab needed fields from *http.Request to generate a span name for `http.server` span op. | ||
func GetHTTPSpanName(r *http.Request) string { | ||
if r.Pattern != "" { | ||
// If value does not start with HTTP methods, add them. | ||
// The method and the path should be separated by a space. | ||
if parts := strings.SplitN(r.Pattern, " ", 2); len(parts) == 1 { | ||
return r.Method + " " + r.Pattern | ||
} | ||
|
||
return r.Pattern | ||
} | ||
|
||
return r.Method + " " + r.URL.Path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//go:build go1.23 | ||
|
||
package traceutils | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestGetHTTPSpanName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
got string | ||
want string | ||
}{ | ||
{ | ||
name: "Without Pattern", | ||
got: GetHTTPSpanName(&http.Request{Method: "GET", URL: &url.URL{Path: "/"}}), | ||
want: "GET /", | ||
}, | ||
{ | ||
name: "Pattern with method", | ||
got: GetHTTPSpanName(&http.Request{Method: "GET", URL: &url.URL{Path: "/"}, Pattern: "POST /foo/{bar}"}), | ||
want: "POST /foo/{bar}", | ||
}, | ||
{ | ||
name: "Pattern without method", | ||
got: GetHTTPSpanName(&http.Request{Method: "GET", URL: &url.URL{Path: "/"}, Pattern: "/foo/{bar}"}), | ||
want: "GET /foo/{bar}", | ||
}, | ||
{ | ||
name: "Pattern without slash", | ||
got: GetHTTPSpanName(&http.Request{Method: "GET", URL: &url.URL{Path: "/"}, Pattern: "example.com/"}), | ||
want: "GET example.com/", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.got != tt.want { | ||
t.Errorf("got %q; want %q", tt.got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters