Skip to content
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
106 changes: 106 additions & 0 deletions pkg/rules/http/client_server_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2026 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package http

import (
"net/url"
"testing"
)

func TestNetHttpClientGetServerAddress(t *testing.T) {
getter := netHttpClientAttrsGetter{}
mustURL := func(raw string) *url.URL {
u, err := url.Parse(raw)
if err != nil {
t.Fatalf("parse url: %v", err)
}
return u
}

tests := []struct {
name string
req *netHttpRequest
want string
}{
{
name: "host set",
req: &netHttpRequest{host: "example.com", url: mustURL("http://other.example/path")},
want: "example.com",
},
{
name: "host with port preferred over url",
req: &netHttpRequest{host: "example.com:8080", url: mustURL("http://other.example/path")},
want: "example.com",
},
{
name: "empty host falls back to url host",
req: &netHttpRequest{url: mustURL("http://example.com/path")},
want: "example.com",
},
{
name: "empty host falls back to url hostname without port",
req: &netHttpRequest{url: mustURL("http://example.com:8080/path")},
want: "example.com",
},
{
name: "ipv4 host with port",
req: &netHttpRequest{host: "192.168.1.10:8080", url: mustURL("http://other.example/path")},
want: "192.168.1.10",
},
{
name: "empty host falls back to ipv4 url without port",
req: &netHttpRequest{url: mustURL("http://10.0.0.1/v1")},
want: "10.0.0.1",
},
{
name: "empty host falls back to ipv4 url with port",
req: &netHttpRequest{url: mustURL("http://192.168.1.10:8080/v1")},
want: "192.168.1.10",
},
{
name: "ipv6 host with port",
req: &netHttpRequest{host: "[2001:db8::1]:8443", url: mustURL("http://other.example/path")},
want: "2001:db8::1",
},
{
name: "ipv6 host without port",
req: &netHttpRequest{host: "[::1]", url: mustURL("http://other.example/path")},
want: "::1",
},
{
name: "empty host falls back to ipv6 url with port",
req: &netHttpRequest{url: mustURL("http://[2001:db8::1]:8443/v1")},
want: "2001:db8::1",
},
{
name: "empty host falls back to ipv6 url without port",
req: &netHttpRequest{url: mustURL("http://[::1]/v1")},
want: "::1",
},
{
name: "empty host and nil url",
req: &netHttpRequest{},
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getter.GetServerAddress(tt.req); got != tt.want {
t.Fatalf("GetServerAddress() = %q, want %q", got, tt.want)
}
})
}
}
24 changes: 23 additions & 1 deletion pkg/rules/http/net_http_otel_instrumenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package http

import (
stdlibnet "net"
"os"
"strconv"

Expand Down Expand Up @@ -117,7 +118,28 @@ func (n netHttpClientAttrsGetter) GetUrlFull(request *netHttpRequest) string {
}

func (n netHttpClientAttrsGetter) GetServerAddress(request *netHttpRequest) string {
return request.host
// Client Request.Host is optional; when empty the transport uses URL.Host.
// Fall back so server.address is still populated for those requests.
// Return host/IP only (no port) to align with OTel server.address.
if request.host != "" {
return hostnameFromHostPort(request.host)
}
if request.url != nil {
return request.url.Hostname()
}
return ""
}

// hostnameFromHostPort returns the host/IP without a port.
// Request.Host may be "example.com", "192.168.1.1:8080", "[::1]:8443", or "[::1]".
func hostnameFromHostPort(host string) string {
if h, _, err := stdlibnet.SplitHostPort(host); err == nil {
return h
}
if len(host) >= 2 && host[0] == '[' && host[len(host)-1] == ']' {
return host[1 : len(host)-1]
}
return host
}

func (n netHttpClientAttrsGetter) GetServerPort(request *netHttpRequest) int {
Expand Down
2 changes: 1 addition & 1 deletion test/elasticsearch/v8.4.0/test_es_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func main() {
}
attrs := dp.Attributes.ToSlice()
if verifier.GetAttribute(attrs, "http.request.method").AsString() == "PUT" {
verifier.VerifyHttpClientMetricsAttributes(attrs, "PUT", "", "", "http", "1.1", port, 200)
verifier.VerifyHttpClientMetricsAttributes(attrs, "PUT", "127.0.0.1", "", "http", "1.1", port, 200)
found = true
break
}
Expand Down
2 changes: 1 addition & 1 deletion test/elasticsearch/v8.5.0/test_es_typedclient_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func main() {
}
attrs := dp.Attributes.ToSlice()
if verifier.GetAttribute(attrs, "http.request.method").AsString() == "PUT" {
verifier.VerifyHttpClientMetricsAttributes(attrs, "PUT", "", "", "http", "1.1", port, 200)
verifier.VerifyHttpClientMetricsAttributes(attrs, "PUT", "127.0.0.1", "", "http", "1.1", port, 200)
found = true
break
}
Expand Down
2 changes: 1 addition & 1 deletion test/nethttp/test_http_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
if point.DataPoints[0].Count <= 0 {
panic("http.client.request.duration metrics count is not positive, actually " + strconv.Itoa(int(point.DataPoints[0].Count)))
}
verifier.VerifyHttpClientMetricsAttributes(point.DataPoints[0].Attributes.ToSlice(), "GET", "127.0.0.1:"+strconv.Itoa(port), "", "http", "1.1", port, 200)
verifier.VerifyHttpClientMetricsAttributes(point.DataPoints[0].Attributes.ToSlice(), "GET", "127.0.0.1", "", "http", "1.1", port, 200)
},
})
}
Loading