@@ -8,8 +8,11 @@ import (
8
8
9
9
"github.com/google/uuid"
10
10
"github.com/stretchr/testify/assert"
11
+ sdkresource "go.opentelemetry.io/otel/sdk/resource"
11
12
12
13
"go.opentelemetry.io/collector/component"
14
+ "go.opentelemetry.io/collector/pdata/pcommon"
15
+ semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
13
16
)
14
17
15
18
const (
@@ -101,3 +104,67 @@ func TestNew(t *testing.T) {
101
104
}
102
105
103
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 , res .Attributes ().Len (), 3 )
128
+ value , ok := res .Attributes ().Get (semconv .AttributeServiceName )
129
+ assert .True (t , ok )
130
+ assert .Equal (t , buildInfo .Command , value .AsString ())
131
+ value , ok = res .Attributes ().Get (semconv .AttributeServiceVersion )
132
+ assert .True (t , ok )
133
+ assert .Equal (t , buildInfo .Version , value .AsString ())
134
+
135
+ _ , ok = res .Attributes ().Get (semconv .AttributeServiceInstanceID )
136
+ assert .True (t , ok )
137
+
138
+ // Check override by nil
139
+ resMap = map [string ]* string {
140
+ semconv .AttributeServiceName : nil ,
141
+ semconv .AttributeServiceVersion : nil ,
142
+ semconv .AttributeServiceInstanceID : 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 , res .Attributes ().Len (), 0 )
149
+
150
+ // Check override values
151
+ strPtr := func (v string ) * string { return & v }
152
+ resMap = map [string ]* string {
153
+ semconv .AttributeServiceName : strPtr ("a" ),
154
+ semconv .AttributeServiceVersion : strPtr ("b" ),
155
+ semconv .AttributeServiceInstanceID : strPtr ("c" ),
156
+ }
157
+ otelRes = New (buildInfo , resMap )
158
+ res = pdataFromSdk (otelRes )
159
+
160
+ assert .Equal (t , res .Attributes ().Len (), 3 )
161
+ value , ok = res .Attributes ().Get (semconv .AttributeServiceName )
162
+ assert .True (t , ok )
163
+ assert .Equal (t , "a" , value .AsString ())
164
+ value , ok = res .Attributes ().Get (semconv .AttributeServiceVersion )
165
+ assert .True (t , ok )
166
+ assert .Equal (t , "b" , value .AsString ())
167
+ value , ok = res .Attributes ().Get (semconv .AttributeServiceInstanceID )
168
+ assert .True (t , ok )
169
+ assert .Equal (t , "c" , value .AsString ())
170
+ }
0 commit comments