Skip to content

Commit 29cee4c

Browse files
committed
reuse kv code
Signed-off-by: alex boten <[email protected]>
1 parent f42b70f commit 29cee4c

File tree

2 files changed

+4
-71
lines changed

2 files changed

+4
-71
lines changed

otelconf/resource.go

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,12 @@ package otelconf // import "go.opentelemetry.io/contrib/otelconf/v1.0.0-rc.1"
55

66
import (
77
"errors"
8-
"fmt"
9-
"strconv"
108

119
"go.opentelemetry.io/otel/attribute"
1210
"go.opentelemetry.io/otel/sdk/resource"
13-
)
1411

15-
func keyVal(k string, v any) attribute.KeyValue {
16-
switch val := v.(type) {
17-
case bool:
18-
return attribute.Bool(k, val)
19-
case int64:
20-
return attribute.Int64(k, val)
21-
case uint64:
22-
return attribute.String(k, strconv.FormatUint(val, 10))
23-
case float64:
24-
return attribute.Float64(k, val)
25-
case int8:
26-
return attribute.Int64(k, int64(val))
27-
case uint8:
28-
return attribute.Int64(k, int64(val))
29-
case int16:
30-
return attribute.Int64(k, int64(val))
31-
case uint16:
32-
return attribute.Int64(k, int64(val))
33-
case int32:
34-
return attribute.Int64(k, int64(val))
35-
case uint32:
36-
return attribute.Int64(k, int64(val))
37-
case float32:
38-
return attribute.Float64(k, float64(val))
39-
case int:
40-
return attribute.Int(k, val)
41-
case uint:
42-
return attribute.String(k, strconv.FormatUint(uint64(val), 10))
43-
case string:
44-
return attribute.String(k, val)
45-
default:
46-
return attribute.String(k, fmt.Sprint(v))
47-
}
48-
}
12+
"go.opentelemetry.io/contrib/otelconf/internal/kv"
13+
)
4914

5015
func newResource(res OpenTelemetryConfigurationResource) (*resource.Resource, error) {
5116
if res == nil {
@@ -59,7 +24,7 @@ func newResource(res OpenTelemetryConfigurationResource) (*resource.Resource, er
5924

6025
var attrs []attribute.KeyValue
6126
for _, v := range r.Attributes {
62-
attrs = append(attrs, keyVal(v.Name, v.Value))
27+
attrs = append(attrs, kv.FromNameValue(v.Name, v.Value))
6328
}
6429

6530
if r.SchemaUrl == nil {

otelconf/resource_test.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package otelconf
55

66
import (
7-
"fmt"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
@@ -14,10 +13,7 @@ import (
1413
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
1514
)
1615

17-
type mockType struct{}
18-
1916
func TestNewResource(t *testing.T) {
20-
other := mockType{}
2117
tests := []struct {
2218
name string
2319
config OpenTelemetryConfigurationResource
@@ -68,40 +64,12 @@ func TestNewResource(t *testing.T) {
6864
Attributes: []AttributeNameValue{
6965
{Name: "service.name", Value: "service-a"},
7066
{Name: "attr-bool", Value: true},
71-
{Name: "attr-int64", Value: int64(-164)},
72-
{Name: "attr-uint64", Value: uint64(164)},
73-
{Name: "attr-float64", Value: float64(64.0)},
74-
{Name: "attr-int8", Value: int8(-18)},
75-
{Name: "attr-uint8", Value: uint8(18)},
76-
{Name: "attr-int16", Value: int16(-116)},
77-
{Name: "attr-uint16", Value: uint16(116)},
78-
{Name: "attr-int32", Value: int32(-132)},
79-
{Name: "attr-uint32", Value: uint32(132)},
80-
{Name: "attr-float32", Value: float32(32.0)},
81-
{Name: "attr-int", Value: int(-1)},
82-
{Name: "attr-uint", Value: uint(1)},
83-
{Name: "attr-string", Value: "string-val"},
84-
{Name: "attr-default", Value: other},
8567
},
8668
SchemaUrl: ptr(semconv.SchemaURL),
8769
},
8870
wantResource: resource.NewWithAttributes(semconv.SchemaURL,
8971
semconv.ServiceName("service-a"),
90-
attribute.Bool("attr-bool", true),
91-
attribute.String("attr-uint64", fmt.Sprintf("%d", 164)),
92-
attribute.Int64("attr-int64", int64(-164)),
93-
attribute.Float64("attr-float64", float64(64.0)),
94-
attribute.Int64("attr-int8", int64(-18)),
95-
attribute.Int64("attr-uint8", int64(18)),
96-
attribute.Int64("attr-int16", int64(-116)),
97-
attribute.Int64("attr-uint16", int64(116)),
98-
attribute.Int64("attr-int32", int64(-132)),
99-
attribute.Int64("attr-uint32", int64(132)),
100-
attribute.Float64("attr-float32", float64(32.0)),
101-
attribute.Int64("attr-int", int64(-1)),
102-
attribute.String("attr-uint", fmt.Sprintf("%d", 1)),
103-
attribute.String("attr-string", "string-val"),
104-
attribute.String("attr-default", fmt.Sprintf("%v", other))),
72+
attribute.Bool("attr-bool", true)),
10573
},
10674
}
10775
for _, tt := range tests {

0 commit comments

Comments
 (0)