Skip to content

Commit bbc51b2

Browse files
codebotenMrAlias
andauthored
otelconf: refactor resource kv code (#8053)
Signed-off-by: alex boten <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent 99b9e2a commit bbc51b2

File tree

6 files changed

+97
-144
lines changed

6 files changed

+97
-144
lines changed

otelconf/internal/kv/keyvalue.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package kv contains function to translate name value pairs into
5+
// attribute.KeyValue.
6+
package kv // import "go.opentelemetry.io/contrib/otelconf/internal/kv"
7+
8+
import (
9+
"fmt"
10+
"strconv"
11+
12+
"go.opentelemetry.io/otel/attribute"
13+
)
14+
15+
func FromNameValue(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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package kv
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
"go.opentelemetry.io/otel/attribute"
12+
)
13+
14+
func TestFromNameValue(t *testing.T) {
15+
other := struct{}{}
16+
for _, tt := range []struct {
17+
name string
18+
val any
19+
want attribute.KeyValue
20+
}{
21+
{name: "attr-bool", val: true, want: attribute.Bool("attr-bool", true)},
22+
{name: "attr-uint64", val: uint64(164), want: attribute.String("attr-uint64", fmt.Sprintf("%d", 164))},
23+
{name: "attr-int64", val: int64(-164), want: attribute.Int64("attr-int64", int64(-164))},
24+
{name: "attr-float64", val: float64(64.0), want: attribute.Float64("attr-float64", float64(64.0))},
25+
{name: "attr-int8", val: int8(-18), want: attribute.Int64("attr-int8", int64(-18))},
26+
{name: "attr-uint8", val: uint8(18), want: attribute.Int64("attr-uint8", int64(18))},
27+
{name: "attr-int16", val: int16(-116), want: attribute.Int64("attr-int16", int64(-116))},
28+
{name: "attr-uint16", val: uint16(116), want: attribute.Int64("attr-uint16", int64(116))},
29+
{name: "attr-int32", val: int32(-132), want: attribute.Int64("attr-int32", int64(-132))},
30+
{name: "attr-uint32", val: uint32(132), want: attribute.Int64("attr-uint32", int64(132))},
31+
{name: "attr-float32", val: float32(32.0), want: attribute.Float64("attr-float32", float64(32.0))},
32+
{name: "attr-int", val: int(-1), want: attribute.Int64("attr-int", int64(-1))},
33+
{name: "attr-uint", val: uint(1), want: attribute.String("attr-uint", fmt.Sprintf("%d", 1))},
34+
{name: "attr-string", val: "string-val", want: attribute.String("attr-string", "string-val")},
35+
{name: "attr-default", val: other, want: attribute.String("attr-default", fmt.Sprintf("%v", other))},
36+
} {
37+
t.Run(tt.name, func(t *testing.T) {
38+
require.Equal(t, tt.want, FromNameValue(tt.name, tt.val))
39+
})
40+
}
41+
}

otelconf/v0.2.0/resource.go

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,11 @@
44
package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.2.0"
55

66
import (
7-
"fmt"
8-
"strconv"
9-
107
"go.opentelemetry.io/otel/attribute"
118
"go.opentelemetry.io/otel/sdk/resource"
12-
)
139

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

4913
func newResource(res *Resource) (*resource.Resource, error) {
5014
if res == nil || res.Attributes == nil {
@@ -53,7 +17,7 @@ func newResource(res *Resource) (*resource.Resource, error) {
5317
var attrs []attribute.KeyValue
5418

5519
for k, v := range res.Attributes {
56-
attrs = append(attrs, keyVal(k, v))
20+
attrs = append(attrs, kv.FromNameValue(k, v))
5721
}
5822

5923
return resource.Merge(resource.Default(),

otelconf/v0.2.0/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,33 +13,16 @@ import (
1413
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
1514
)
1615

17-
type mockType struct{}
18-
1916
func TestNewResource(t *testing.T) {
2017
res, err := resource.Merge(resource.Default(),
2118
resource.NewWithAttributes(semconv.SchemaURL,
2219
semconv.ServiceName("service-a"),
2320
))
24-
other := mockType{}
2521
require.NoError(t, err)
2622
resWithAttrs, err := resource.Merge(resource.Default(),
2723
resource.NewWithAttributes(semconv.SchemaURL,
2824
semconv.ServiceName("service-a"),
2925
attribute.Bool("attr-bool", true),
30-
attribute.String("attr-uint64", fmt.Sprintf("%d", 164)),
31-
attribute.Int64("attr-int64", int64(-164)),
32-
attribute.Float64("attr-float64", float64(64.0)),
33-
attribute.Int64("attr-int8", int64(-18)),
34-
attribute.Int64("attr-uint8", int64(18)),
35-
attribute.Int64("attr-int16", int64(-116)),
36-
attribute.Int64("attr-uint16", int64(116)),
37-
attribute.Int64("attr-int32", int64(-132)),
38-
attribute.Int64("attr-uint32", int64(132)),
39-
attribute.Float64("attr-float32", float64(32.0)),
40-
attribute.Int64("attr-int", int64(-1)),
41-
attribute.String("attr-uint", fmt.Sprintf("%d", 1)),
42-
attribute.String("attr-string", "string-val"),
43-
attribute.String("attr-default", fmt.Sprintf("%v", other)),
4426
))
4527
require.NoError(t, err)
4628
tests := []struct {
@@ -61,7 +43,7 @@ func TestNewResource(t *testing.T) {
6143
{
6244
name: "resource-with-attributes-invalid-schema",
6345
config: &Resource{
64-
SchemaUrl: ptr("https://opentelemetry.io/invalid-schema"),
46+
SchemaUrl: ptr("https://opentelemetry.io/"),
6547
Attributes: Attributes{
6648
"service.name": "service-a",
6749
},
@@ -85,20 +67,6 @@ func TestNewResource(t *testing.T) {
8567
Attributes: Attributes{
8668
"service.name": "service-a",
8769
"attr-bool": true,
88-
"attr-int64": int64(-164),
89-
"attr-uint64": uint64(164),
90-
"attr-float64": float64(64.0),
91-
"attr-int8": int8(-18),
92-
"attr-uint8": uint8(18),
93-
"attr-int16": int16(-116),
94-
"attr-uint16": uint16(116),
95-
"attr-int32": int32(-132),
96-
"attr-uint32": uint32(132),
97-
"attr-float32": float32(32.0),
98-
"attr-int": int(-1),
99-
"attr-uint": uint(1),
100-
"attr-string": "string-val",
101-
"attr-default": other,
10270
},
10371
SchemaUrl: ptr(semconv.SchemaURL),
10472
},

otelconf/v0.3.0/resource.go

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,11 @@
44
package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.3.0"
55

66
import (
7-
"fmt"
8-
"strconv"
9-
107
"go.opentelemetry.io/otel/attribute"
118
"go.opentelemetry.io/otel/sdk/resource"
12-
)
139

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

4913
func newResource(res *Resource) *resource.Resource {
5014
if res == nil {
@@ -53,7 +17,7 @@ func newResource(res *Resource) *resource.Resource {
5317

5418
var attrs []attribute.KeyValue
5519
for _, v := range res.Attributes {
56-
attrs = append(attrs, keyVal(v.Name, v.Value))
20+
attrs = append(attrs, kv.FromNameValue(v.Name, v.Value))
5721
}
5822

5923
if res.SchemaUrl == nil {

otelconf/v0.3.0/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"
@@ -13,10 +12,7 @@ import (
1312
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
1413
)
1514

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

0 commit comments

Comments
 (0)