Debian Codegraph @

detectors/autodetect/autodetect_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package autodetect
5
6 import (
7 "context"
8 "errors"
9 "testing"
10
11 "go.opentelemetry.io/otel/attribute"
12 "go.opentelemetry.io/otel/sdk/resource"
13 )
14
15 type testDetector struct {
16 schemaURL string
17 attr []attribute.KeyValue
18 err error
19 }
20
21 var _ resource.Detector = (*testDetector)(nil)
22
23 func testFactory() func() resource.Detector {
24 return func() resource.Detector { return &testDetector{} }
25 }
26
27 func (d *testDetector) Detect(context.Context) (*resource.Resource, error) {
28 return resource.NewWithAttributes(d.schemaURL, d.attr...), d.err
29 }
30
31 func TestRegisterAndDetector(t *testing.T) {
32 id := ID("custom")
33 Register(id, testFactory())
34
35 detector, err := Detector(id)
36 if err != nil {
37 t.Fatalf("got error: %v, expected no error", err)
38 }
39
40 c, ok := detector.(*composite)
41 if !ok {
42 t.Errorf("got %T, expected composite detector", detector)
43 }
44
45 if len(c.detectors) != 1 {
46 t.Fatalf("got %d detectors, expected 1 detector", len(c.detectors))
47 }
48
49 if _, ok := c.detectors[0].(*testDetector); !ok {
50 t.Errorf("got %T, expected testDetector", c.detectors[0])
51 }
52 }
53
54 func TestRegisterDuplicate(t *testing.T) {
55 id := ID("duplicate")
56 Register(id, testFactory())
57
58 defer func() {
59 if r := recover(); r == nil {
60 t.Errorf("got no panic, expected panic for duplicate registration")
61 }
62 }()
63 Register(id, testFactory())
64 }
65
66 func TestParse(t *testing.T) {
67 ids := make([]ID, 0, len(registry))
68 for id := range registry {
69 ids = append(ids, id)
70 }
71
72 detector, err := Detector(ids...)
73 if err != nil {
74 t.Fatalf("got error: %v, expected no error", err)
75 }
76
77 c, ok := detector.(*composite)
78 if !ok {
79 t.Errorf("got %T, expected composite detector", detector)
80 }
81
82 if len(c.detectors) != len(registry) {
83 t.Errorf("got %d detectors, expected %d detectors", len(c.detectors), len(registry))
84 }
85 }
86
87 func TestParseUnknown(t *testing.T) {
88 _, err := Detector(ID("unknown"))
89 if !errors.Is(err, ErrUnknownDetector) {
90 t.Errorf("got %v, expected ErrUnknownDetector", err)
91 }
92 }
93
94 func TestOptDetectorDetect(t *testing.T) {
95 want := attribute.String("key", "value")
96 opt := resource.WithAttributes(want)
97 detector := optDetector{opt: opt}
98
99 res, err := detector.Detect(t.Context())
100 if err != nil {
101 t.Fatalf("got error: %v, expected no error", err)
102 }
103
104 if res == nil {
105 t.Errorf("got nil resource, expected non-nil resource")
106 }
107
108 got := res.Attributes()
109 if len(got) != 1 || got[0] != want {
110 t.Errorf("got %v, expected %v", got, []attribute.KeyValue{want})
111 }
112 }
113
114 func TestCompositeDetect(t *testing.T) {
115 a, b := attribute.Int("a", 0), attribute.Int("b", 0)
116 knownErr := errors.New("known error")
117 detectors := []resource.Detector{
118 &testDetector{attr: []attribute.KeyValue{a}},
119 &testDetector{
120 attr: []attribute.KeyValue{b},
121 err: knownErr,
122 },
123 }
124 comp := newComposite(detectors)
125
126 res, err := comp.Detect(t.Context())
127 if !errors.Is(err, knownErr) {
128 t.Errorf("got error %v, expected %v", err, knownErr)
129 }
130
131 if res == nil {
132 t.Errorf("got nil resource, expected non-nil resource")
133 }
134
135 got := res.Attributes()
136 if len(got) != 2 {
137 t.Fatalf("got %d attributes, expected 2 attributes", len(got))
138 }
139
140 if got[0].Key != a.Key && got[1].Key != a.Key {
141 t.Errorf("got %v, expected attribute %s", got, a.Key)
142 }
143 if got[0].Key != b.Key && got[1].Key != b.Key {
144 t.Errorf("got %v, expected attribute %s", got, b.Key)
145 }
146 }
147
148 func TestCompositeDetectMergeError(t *testing.T) {
149 a, b := attribute.Int("a", 0), attribute.Int("b", 0)
150 detectors := []resource.Detector{
151 &testDetector{
152 schemaURL: "a",
153 attr: []attribute.KeyValue{a},
154 },
155 &testDetector{
156 schemaURL: "b",
157 attr: []attribute.KeyValue{b},
158 },
159 }
160 comp := newComposite(detectors)
161
162 res, err := comp.Detect(t.Context())
163 if !errors.Is(err, resource.ErrSchemaURLConflict) {
164 t.Errorf("got error %v, expected %v", err, resource.ErrSchemaURLConflict)
165 }
166
167 if res != nil {
168 t.Error("got non-nil resource, expected nil resource")
169 }
170 }