2020-10-31 11:16:55 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 07:05:28 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-10-31 11:16:55 -07:00
|
|
|
|
|
|
|
package resource_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-02-18 12:59:37 -05:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-10-31 11:16:55 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuiltinStringDetector(t *testing.T) {
|
|
|
|
E := fmt.Errorf("no K")
|
2021-06-08 12:46:42 -04:00
|
|
|
res, err := resource.StringDetector("", attribute.Key("K"), func() (string, error) {
|
2020-10-31 11:16:55 -07:00
|
|
|
return "", E
|
|
|
|
}).Detect(context.Background())
|
2024-09-24 19:17:16 +02:00
|
|
|
require.ErrorIs(t, err, E)
|
2020-10-31 11:16:55 -07:00
|
|
|
require.NotEqual(t, E, err)
|
|
|
|
require.Nil(t, res)
|
|
|
|
}
|
|
|
|
|
2021-03-18 00:48:43 +00:00
|
|
|
func TestStringDetectorErrors(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
s resource.Detector
|
|
|
|
errContains string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "explicit error from func should be returned",
|
2021-06-08 12:46:42 -04:00
|
|
|
s: resource.StringDetector("", attribute.Key("K"), func() (string, error) {
|
2022-05-19 13:15:07 -07:00
|
|
|
return "", fmt.Errorf("k-is-missing")
|
2021-03-18 00:48:43 +00:00
|
|
|
}),
|
2022-05-19 13:15:07 -07:00
|
|
|
errContains: "k-is-missing",
|
2021-03-18 00:48:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "empty key is an invalid",
|
2021-06-08 12:46:42 -04:00
|
|
|
s: resource.StringDetector("", attribute.Key(""), func() (string, error) {
|
2021-03-18 00:48:43 +00:00
|
|
|
return "not-empty", nil
|
|
|
|
}),
|
|
|
|
errContains: "invalid attribute: \"\" -> \"not-empty\"",
|
|
|
|
},
|
2020-10-31 11:16:55 -07:00
|
|
|
}
|
2021-03-18 00:48:43 +00:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
res, err := resource.New(
|
|
|
|
context.Background(),
|
|
|
|
resource.WithAttributes(attribute.String("A", "B")),
|
|
|
|
resource.WithDetectors(test.s),
|
|
|
|
)
|
|
|
|
require.Error(t, err, test.desc)
|
|
|
|
require.Contains(t, err.Error(), test.errContains)
|
|
|
|
require.NotNil(t, res, "resource contains remaining valid entries")
|
|
|
|
|
|
|
|
m := map[string]string{}
|
|
|
|
for _, kv := range res.Attributes() {
|
|
|
|
m[string(kv.Key)] = kv.Value.Emit()
|
|
|
|
}
|
|
|
|
require.EqualValues(t, map[string]string{"A": "B"}, m)
|
|
|
|
}
|
2020-10-31 11:16:55 -07:00
|
|
|
}
|