File: auto_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 (93 lines) | stat: -rw-r--r-- 2,366 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package resource_test

import (
	"context"
	"errors"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"

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

type detector struct {
	SchemaURL  string
	Attributes []attribute.KeyValue
}

func newDetector(schemaURL string, attrs ...attribute.KeyValue) resource.Detector {
	return detector{schemaURL, attrs}
}

func (d detector) Detect(context.Context) (*resource.Resource, error) {
	return resource.NewWithAttributes(d.SchemaURL, d.Attributes...), nil
}

func TestDetect(t *testing.T) {
	v130 := "https://opentelemetry.io/schemas/1.3.0"
	v140 := "https://opentelemetry.io/schemas/1.4.0"
	v150 := "https://opentelemetry.io/schemas/1.5.0"

	alice := attribute.String("name", "Alice")
	bob := attribute.String("name", "Bob")
	carol := attribute.String("name", "Carol")

	admin := attribute.Bool("admin", true)
	user := attribute.Bool("admin", false)

	cases := []struct {
		name      string
		detectors []resource.Detector
		want      *resource.Resource
		wantErr   error
	}{
		{
			name: "two different schema urls",
			detectors: []resource.Detector{
				newDetector(v130, alice, admin),
				newDetector(v140, bob, user),
			},
			want:    resource.NewSchemaless(bob, user),
			wantErr: resource.ErrSchemaURLConflict,
		},
		{
			name: "three different schema urls",
			detectors: []resource.Detector{
				newDetector(v130, alice, admin),
				newDetector(v140, bob, user),
				newDetector(v150, carol),
			},
			want:    resource.NewSchemaless(carol, user),
			wantErr: resource.ErrSchemaURLConflict,
		},
		{
			name: "same schema url",
			detectors: []resource.Detector{
				newDetector(v140, alice, admin),
				newDetector(v140, bob, user),
			},
			want: resource.NewWithAttributes(v140, bob, user),
		},
	}

	for _, c := range cases {
		t.Run(fmt.Sprintf("case-%s", c.name), func(t *testing.T) {
			r, err := resource.Detect(context.Background(), c.detectors...)
			if c.wantErr != nil {
				assert.ErrorIs(t, err, c.wantErr)
				if errors.Is(c.wantErr, resource.ErrSchemaURLConflict) {
					assert.Zero(t, r.SchemaURL())
				}
			} else {
				assert.NoError(t, err)
			}
			assert.Equal(t, c.want.SchemaURL(), r.SchemaURL())
			assert.ElementsMatch(t, c.want.Attributes(), r.Attributes())
		})
	}
}