Debian Codegraph @

otelconf/v0.3.0/resource_test.go

raw
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 "go.opentelemetry.io/otel/attribute"
11 "go.opentelemetry.io/otel/sdk/resource"
12 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
13 )
14
15 func TestNewResource(t *testing.T) {
16 tests := []struct {
17 name string
18 config *Resource
19 wantResource *resource.Resource
20 }{
21 {
22 name: "no-resource-configuration",
23 wantResource: resource.Default(),
24 },
25 {
26 name: "resource-no-attributes",
27 config: &Resource{},
28 wantResource: resource.NewSchemaless(),
29 },
30 {
31 name: "resource-with-schema",
32 config: &Resource{
33 SchemaUrl: ptr(semconv.SchemaURL),
34 },
35 wantResource: resource.NewWithAttributes(semconv.SchemaURL),
36 },
37 {
38 name: "resource-with-attributes",
39 config: &Resource{
40 Attributes: []AttributeNameValue{
41 {Name: "service.name", Value: "service-a"},
42 },
43 },
44 wantResource: resource.NewWithAttributes("",
45 semconv.ServiceName("service-a"),
46 ),
47 },
48 {
49 name: "resource-with-attributes-and-schema",
50 config: &Resource{
51 Attributes: []AttributeNameValue{
52 {Name: "service.name", Value: "service-a"},
53 },
54 SchemaUrl: ptr(semconv.SchemaURL),
55 },
56 wantResource: resource.NewWithAttributes(semconv.SchemaURL,
57 semconv.ServiceName("service-a"),
58 ),
59 },
60 {
61 name: "resource-with-additional-attributes-and-schema",
62 config: &Resource{
63 Attributes: []AttributeNameValue{
64 {Name: "service.name", Value: "service-a"},
65 {Name: "attr-bool", Value: true},
66 },
67 SchemaUrl: ptr(semconv.SchemaURL),
68 },
69 wantResource: resource.NewWithAttributes(semconv.SchemaURL,
70 semconv.ServiceName("service-a"),
71 attribute.Bool("attr-bool", true)),
72 },
73 }
74 for _, tt := range tests {
75 t.Run(tt.name, func(t *testing.T) {
76 got := newResource(tt.config)
77 assert.Equal(t, tt.wantResource, got)
78 })
79 }
80 }