1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-10-31 00:07:40 +02:00

Use url.PathUnescape rather than url.QueryUnescape when parsing OTLP headers and resource attributes env vars (#4698) (#4699)

This commit is contained in:
Michael Blum
2023-11-09 12:51:59 -06:00
committed by GitHub
parent 0c5ebd5856
commit be5064a387
13 changed files with 127 additions and 17 deletions

View File

@@ -89,7 +89,7 @@ func constructOTResources(s string) (*Resource, error) {
continue
}
key := strings.TrimSpace(k)
val, err := url.QueryUnescape(strings.TrimSpace(v))
val, err := url.PathUnescape(strings.TrimSpace(v))
if err != nil {
// Retain original value if decoding fails, otherwise it will be
// an empty string.

View File

@@ -40,6 +40,19 @@ func TestDetectOnePair(t *testing.T) {
assert.Equal(t, NewSchemaless(attribute.String("key", "value")), res)
}
func TestDetectURIEncodingOnePair(t *testing.T) {
store, err := ottest.SetEnvVariables(map[string]string{
resourceAttrKey: "key=x+y+z?q=123",
})
require.NoError(t, err)
defer func() { require.NoError(t, store.Restore()) }()
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
require.NoError(t, err)
assert.Equal(t, NewSchemaless(attribute.String("key", "x+y+z?q=123")), res)
}
func TestDetectMultiPairs(t *testing.T) {
store, err := ottest.SetEnvVariables(map[string]string{
"x": "1",
@@ -60,6 +73,23 @@ func TestDetectMultiPairs(t *testing.T) {
), res)
}
func TestDetectURIEncodingMultiPairs(t *testing.T) {
store, err := ottest.SetEnvVariables(map[string]string{
"x": "1",
resourceAttrKey: "key=x+y+z,namespace=localhost/test&verify",
})
require.NoError(t, err)
defer func() { require.NoError(t, store.Restore()) }()
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
require.NoError(t, err)
assert.Equal(t, NewSchemaless(
attribute.String("key", "x+y+z"),
attribute.String("namespace", "localhost/test&verify"),
), res)
}
func TestEmpty(t *testing.T) {
store, err := ottest.SetEnvVariables(map[string]string{
resourceAttrKey: " ",