| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelconf |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/sdk/resource" |
| 13 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 14 |
) |
| 15 |
|
| 16 |
func TestNewResource(t *testing.T) { |
| 17 |
tests := []struct { |
| 18 |
name string |
| 19 |
config *Resource |
| 20 |
wantResource *resource.Resource |
| 21 |
wantErrT error |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "no-resource-configuration", |
| 25 |
wantResource: resource.Default(), |
| 26 |
}, |
| 27 |
{ |
| 28 |
name: "resource-no-attributes", |
| 29 |
config: &Resource{}, |
| 30 |
wantResource: resource.NewSchemaless(), |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "resource-with-schema", |
| 34 |
config: &Resource{ |
| 35 |
SchemaUrl: ptr(semconv.SchemaURL), |
| 36 |
}, |
| 37 |
wantResource: resource.NewWithAttributes(semconv.SchemaURL), |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "resource-with-attributes", |
| 41 |
config: &Resource{ |
| 42 |
Attributes: []AttributeNameValue{ |
| 43 |
{Name: string(semconv.ServiceNameKey), Value: "service-a"}, |
| 44 |
}, |
| 45 |
}, |
| 46 |
wantResource: resource.NewWithAttributes("", |
| 47 |
semconv.ServiceName("service-a"), |
| 48 |
), |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "resource-with-attributes-and-schema", |
| 52 |
config: &Resource{ |
| 53 |
Attributes: []AttributeNameValue{ |
| 54 |
{Name: string(semconv.ServiceNameKey), Value: "service-a"}, |
| 55 |
}, |
| 56 |
SchemaUrl: ptr(semconv.SchemaURL), |
| 57 |
}, |
| 58 |
wantResource: resource.NewWithAttributes(semconv.SchemaURL, |
| 59 |
semconv.ServiceName("service-a"), |
| 60 |
), |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "resource-with-additional-attributes-and-schema", |
| 64 |
config: &Resource{ |
| 65 |
Attributes: []AttributeNameValue{ |
| 66 |
{Name: string(semconv.ServiceNameKey), Value: "service-a"}, |
| 67 |
{Name: "attr-bool", Value: true}, |
| 68 |
}, |
| 69 |
SchemaUrl: ptr(semconv.SchemaURL), |
| 70 |
}, |
| 71 |
wantResource: resource.NewWithAttributes(semconv.SchemaURL, |
| 72 |
semconv.ServiceName("service-a"), |
| 73 |
attribute.Bool("attr-bool", true)), |
| 74 |
}, |
| 75 |
} |
| 76 |
for _, tt := range tests { |
| 77 |
t.Run(tt.name, func(t *testing.T) { |
| 78 |
got, err := newResource(tt.config) |
| 79 |
require.ErrorIs(t, tt.wantErrT, err) |
| 80 |
assert.Equal(t, tt.wantResource, got) |
| 81 |
}) |
| 82 |
} |
| 83 |
} |