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 @@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Fix misidentification of OpenTelemetry `SpanKind` in OpenTracing bridge (`go.opentelemetry.io/otel/bridge/opentracing`). (#3096)

### Changed

- Decode urlencoded values from the `OTEL_RESOURCE_ATTRIBUTES` environment variable. (#2963)
lgfa29 marked this conversation as resolved.
Show resolved Hide resolved

## [1.9.0/0.0.3] - 2022-08-01

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

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
)
Expand Down Expand Up @@ -88,7 +90,14 @@ 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, otherwise it will be
// an empty string.
v = field[1]
otel.Handle(err)
}
attrs = append(attrs, attribute.String(k, v))
lgfa29 marked this conversation as resolved.
Show resolved Hide resolved
}
var err error
Expand Down
22 changes: 19 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 Expand Up @@ -102,6 +103,21 @@ func TestMissingKeyError(t *testing.T) {
))
}

func TestInvalidPercentDecoding(t *testing.T) {
store, err := ottest.SetEnvVariables(map[string]string{
resourceAttrKey: "key=%invalid",
})
require.NoError(t, err)
defer func() { require.NoError(t, store.Restore()) }()

detector := &fromEnv{}
res, err := detector.Detect(context.Background())
assert.NoError(t, err)
assert.Equal(t, NewSchemaless(
attribute.String("key", "%invalid"),
), res)
}

func TestDetectServiceNameFromEnv(t *testing.T) {
store, err := ottest.SetEnvVariables(map[string]string{
resourceAttrKey: "key=value,service.name=foo",
Expand Down