Skip to content

Commit 6f70130

Browse files
committed
config: Fix handling of SchemaUrl
1 parent 9640704 commit 6f70130

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

config/v0.3.0/resource.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ func keyVal(k string, v any) attribute.KeyValue {
4747
}
4848

4949
func newResource(res *Resource) *resource.Resource {
50-
if res == nil || res.Attributes == nil {
50+
if res == nil || res.SchemaUrl == nil && res.Attributes == nil {
5151
return resource.Default()
5252
}
53-
var attrs []attribute.KeyValue
5453

54+
schemaURL := ""
55+
if res.SchemaUrl != nil {
56+
schemaURL = *res.SchemaUrl
57+
}
58+
59+
var attrs []attribute.KeyValue
5560
for _, v := range res.Attributes {
5661
attrs = append(attrs, keyVal(v.Name, v.Value))
5762
}
5863

59-
return resource.NewWithAttributes(*res.SchemaUrl,
60-
attrs...,
61-
)
64+
return resource.NewWithAttributes(schemaURL, attrs...)
6265
}

config/v0.3.0/resource_test.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,7 @@ import (
1717
type mockType struct{}
1818

1919
func TestNewResource(t *testing.T) {
20-
res := resource.NewWithAttributes(semconv.SchemaURL,
21-
semconv.ServiceName("service-a"),
22-
)
2320
other := mockType{}
24-
resWithAttrs := resource.NewWithAttributes(semconv.SchemaURL,
25-
semconv.ServiceName("service-a"),
26-
attribute.Bool("attr-bool", true),
27-
attribute.String("attr-uint64", fmt.Sprintf("%d", 164)),
28-
attribute.Int64("attr-int64", int64(-164)),
29-
attribute.Float64("attr-float64", float64(64.0)),
30-
attribute.Int64("attr-int8", int64(-18)),
31-
attribute.Int64("attr-uint8", int64(18)),
32-
attribute.Int64("attr-int16", int64(-116)),
33-
attribute.Int64("attr-uint16", int64(116)),
34-
attribute.Int64("attr-int32", int64(-132)),
35-
attribute.Int64("attr-uint32", int64(132)),
36-
attribute.Float64("attr-float32", float64(32.0)),
37-
attribute.Int64("attr-int", int64(-1)),
38-
attribute.String("attr-uint", fmt.Sprintf("%d", 1)),
39-
attribute.String("attr-string", "string-val"),
40-
attribute.String("attr-default", fmt.Sprintf("%v", other)),
41-
)
4221
tests := []struct {
4322
name string
4423
config *Resource
@@ -53,6 +32,24 @@ func TestNewResource(t *testing.T) {
5332
config: &Resource{},
5433
wantResource: resource.Default(),
5534
},
35+
{
36+
name: "resource-with-schema",
37+
config: &Resource{
38+
SchemaUrl: ptr(semconv.SchemaURL),
39+
},
40+
wantResource: resource.NewWithAttributes(semconv.SchemaURL),
41+
},
42+
{
43+
name: "resource-with-attributes",
44+
config: &Resource{
45+
Attributes: []AttributeNameValue{
46+
{Name: "service.name", Value: "service-a"},
47+
},
48+
},
49+
wantResource: resource.NewWithAttributes("",
50+
semconv.ServiceName("service-a"),
51+
),
52+
},
5653
{
5754
name: "resource-with-attributes-and-schema",
5855
config: &Resource{
@@ -61,7 +58,9 @@ func TestNewResource(t *testing.T) {
6158
},
6259
SchemaUrl: ptr(semconv.SchemaURL),
6360
},
64-
wantResource: res,
61+
wantResource: resource.NewWithAttributes(semconv.SchemaURL,
62+
semconv.ServiceName("service-a"),
63+
),
6564
},
6665
{
6766
name: "resource-with-additional-attributes-and-schema",
@@ -86,7 +85,23 @@ func TestNewResource(t *testing.T) {
8685
},
8786
SchemaUrl: ptr(semconv.SchemaURL),
8887
},
89-
wantResource: resWithAttrs,
88+
wantResource: resource.NewWithAttributes(semconv.SchemaURL,
89+
semconv.ServiceName("service-a"),
90+
attribute.Bool("attr-bool", true),
91+
attribute.String("attr-uint64", fmt.Sprintf("%d", 164)),
92+
attribute.Int64("attr-int64", int64(-164)),
93+
attribute.Float64("attr-float64", float64(64.0)),
94+
attribute.Int64("attr-int8", int64(-18)),
95+
attribute.Int64("attr-uint8", int64(18)),
96+
attribute.Int64("attr-int16", int64(-116)),
97+
attribute.Int64("attr-uint16", int64(116)),
98+
attribute.Int64("attr-int32", int64(-132)),
99+
attribute.Int64("attr-uint32", int64(132)),
100+
attribute.Float64("attr-float32", float64(32.0)),
101+
attribute.Int64("attr-int", int64(-1)),
102+
attribute.String("attr-uint", fmt.Sprintf("%d", 1)),
103+
attribute.String("attr-string", "string-val"),
104+
attribute.String("attr-default", fmt.Sprintf("%v", other))),
90105
},
91106
}
92107
for _, tt := range tests {

0 commit comments

Comments
 (0)