File: builtin_test.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (65 lines) | stat: -rw-r--r-- 1,651 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package resource_test

import (
	"context"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/resource"
)

func TestBuiltinStringDetector(t *testing.T) {
	E := fmt.Errorf("no K")
	res, err := resource.StringDetector("", attribute.Key("K"), func() (string, error) {
		return "", E
	}).Detect(context.Background())
	require.ErrorIs(t, err, E)
	require.NotEqual(t, E, err)
	require.Nil(t, res)
}

func TestStringDetectorErrors(t *testing.T) {
	tests := []struct {
		desc        string
		s           resource.Detector
		errContains string
	}{
		{
			desc: "explicit error from func should be returned",
			s: resource.StringDetector("", attribute.Key("K"), func() (string, error) {
				return "", fmt.Errorf("k-is-missing")
			}),
			errContains: "k-is-missing",
		},
		{
			desc: "empty key is an invalid",
			s: resource.StringDetector("", attribute.Key(""), func() (string, error) {
				return "not-empty", nil
			}),
			errContains: "invalid attribute: \"\" -> \"not-empty\"",
		},
	}

	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)
	}
}