File: resource_test.go

package info (click to toggle)
golang-opentelemetry-contrib 0.56.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,884 kB
  • sloc: makefile: 278; sh: 211; sed: 1
file content (116 lines) | stat: -rw-r--r-- 3,620 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package config // import "go.opentelemetry.io/contrib/config"

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)

type mockType struct{}

func TestNewResource(t *testing.T) {
	res, err := resource.Merge(resource.Default(),
		resource.NewWithAttributes(semconv.SchemaURL,
			semconv.ServiceName("service-a"),
		))
	other := mockType{}
	require.NoError(t, err)
	resWithAttrs, err := resource.Merge(resource.Default(),
		resource.NewWithAttributes(semconv.SchemaURL,
			semconv.ServiceName("service-a"),
			attribute.Bool("attr-bool", true),
			attribute.String("attr-uint64", fmt.Sprintf("%d", 164)),
			attribute.Int64("attr-int64", int64(-164)),
			attribute.Float64("attr-float64", float64(64.0)),
			attribute.Int64("attr-int8", int64(-18)),
			attribute.Int64("attr-uint8", int64(18)),
			attribute.Int64("attr-int16", int64(-116)),
			attribute.Int64("attr-uint16", int64(116)),
			attribute.Int64("attr-int32", int64(-132)),
			attribute.Int64("attr-uint32", int64(132)),
			attribute.Float64("attr-float32", float64(32.0)),
			attribute.Int64("attr-int", int64(-1)),
			attribute.String("attr-uint", fmt.Sprintf("%d", 1)),
			attribute.String("attr-string", "string-val"),
			attribute.String("attr-default", fmt.Sprintf("%v", other)),
		))
	require.NoError(t, err)
	tests := []struct {
		name         string
		config       *Resource
		wantResource *resource.Resource
		wantErr      error
	}{
		{
			name:         "no-resource-configuration",
			wantResource: resource.Default(),
		},
		{
			name:         "resource-no-attributes",
			config:       &Resource{},
			wantResource: resource.Default(),
		},
		{
			name: "resource-with-attributes-invalid-schema",
			config: &Resource{
				SchemaUrl: ptr("https://opentelemetry.io/invalid-schema"),
				Attributes: []AttributeNameValue{
					{Name: "service.name", Value: "service-a"},
				},
			},
			wantResource: resource.NewSchemaless(res.Attributes()...),
			wantErr:      resource.ErrSchemaURLConflict,
		},
		{
			name: "resource-with-attributes-and-schema",
			config: &Resource{
				Attributes: []AttributeNameValue{
					{Name: "service.name", Value: "service-a"},
				},
				SchemaUrl: ptr(semconv.SchemaURL),
			},
			wantResource: res,
		},
		{
			name: "resource-with-additional-attributes-and-schema",
			config: &Resource{
				Attributes: []AttributeNameValue{
					{Name: "service.name", Value: "service-a"},
					{Name: "attr-bool", Value: true},
					{Name: "attr-int64", Value: int64(-164)},
					{Name: "attr-uint64", Value: uint64(164)},
					{Name: "attr-float64", Value: float64(64.0)},
					{Name: "attr-int8", Value: int8(-18)},
					{Name: "attr-uint8", Value: uint8(18)},
					{Name: "attr-int16", Value: int16(-116)},
					{Name: "attr-uint16", Value: uint16(116)},
					{Name: "attr-int32", Value: int32(-132)},
					{Name: "attr-uint32", Value: uint32(132)},
					{Name: "attr-float32", Value: float32(32.0)},
					{Name: "attr-int", Value: int(-1)},
					{Name: "attr-uint", Value: uint(1)},
					{Name: "attr-string", Value: "string-val"},
					{Name: "attr-default", Value: other},
				},
				SchemaUrl: ptr(semconv.SchemaURL),
			},
			wantResource: resWithAttrs,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := newResource(tt.config)
			assert.ErrorIs(t, err, tt.wantErr)
			assert.Equal(t, tt.wantResource, got)
		})
	}
}