Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use request.URL.Host instead when request.Host is empty #213

Merged
merged 7 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Release Notes.
* Fix redis plugin cannot work in cluster mode.
* Fix cannot find file when exec build in test/plugins.
* Fix not set span error when http status code >= 400
* Fix http plugin cannot provide peer name when optional Host is empty.

#### Issues and PR
- All issues are [here](https://github.com/apache/skywalking/milestone/219?closed=1)
Expand Down
8 changes: 6 additions & 2 deletions plugins/http/client_intercepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ type ClientInterceptor struct {

func (h *ClientInterceptor) BeforeInvoke(invocation operator.Invocation) error {
request := invocation.Args()[0].(*http.Request)
s, err := tracing.CreateExitSpan(fmt.Sprintf("%s:%s", request.Method, request.URL.Path), request.Host, func(headerKey, headerValue string) error {
host := request.Host
if host == "" && request.URL != nil {
host = request.URL.Host
}
s, err := tracing.CreateExitSpan(fmt.Sprintf("%s:%s", request.Method, request.URL.Path), host, func(headerKey, headerValue string) error {
request.Header.Add(headerKey, headerValue)
return nil
}, tracing.WithLayer(tracing.SpanLayerHTTP),
tracing.WithTag(tracing.TagHTTPMethod, request.Method),
tracing.WithTag(tracing.TagURL, request.Host+request.URL.Path),
tracing.WithTag(tracing.TagURL, host+request.URL.Path),
tracing.WithComponent(5005))
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions plugins/http/client_intercepter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ func TestClientInvoke(t *testing.T) {
assert.Nil(t, err, "after invoke error should be nil")

time.Sleep(100 * time.Millisecond)

reqWithoutHost, err := http.NewRequest("GET", "/api/hello", http.NoBody) // Create request with path only, will result empty Request.Host
// Set URL after is valid behavior
reqWithoutHost.URL.Scheme = "https"
reqWithoutHost.URL.Host = "localhost"
assert.Nil(t, err, "new request with no host in url should be no error")
invocationWithoutHost := operator.NewInvocation(nil, reqWithoutHost)
err = interceptor.BeforeInvoke(invocationWithoutHost)
assert.Nil(t, err, "BeforeInvoke with URL.Host should be no error")

time.Sleep(100 * time.Millisecond)

spans := core.GetReportedSpans()
assert.NotNil(t, spans, "spans should not be nil")
assert.Equal(t, 1, len(spans), "spans length should be 1")
Expand Down
Loading