Skip to content

Commit

Permalink
Fix comments and simplify a bit of code
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed May 27, 2021
1 parent 4185685 commit e689c86
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -31,6 +31,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
It can be used to decode a `TraceState` from a `tracestate` header string value. (#1937)
- The `Len` method is added to the `TraceState` type in the `go.opentelemetry.io/otel/trace` package.
This method returns the number of list-members the `TraceState` holds. (#1937)
- Changes `resource.NewWithAttributes` to require a schema URL. The old function is still available as `resource.NewSchemaless`. This is a breaking change. (#1938)
- Several builtin resource detectors now correctly populate the schema URL. (#1938)

### Changed

Expand Down
3 changes: 2 additions & 1 deletion sdk/resource/builtin.go
Expand Up @@ -72,7 +72,8 @@ func (host) Detect(ctx context.Context) (*Resource, error) {
}

// StringDetector returns a Detector that will produce a *Resource
// containing the string as a value corresponding to k.
// containing the string as a value corresponding to k. The resulting Resource
// will have the specified schemaURL.
func StringDetector(schemaURL string, k attribute.Key, f func() (string, error)) Detector {
return stringDetector{schemaURL: schemaURL, K: k, F: f}
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/resource/config.go
Expand Up @@ -67,7 +67,7 @@ func WithBuiltinDetectors() Option {
fromEnv{})
}

// WithFromEnv adds attributes from environment variables to the configured resource.
// WithFromEnv adds attributes from environment variables to the configured resource.
func WithFromEnv() Option {
return WithDetectors(fromEnv{})
}
Expand All @@ -82,7 +82,7 @@ func WithTelemetrySDK() Option {
return WithDetectors(telemetrySDK{})
}

// WithSchemaURL sets the schema URL for the the configured resource.
// WithSchemaURL sets the schema URL for the configured resource.
func WithSchemaURL(schemaURL string) Option {
return schemaURLOption(schemaURL)
}
Expand Down
14 changes: 6 additions & 8 deletions sdk/resource/resource.go
Expand Up @@ -59,14 +59,12 @@ func New(ctx context.Context, opts ...Option) (*Resource, error) {

resource, err := Detect(ctx, cfg.detectors...)

if resource != nil {
var err2 error
resource, err2 = Merge(resource, &Resource{schemaURL: cfg.schemaURL})
if err == nil {
err = err2
} else if err2 != nil {
err = fmt.Errorf("detecting resources: %s", []string{err.Error(), err2.Error()})
}
var err2 error
resource, err2 = Merge(resource, &Resource{schemaURL: cfg.schemaURL})
if err == nil {
err = err2
} else if err2 != nil {
err = fmt.Errorf("detecting resources: %s", []string{err.Error(), err2.Error()})
}

return resource, err
Expand Down
6 changes: 4 additions & 2 deletions sdk/resource/resource_test.go
Expand Up @@ -17,6 +17,7 @@ package resource_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -408,6 +409,7 @@ func TestNew(t *testing.T) {
resource.WithSchemaURL("https://opentelemetry.io/schemas/1.0.0"),
},
resourceValues: map[string]string{},
schemaURL: "",
isErr: true,
},
{
Expand All @@ -416,12 +418,12 @@ func TestNew(t *testing.T) {
options: []resource.Option{
resource.WithDetectors(
resource.StringDetector("https://opentelemetry.io/schemas/1.0.0", semconv.HostNameKey, os.Hostname),
resource.StringDetector("https://opentelemetry.io/schemas/1.1.0", semconv.HostNameKey, os.Hostname),
resource.StringDetector("https://opentelemetry.io/schemas/1.1.0", semconv.HostNameKey, func() (string, error) { return "", errors.New("fail") }),
),
resource.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0"),
},
resourceValues: map[string]string{},
schemaURL: "https://opentelemetry.io/schemas/1.2.0",
schemaURL: "",
isErr: true,
},
}
Expand Down

0 comments on commit e689c86

Please sign in to comment.