Debian Codegraph @

service/internal/resource/config_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package resource
5
6 import (
7 "testing"
8
9 "github.com/google/uuid"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 sdkresource "go.opentelemetry.io/otel/sdk/resource"
13 semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
14
15 "go.opentelemetry.io/collector/component"
16 "go.opentelemetry.io/collector/pdata/pcommon"
17 )
18
19 const (
20 randomUUIDSpecialValue = "random-uuid"
21 )
22
23 var buildInfo = component.BuildInfo{
24 Command: "otelcol",
25 Version: "1.0.0",
26 }
27
28 func ptr[T any](v T) *T {
29 return &v
30 }
31
32 func TestNew(t *testing.T) {
33 tests := []struct {
34 name string
35 resourceCfg map[string]*string
36 want map[string]string
37 }{
38 {
39 name: "empty",
40 resourceCfg: map[string]*string{},
41 want: map[string]string{
42 "service.name": "otelcol",
43 "service.version": "1.0.0",
44 "service.instance.id": randomUUIDSpecialValue,
45 },
46 },
47 {
48 name: "overwrite",
49 resourceCfg: map[string]*string{
50 "service.name": ptr("my-service"),
51 "service.version": ptr("1.2.3"),
52 "service.instance.id": ptr("123"),
53 },
54 want: map[string]string{
55 "service.name": "my-service",
56 "service.version": "1.2.3",
57 "service.instance.id": "123",
58 },
59 },
60 {
61 name: "remove",
62 resourceCfg: map[string]*string{
63 "service.name": nil,
64 "service.version": nil,
65 "service.instance.id": nil,
66 },
67 want: map[string]string{},
68 },
69 {
70 name: "add",
71 resourceCfg: map[string]*string{
72 "host.name": ptr("my-host"),
73 },
74 want: map[string]string{
75 "service.name": "otelcol",
76 "service.version": "1.0.0",
77 "service.instance.id": randomUUIDSpecialValue,
78 "host.name": "my-host",
79 },
80 },
81 }
82
83 for _, tt := range tests {
84 t.Run(tt.name, func(t *testing.T) {
85 res := New(buildInfo, tt.resourceCfg)
86 got := make(map[string]string)
87 for _, attr := range res.Attributes() {
88 got[string(attr.Key)] = attr.Value.Emit()
89 }
90
91 if tt.want["service.instance.id"] == randomUUIDSpecialValue {
92 assert.Contains(t, got, "service.instance.id")
93
94 // Check that the value is a valid UUID.
95 _, err := uuid.Parse(got["service.instance.id"])
96 require.NoError(t, err)
97
98 // Remove so that we can compare the rest of the map.
99 delete(got, "service.instance.id")
100 delete(tt.want, "service.instance.id")
101 }
102
103 assert.Equal(t, tt.want, got)
104 })
105 }
106 }
107
108 func pdataFromSdk(res *sdkresource.Resource) pcommon.Resource {
109 // pcommon.NewResource is the best way to generate a new resource currently and is safe to use outside of tests.
110 // Because the resource is signal agnostic, and we need a net new resource, not an existing one, this is the only
111 // method of creating it without exposing internal packages.
112 pcommonRes := pcommon.NewResource()
113 for _, keyValue := range res.Attributes() {
114 pcommonRes.Attributes().PutStr(string(keyValue.Key), keyValue.Value.AsString())
115 }
116 return pcommonRes
117 }
118
119 func TestBuildResource(t *testing.T) {
120 buildInfo := component.NewDefaultBuildInfo()
121
122 // Check default config
123 var resMap map[string]*string
124 otelRes := New(buildInfo, resMap)
125 res := pdataFromSdk(otelRes)
126
127 assert.Equal(t, 3, res.Attributes().Len())
128 value, ok := res.Attributes().Get("service.name")
129 assert.True(t, ok)
130 assert.Equal(t, buildInfo.Command, value.AsString())
131 value, ok = res.Attributes().Get("service.version")
132 assert.True(t, ok)
133 assert.Equal(t, buildInfo.Version, value.AsString())
134
135 _, ok = res.Attributes().Get("service.instance.id")
136 assert.True(t, ok)
137
138 // Check override by nil
139 resMap = map[string]*string{
140 string(semconv.ServiceNameKey): nil,
141 string(semconv.ServiceVersionKey): nil,
142 string(semconv.ServiceInstanceIDKey): nil,
143 }
144 otelRes = New(buildInfo, resMap)
145 res = pdataFromSdk(otelRes)
146
147 // Attributes should not exist since we nil-ified all.
148 assert.Equal(t, 0, res.Attributes().Len())
149
150 // Check override values
151 strPtr := func(v string) *string { return &v }
152 resMap = map[string]*string{
153 string(semconv.ServiceNameKey): strPtr("a"),
154 string(semconv.ServiceVersionKey): strPtr("b"),
155 string(semconv.ServiceInstanceIDKey): strPtr("c"),
156 }
157 otelRes = New(buildInfo, resMap)
158 res = pdataFromSdk(otelRes)
159
160 assert.Equal(t, 3, res.Attributes().Len())
161 value, ok = res.Attributes().Get("service.name")
162 assert.True(t, ok)
163 assert.Equal(t, "a", value.AsString())
164 value, ok = res.Attributes().Get("service.version")
165 assert.True(t, ok)
166 assert.Equal(t, "b", value.AsString())
167 value, ok = res.Attributes().Get("service.instance.id")
168 assert.True(t, ok)
169 assert.Equal(t, "c", value.AsString())
170 }