2020-07-20 17:43:51 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 08:05:28 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-20 17:43:51 +02:00
|
|
|
|
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2023-08-09 09:15:47 +02:00
|
|
|
ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
|
2024-06-06 18:36:59 +02:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
|
2020-07-20 17:43:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDetectOnePair(t *testing.T) {
|
2020-10-31 20:16:55 +02:00
|
|
|
store, err := ottest.SetEnvVariables(map[string]string{
|
2021-06-04 20:19:15 +02:00
|
|
|
resourceAttrKey: "key=value",
|
2020-10-31 20:16:55 +02:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, store.Restore()) }()
|
2020-07-20 17:43:51 +02:00
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
detector := &fromEnv{}
|
2020-07-20 17:43:51 +02:00
|
|
|
res, err := detector.Detect(context.Background())
|
|
|
|
require.NoError(t, err)
|
2021-06-08 18:46:42 +02:00
|
|
|
assert.Equal(t, NewSchemaless(attribute.String("key", "value")), res)
|
2020-07-20 17:43:51 +02:00
|
|
|
}
|
|
|
|
|
2023-11-09 20:51:59 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:43:51 +02:00
|
|
|
func TestDetectMultiPairs(t *testing.T) {
|
2020-10-31 20:16:55 +02:00
|
|
|
store, err := ottest.SetEnvVariables(map[string]string{
|
2021-06-04 20:19:15 +02:00
|
|
|
"x": "1",
|
2022-10-19 18:13:20 +02:00
|
|
|
resourceAttrKey: "key=value, k = v , a= x, a=z, b=c%2Fd",
|
2020-10-31 20:16:55 +02:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, store.Restore()) }()
|
2020-07-20 17:43:51 +02:00
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
detector := &fromEnv{}
|
2020-07-20 17:43:51 +02:00
|
|
|
res, err := detector.Detect(context.Background())
|
|
|
|
require.NoError(t, err)
|
2022-10-19 18:13:20 +02:00
|
|
|
assert.Equal(t, NewSchemaless(
|
2021-02-18 19:59:37 +02:00
|
|
|
attribute.String("key", "value"),
|
|
|
|
attribute.String("k", "v"),
|
|
|
|
attribute.String("a", "x"),
|
|
|
|
attribute.String("a", "z"),
|
2022-10-19 18:13:20 +02:00
|
|
|
attribute.String("b", "c/d"),
|
|
|
|
), res)
|
2020-07-20 17:43:51 +02:00
|
|
|
}
|
|
|
|
|
2023-11-09 20:51:59 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:43:51 +02:00
|
|
|
func TestEmpty(t *testing.T) {
|
2020-10-31 20:16:55 +02:00
|
|
|
store, err := ottest.SetEnvVariables(map[string]string{
|
2021-06-04 20:19:15 +02:00
|
|
|
resourceAttrKey: " ",
|
2020-10-31 20:16:55 +02:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, store.Restore()) }()
|
2020-07-20 17:43:51 +02:00
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
detector := &fromEnv{}
|
2020-07-20 17:43:51 +02:00
|
|
|
res, err := detector.Detect(context.Background())
|
2020-09-14 18:08:05 +02:00
|
|
|
require.NoError(t, err)
|
2020-07-20 17:43:51 +02:00
|
|
|
assert.Equal(t, Empty(), res)
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:09:07 +02:00
|
|
|
func TestNoResourceAttributesSet(t *testing.T) {
|
|
|
|
store, err := ottest.SetEnvVariables(map[string]string{
|
|
|
|
svcNameKey: "bar",
|
|
|
|
})
|
|
|
|
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(
|
2023-02-07 23:42:47 +02:00
|
|
|
semconv.ServiceName("bar"),
|
2021-07-29 19:09:07 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:43:51 +02:00
|
|
|
func TestMissingKeyError(t *testing.T) {
|
2020-10-31 20:16:55 +02:00
|
|
|
store, err := ottest.SetEnvVariables(map[string]string{
|
2021-06-04 20:19:15 +02:00
|
|
|
resourceAttrKey: "key=value,key",
|
2020-10-31 20:16:55 +02:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, store.Restore()) }()
|
2020-07-20 17:43:51 +02:00
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
detector := &fromEnv{}
|
2020-07-20 17:43:51 +02:00
|
|
|
res, err := detector.Detect(context.Background())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, fmt.Errorf("%w: %v", errMissingValue, "[key]"))
|
2021-06-08 18:46:42 +02:00
|
|
|
assert.Equal(t, res, NewSchemaless(
|
2021-02-18 19:59:37 +02:00
|
|
|
attribute.String("key", "value"),
|
2020-07-20 17:43:51 +02:00
|
|
|
))
|
|
|
|
}
|
2021-06-04 20:19:15 +02:00
|
|
|
|
2022-10-19 18:13:20 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-06-04 20:19:15 +02:00
|
|
|
func TestDetectServiceNameFromEnv(t *testing.T) {
|
|
|
|
store, err := ottest.SetEnvVariables(map[string]string{
|
|
|
|
resourceAttrKey: "key=value,service.name=foo",
|
|
|
|
svcNameKey: "bar",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, store.Restore()) }()
|
|
|
|
|
|
|
|
detector := &fromEnv{}
|
|
|
|
res, err := detector.Detect(context.Background())
|
|
|
|
require.NoError(t, err)
|
2021-06-08 18:46:42 +02:00
|
|
|
assert.Equal(t, res, NewSchemaless(
|
2021-06-04 20:19:15 +02:00
|
|
|
attribute.String("key", "value"),
|
2023-02-07 23:42:47 +02:00
|
|
|
semconv.ServiceName("bar"),
|
2021-06-04 20:19:15 +02:00
|
|
|
))
|
|
|
|
}
|