Skip to content

Commit

Permalink
Merge converter.go into resource.go
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Sep 13, 2023
1 parent 020f5fa commit 4d4ae1c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 49 deletions.
49 changes: 0 additions & 49 deletions sdk/resource/converter.go

This file was deleted.

29 changes: 29 additions & 0 deletions sdk/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ package resource // import "go.opentelemetry.io/otel/sdk/resource"

import (
"context"
"errors"
"fmt"
"sync"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource/internal/schema"
"go.opentelemetry.io/otel/sdk/resource/internal/schema/resconv"
)

// Resource describes an entity about which identifying information
Expand Down Expand Up @@ -218,6 +221,32 @@ func Merge(a, b *Resource) (*Resource, error) {
return merged, err
}

var errUnknownSchema = errors.New("unknown schema")

// upgradeResource returns a copy of orig with the schema URL set to schemaURL
// and all attributes transformed based on the associated schema. If the schema
// transformation fails an error is returned.
func upgradeResource(schemaURL string, r *Resource) (*Resource, error) {
if r == nil || r.Len() == 0 {
return NewWithAttributes(schemaURL), nil
}

if r.SchemaURL() == schemaURL {
// Resources are immutable, just return the ptr to the same value.
return r, nil
}

s, ok := schema.Schemas[schemaURL]
if !ok {
return nil, fmt.Errorf("%w: %s", errUnknownSchema, schemaURL)
}
attrs := r.Attributes()
if err := resconv.Upgrade(s, attrs); err != nil {
return nil, err
}
return NewWithAttributes(schemaURL, attrs...), nil
}

// Empty returns an instance of Resource with no attributes. It is
// equivalent to a `nil` Resource.
func Empty() *Resource {
Expand Down

0 comments on commit 4d4ae1c

Please sign in to comment.