Skip to content

Commit

Permalink
Ensure no schema URL when merge conflict on detect
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Feb 1, 2024
1 parent 9f55256 commit 4f1da09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
5 changes: 5 additions & 0 deletions sdk/resource/auto.go
Expand Up @@ -94,6 +94,11 @@ func detect(ctx context.Context, res *Resource, detectors []Detector) error {
if len(errs) == 0 {
return nil
}
if errors.Is(errs, ErrSchemaURLConflict) {
// If there has been a merge conflict, ensure the resource has not
// schema URL.
res.schemaURL = ""
}
return errs
}

Expand Down
50 changes: 34 additions & 16 deletions sdk/resource/auto_test.go
Expand Up @@ -16,6 +16,7 @@ package resource_test

import (
"context"
"errors"
"fmt"
"os"
"testing"
Expand All @@ -28,32 +29,49 @@ import (

func TestDetect(t *testing.T) {
cases := []struct {
name string
schema1, schema2 string
isErr bool
name string
schema []string
wantErr error
}{
{
name: "different schema urls",
schema1: "https://opentelemetry.io/schemas/1.3.0",
schema2: "https://opentelemetry.io/schemas/1.4.0",
isErr: true,
name: "two different schema urls",
schema: []string{
"https://opentelemetry.io/schemas/1.3.0",
"https://opentelemetry.io/schemas/1.4.0",
},
wantErr: resource.ErrSchemaURLConflict,
},
{
name: "same schema url",
schema1: "https://opentelemetry.io/schemas/1.4.0",
schema2: "https://opentelemetry.io/schemas/1.4.0",
isErr: false,
name: "three different schema urls",
schema: []string{
"https://opentelemetry.io/schemas/1.3.0",
"https://opentelemetry.io/schemas/1.4.0",
"https://opentelemetry.io/schemas/1.5.0",
},
wantErr: resource.ErrSchemaURLConflict,
},
{
name: "same schema url",
schema: []string{
"https://opentelemetry.io/schemas/1.4.0",
"https://opentelemetry.io/schemas/1.4.0",
},
},
}

for _, c := range cases {
t.Run(fmt.Sprintf("case-%s", c.name), func(t *testing.T) {
d1 := resource.StringDetector(c.schema1, semconv.HostNameKey, os.Hostname)
d2 := resource.StringDetector(c.schema2, semconv.HostNameKey, os.Hostname)
r, err := resource.Detect(context.Background(), d1, d2)
detectors := make([]resource.Detector, len(c.schema))
for i, s := range c.schema {
detectors[i] = resource.StringDetector(s, semconv.HostNameKey, os.Hostname)
}
r, err := resource.Detect(context.Background(), detectors...)
assert.NotNil(t, r)
if c.isErr {
assert.Error(t, err)
if c.wantErr != nil {
assert.ErrorIs(t, err, c.wantErr)
if errors.Is(c.wantErr, resource.ErrSchemaURLConflict) {
assert.Equal(t, "", r.SchemaURL())
}
} else {
assert.NoError(t, err)
}
Expand Down

0 comments on commit 4f1da09

Please sign in to comment.