Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return merged Resource on schema conflict #4876

Merged
merged 12 commits into from Feb 5, 2024
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 no
// 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",
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
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())
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
assert.NoError(t, err)
}
Expand Down