Skip to content

Commit 8f11018

Browse files
authored
feat: Use allow-utf8-labelnames client capability in profilecli (#4490)
1 parent 019bac2 commit 8f11018

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

cmd/profilecli/client.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67

78
"connectrpc.com/connect"
89
"github.com/prometheus/common/version"
@@ -15,10 +16,40 @@ const (
1516
protocolTypeConnect = "connect"
1617
protocolTypeGRPC = "grpc"
1718
protocolTypeGRPCWeb = "grpc-web"
19+
20+
acceptHeaderMimeType = "*/*"
1821
)
1922

23+
var acceptHeaderClientCapabilities = []string{
24+
"allow-utf8-labelnames=true",
25+
}
26+
2027
var userAgentHeader = fmt.Sprintf("pyroscope/%s", version.Version)
2128

29+
func addClientCapabilitiesHeader(r *http.Request, mime string, clientCapabilities []string) {
30+
missingClientCapabilities := make([]string, 0, len(clientCapabilities))
31+
for _, capability := range clientCapabilities {
32+
found := false
33+
// Check if any header value already contains this capability
34+
for _, value := range r.Header.Values("Accept") {
35+
if strings.Contains(value, capability) {
36+
found = true
37+
break
38+
}
39+
}
40+
41+
if !found {
42+
missingClientCapabilities = append(missingClientCapabilities, capability)
43+
}
44+
}
45+
46+
if len(missingClientCapabilities) > 0 {
47+
acceptHeader := mime
48+
acceptHeader += ";" + strings.Join(missingClientCapabilities, ";")
49+
r.Header.Add("Accept", acceptHeader)
50+
}
51+
}
52+
2253
type phlareClient struct {
2354
TenantID string
2455
URL string
@@ -49,7 +80,9 @@ func (a *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
4980
}
5081
}
5182

83+
addClientCapabilitiesHeader(req, acceptHeaderMimeType, acceptHeaderClientCapabilities)
5284
req.Header.Set("User-Agent", userAgentHeader)
85+
5386
return a.next.RoundTrip(req)
5487
}
5588

cmd/profilecli/client_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func Test_AcceptHeader(t *testing.T) {
11+
tests := []struct {
12+
Name string
13+
Header http.Header
14+
ClientCapabilities []string
15+
Want []string
16+
}{
17+
{
18+
Name: "empty header adds capability",
19+
Header: http.Header{},
20+
ClientCapabilities: []string{
21+
"allow-utf8-labelnames=true",
22+
},
23+
Want: []string{"*/*;allow-utf8-labelnames=true"},
24+
},
25+
{
26+
Name: "existing header appends capability",
27+
Header: http.Header{
28+
"Accept": []string{"application/json"},
29+
},
30+
ClientCapabilities: []string{
31+
"allow-utf8-labelnames=true",
32+
},
33+
Want: []string{"application/json", "*/*;allow-utf8-labelnames=true"},
34+
},
35+
{
36+
Name: "multiple existing values appends capability",
37+
Header: http.Header{
38+
"Accept": []string{"application/json", "text/plain"},
39+
},
40+
ClientCapabilities: []string{
41+
"allow-utf8-labelnames=true",
42+
},
43+
Want: []string{"application/json", "text/plain", "*/*;allow-utf8-labelnames=true"},
44+
},
45+
{
46+
Name: "existing capability is not duplicated",
47+
Header: http.Header{
48+
"Accept": []string{"*/*;allow-utf8-labelnames=true"},
49+
},
50+
ClientCapabilities: []string{
51+
"allow-utf8-labelnames=true",
52+
},
53+
Want: []string{"*/*;allow-utf8-labelnames=true"},
54+
},
55+
{
56+
Name: "multiple client capabilities appends capability",
57+
Header: http.Header{
58+
"Accept": []string{"*/*;allow-utf8-labelnames=true"},
59+
},
60+
ClientCapabilities: []string{
61+
"allow-utf8-labelnames=true",
62+
"capability2=false",
63+
},
64+
Want: []string{"*/*;allow-utf8-labelnames=true", "*/*;capability2=false"},
65+
},
66+
}
67+
68+
for _, tt := range tests {
69+
t.Run(tt.Name, func(t *testing.T) {
70+
t.Parallel()
71+
req, _ := http.NewRequest("GET", "example.com", nil)
72+
req.Header = tt.Header
73+
clientCapabilities := tt.ClientCapabilities
74+
75+
addClientCapabilitiesHeader(req, acceptHeaderMimeType, clientCapabilities)
76+
require.Equal(t, tt.Want, req.Header.Values("Accept"))
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)