-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: refactor resource code (#4824)
* config: refactor resource code This moves the resource code out of config.go and into resource.go. Fixes #4821 Signed-off-by: Alex Boten <aboten@lightstep.com> * Update resource.go * Update resource_test.go --------- Signed-off-by: Alex Boten <aboten@lightstep.com> Co-authored-by: Robert Pająk <pellared@hotmail.com>
- Loading branch information
Showing
3 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config // import "go.opentelemetry.io/contrib/config" | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.24.0" | ||
) | ||
|
||
func newResource(res *Resource) (*resource.Resource, error) { | ||
if res == nil || res.Attributes == nil { | ||
return resource.Default(), nil | ||
} | ||
return resource.Merge(resource.Default(), | ||
resource.NewWithAttributes(*res.SchemaUrl, | ||
semconv.ServiceName(*res.Attributes.ServiceName), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config // import "go.opentelemetry.io/contrib/config" | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.24.0" | ||
) | ||
|
||
func TestNewResource(t *testing.T) { | ||
res, err := resource.Merge(resource.Default(), | ||
resource.NewWithAttributes(semconv.SchemaURL, | ||
semconv.ServiceName("service-a"), | ||
)) | ||
require.NoError(t, err) | ||
tests := []struct { | ||
name string | ||
config *Resource | ||
wantResource *resource.Resource | ||
wantErr error | ||
}{ | ||
{ | ||
name: "no-resource-configuration", | ||
wantResource: resource.Default(), | ||
}, | ||
{ | ||
name: "resource-no-attributes", | ||
config: &Resource{}, | ||
wantResource: resource.Default(), | ||
}, | ||
{ | ||
name: "resource-with-attributes-invalid-schema", | ||
config: &Resource{ | ||
SchemaUrl: ptr("https://opentelemetry.io/invalid-schema"), | ||
Attributes: &Attributes{ | ||
ServiceName: ptr("service-a"), | ||
}, | ||
}, | ||
wantResource: resource.Empty(), | ||
wantErr: errors.New("cannot merge resource due to conflicting Schema URL"), | ||
}, | ||
{ | ||
name: "resource-with-attributes-and-schema", | ||
config: &Resource{ | ||
Attributes: &Attributes{ | ||
ServiceName: ptr("service-a"), | ||
}, | ||
SchemaUrl: ptr(semconv.SchemaURL), | ||
}, | ||
wantResource: res, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := newResource(tt.config) | ||
require.Equal(t, tt.wantErr, err) | ||
assert.Equal(t, tt.wantResource, got) | ||
}) | ||
} | ||
} |