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

sdk/resource: Add Resource.WithoutSchemaURL #4484

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `WithProducer` option in `go.opentelemetry.op/otel/exporters/prometheus` to restore the ability to register producers on the prometheus exporter's manual reader. (#4473)
- Add `IgnoreValue` option in `go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest` to allow ignoring values when comparing metrics. (#4447)
- Add `Resource.WithoutSchemaURL` method in `go.opentelemetry.io/otel/sdk/resource` to allow mitigating schema URL conflits when merging resources. (#4484)

### Deprecated

Expand Down
11 changes: 10 additions & 1 deletion sdk/resource/resource.go
Expand Up @@ -146,6 +146,14 @@ func (r *Resource) Equal(eq *Resource) bool {
return r.Equivalent() == eq.Equivalent()
}

// WithoutSchemaURL returns a copy of the resource without schema URL.
//
// You can use this method to mitigate the error returned by [Merge]
// when resources have different non-empty schema URLs.
func (r *Resource) WithoutSchemaURL() *Resource {
return &Resource{attrs: r.attrs}
}

// Merge creates a new resource by combining resource a and b.
//
// If there are common keys between resource a and b, then the value
Expand All @@ -154,8 +162,9 @@ func (r *Resource) Equal(eq *Resource) bool {
//
// The SchemaURL of the resources will be merged according to the spec rules:
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/resource/sdk.md#merge
// If the resources have different non-empty schemaURL an empty resource and an error
// If the resources have different non-empty schema URLs, an empty resource and an error
// will be returned.
// You can use [Resource.WithoutSchemaURL] to mitigate the schema URL conflict.
func Merge(a, b *Resource) (*Resource, error) {
if a == nil && b == nil {
return Empty(), nil
Expand Down
12 changes: 12 additions & 0 deletions sdk/resource/resource_test.go
Expand Up @@ -453,6 +453,18 @@ func TestNew(t *testing.T) {
}
}

func TestWithoutSchemaURL(t *testing.T) {
schemaURL := "https://opentelemetry.io/schemas/1.4.0"
attrs := []attribute.KeyValue{kv11, kv21}
r := resource.NewWithAttributes(schemaURL, attrs...)

got := r.WithoutSchemaURL()

assert.Equal(t, schemaURL, r.SchemaURL(), "should not modify the original resource's SchemaURL")
assert.Empty(t, got.SchemaURL(), "should create a copy with empty SchemaURL")
assert.Equal(t, r.Attributes(), got.Attributes(), "should have the same attributes")
}

func TestNewWrapedError(t *testing.T) {
localErr := errors.New("local error")
_, err := resource.New(
Expand Down