2020-07-20 17:43:51 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
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"
|
2021-01-12 18:56:16 +02:00
|
|
|
ottest "go.opentelemetry.io/otel/internal/internaltest"
|
2022-07-13 15:55:43 +02:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.12.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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
semconv.ServiceNameKey.String("bar"),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
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"),
|
|
|
|
semconv.ServiceNameKey.String("bar"),
|
|
|
|
))
|
|
|
|
}
|