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

Decode values from OTEL_RESOURCE_ATTRIBUTES #2963

Merged
merged 10 commits into from Oct 19, 2022
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Support for go1.16. Support is now only for go1.17 and go1.18 (#2917)

### Fixed

- Decode urlencoded values from the `OTEL_RESOURCE_ATTRIBUTES` environment variable (#2963)

## [1.7.0/0.30.0] - 2022-04-28

### Added
Expand Down
8 changes: 7 additions & 1 deletion sdk/resource/env.go
Expand Up @@ -17,6 +17,7 @@ package resource // import "go.opentelemetry.io/otel/sdk/resource"
import (
"context"
"fmt"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -88,7 +89,12 @@ func constructOTResources(s string) (*Resource, error) {
invalid = append(invalid, p)
continue
}
k, v := strings.TrimSpace(field[0]), strings.TrimSpace(field[1])
k := strings.TrimSpace(field[0])
v, err := url.QueryUnescape(strings.TrimSpace(field[1]))
if err != nil {
lgfa29 marked this conversation as resolved.
Show resolved Hide resolved
// Retain original value if decoding fails.
v = field[1]
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec actually defines that the invalid character should be replaced

When decoding the value, percent-encoded octet sequences that do not match the UTF-8 encoding scheme MUST be replaced with the replacement character (U+FFFD).

This would take quite a bit of extra work, so I thought it would be better to do it in the baggage package and then migrate the SDK to use it?

attrs = append(attrs, attribute.String(k, v))
lgfa29 marked this conversation as resolved.
Show resolved Hide resolved
}
var err error
Expand Down
7 changes: 4 additions & 3 deletions sdk/resource/env_test.go
Expand Up @@ -43,20 +43,21 @@ func TestDetectOnePair(t *testing.T) {
func TestDetectMultiPairs(t *testing.T) {
store, err := ottest.SetEnvVariables(map[string]string{
"x": "1",
resourceAttrKey: "key=value, k = v , a= x, a=z",
resourceAttrKey: "key=value, k = v , a= x, a=z, b=c%2Fd",
})
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, res, NewSchemaless(
assert.Equal(t, NewSchemaless(
attribute.String("key", "value"),
attribute.String("k", "v"),
attribute.String("a", "x"),
attribute.String("a", "z"),
))
attribute.String("b", "c/d"),
), res)
}

func TestEmpty(t *testing.T) {
Expand Down