| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package x |
| 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 |
} |
| 84 |
|
| 85 |
func TestResourceOptsWithDetectors(t *testing.T) { |
| 86 |
tests := []struct { |
| 87 |
name string |
| 88 |
detectors []ExperimentalResourceDetector |
| 89 |
wantHostAttributes bool |
| 90 |
wantOSAttributes bool |
| 91 |
wantProcessAttribute bool |
| 92 |
wantServiceAttribute bool |
| 93 |
}{ |
| 94 |
{ |
| 95 |
name: "no-detectors", |
| 96 |
detectors: []ExperimentalResourceDetector{}, |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "host-detector-enabled", |
| 100 |
detectors: []ExperimentalResourceDetector{ |
| 101 |
{Host: ExperimentalHostResourceDetector{}}, |
| 102 |
}, |
| 103 |
wantHostAttributes: true, |
| 104 |
wantOSAttributes: true, |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "process-detector-only", |
| 108 |
detectors: []ExperimentalResourceDetector{ |
| 109 |
{Process: ExperimentalProcessResourceDetector{}}, |
| 110 |
}, |
| 111 |
wantProcessAttribute: true, |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "all-detectors", |
| 115 |
detectors: []ExperimentalResourceDetector{ |
| 116 |
{Container: ExperimentalContainerResourceDetector{}}, |
| 117 |
{Host: ExperimentalHostResourceDetector{}}, |
| 118 |
{Process: ExperimentalProcessResourceDetector{}}, |
| 119 |
{Service: ExperimentalServiceResourceDetector{}}, |
| 120 |
}, |
| 121 |
wantHostAttributes: true, |
| 122 |
wantOSAttributes: true, |
| 123 |
wantProcessAttribute: true, |
| 124 |
wantServiceAttribute: true, |
| 125 |
}, |
| 126 |
} |
| 127 |
for _, tt := range tests { |
| 128 |
t.Run(tt.name, func(t *testing.T) { |
| 129 |
config := &Resource{ |
| 130 |
DetectionDevelopment: &ExperimentalResourceDetection{ |
| 131 |
Detectors: tt.detectors, |
| 132 |
}, |
| 133 |
} |
| 134 |
got, err := newResource(config) |
| 135 |
require.NoError(t, err) |
| 136 |
require.NotNil(t, got) |
| 137 |
|
| 138 |
attrs := got.Attributes() |
| 139 |
attrSet := make(map[attribute.Key]bool) |
| 140 |
for _, attr := range attrs { |
| 141 |
attrSet[attr.Key] = true |
| 142 |
} |
| 143 |
|
| 144 |
assert.Equal(t, tt.wantHostAttributes, attrSet[semconv.HostNameKey], "should have host.name attribute") |
| 145 |
assert.Equal(t, tt.wantOSAttributes, attrSet[semconv.OSTypeKey], "should have os.type attribute (from WithOS()") |
| 146 |
assert.Equal(t, tt.wantProcessAttribute, attrSet[semconv.ProcessPIDKey], "should have process.pid attribute") |
| 147 |
assert.Equal(t, tt.wantServiceAttribute, attrSet[semconv.ServiceInstanceIDKey], "should have service.instance.id attribute") |
| 148 |
}) |
| 149 |
} |
| 150 |
} |