Skip to content

Commit

Permalink
Add tests for Merge of invalid schema URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Sep 13, 2023
1 parent cf5f257 commit a0f1e4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
17 changes: 7 additions & 10 deletions sdk/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ func (r *Resource) Equal(eq *Resource) bool {
// (https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/schemas/file_format_v1.1.0.md#resources-section)
// for the translation.
//
// If one of the resources have an invalid SchemaURL (non-OpenTelemetry or an
// otherwise invalid URL), the other resource will be returned along with an
// error. If both resources have invalid schema URLs, an empty resource will be
// returned along with an error.
// If either of the resources have an invalid SchemaURL (non-OpenTelemetry or
// an otherwise invalid URL), an empty resource will be returned along with an
// error.
func Merge(a, b *Resource) (*Resource, error) {
if a == nil && b == nil {
return Empty(), nil
Expand All @@ -181,10 +180,7 @@ func Merge(a, b *Resource) (*Resource, error) {
}

// Merge the schema URL.
var (
schemaURL string
err error
)
var schemaURL string
switch true {
case a.schemaURL == "":
schemaURL = b.schemaURL
Expand All @@ -193,8 +189,9 @@ func Merge(a, b *Resource) (*Resource, error) {
case a.schemaURL == b.schemaURL:
schemaURL = a.schemaURL
default:
var err error
schemaURL, err = schema.GreatestVersion(a.schemaURL, b.SchemaURL())
if schemaURL == "" {
if err != nil {
return Empty(), err
}
if a.schemaURL != schemaURL {
Expand All @@ -218,7 +215,7 @@ func Merge(a, b *Resource) (*Resource, error) {
combine = append(combine, mi.Attribute())
}
merged := NewWithAttributes(schemaURL, combine...)
return merged, err
return merged, nil
}

var errUnknownSchema = errors.New("unknown schema")
Expand Down
12 changes: 12 additions & 0 deletions sdk/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ func TestMerge(t *testing.T) {
want: []attribute.KeyValue{kv42},
schemaURL: "https://opentelemetry.io/schemas/1.4.0",
},
{
name: "Merge with one unknown schema",
a: resource.NewWithAttributes("https://opentelemetry.io/schemas/1.4.0", kv41),
b: resource.NewWithAttributes("https://localhost/2", kv42),
isErr: true,
},
{
name: "Merge with unknown schemas",
a: resource.NewWithAttributes("https://localhost/1", kv41),
b: resource.NewWithAttributes("https://localhost/2", kv42),
isErr: true,
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("case-%s", c.name), func(t *testing.T) {
Expand Down

0 comments on commit a0f1e4a

Please sign in to comment.