diff --git a/otelconf/internal/kv/keyvalue.go b/otelconf/internal/kv/keyvalue.go new file mode 100644 index 00000000000..97412d1a2b6 --- /dev/null +++ b/otelconf/internal/kv/keyvalue.go @@ -0,0 +1,48 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Package kv contains function to translate name value pairs into +// attribute.KeyValue. +package kv // import "go.opentelemetry.io/contrib/otelconf/internal/kv" + +import ( + "fmt" + "strconv" + + "go.opentelemetry.io/otel/attribute" +) + +func FromNameValue(k string, v any) attribute.KeyValue { + switch val := v.(type) { + case bool: + return attribute.Bool(k, val) + case int64: + return attribute.Int64(k, val) + case uint64: + return attribute.String(k, strconv.FormatUint(val, 10)) + case float64: + return attribute.Float64(k, val) + case int8: + return attribute.Int64(k, int64(val)) + case uint8: + return attribute.Int64(k, int64(val)) + case int16: + return attribute.Int64(k, int64(val)) + case uint16: + return attribute.Int64(k, int64(val)) + case int32: + return attribute.Int64(k, int64(val)) + case uint32: + return attribute.Int64(k, int64(val)) + case float32: + return attribute.Float64(k, float64(val)) + case int: + return attribute.Int(k, val) + case uint: + return attribute.String(k, strconv.FormatUint(uint64(val), 10)) + case string: + return attribute.String(k, val) + default: + return attribute.String(k, fmt.Sprint(v)) + } +} diff --git a/otelconf/internal/kv/keyvalue_test.go b/otelconf/internal/kv/keyvalue_test.go new file mode 100644 index 00000000000..b6936e9c02c --- /dev/null +++ b/otelconf/internal/kv/keyvalue_test.go @@ -0,0 +1,41 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package kv + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/attribute" +) + +func TestFromNameValue(t *testing.T) { + other := struct{}{} + for _, tt := range []struct { + name string + val any + want attribute.KeyValue + }{ + {name: "attr-bool", val: true, want: attribute.Bool("attr-bool", true)}, + {name: "attr-uint64", val: uint64(164), want: attribute.String("attr-uint64", fmt.Sprintf("%d", 164))}, + {name: "attr-int64", val: int64(-164), want: attribute.Int64("attr-int64", int64(-164))}, + {name: "attr-float64", val: float64(64.0), want: attribute.Float64("attr-float64", float64(64.0))}, + {name: "attr-int8", val: int8(-18), want: attribute.Int64("attr-int8", int64(-18))}, + {name: "attr-uint8", val: uint8(18), want: attribute.Int64("attr-uint8", int64(18))}, + {name: "attr-int16", val: int16(-116), want: attribute.Int64("attr-int16", int64(-116))}, + {name: "attr-uint16", val: uint16(116), want: attribute.Int64("attr-uint16", int64(116))}, + {name: "attr-int32", val: int32(-132), want: attribute.Int64("attr-int32", int64(-132))}, + {name: "attr-uint32", val: uint32(132), want: attribute.Int64("attr-uint32", int64(132))}, + {name: "attr-float32", val: float32(32.0), want: attribute.Float64("attr-float32", float64(32.0))}, + {name: "attr-int", val: int(-1), want: attribute.Int64("attr-int", int64(-1))}, + {name: "attr-uint", val: uint(1), want: attribute.String("attr-uint", fmt.Sprintf("%d", 1))}, + {name: "attr-string", val: "string-val", want: attribute.String("attr-string", "string-val")}, + {name: "attr-default", val: other, want: attribute.String("attr-default", fmt.Sprintf("%v", other))}, + } { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, FromNameValue(tt.name, tt.val)) + }) + } +} diff --git a/otelconf/v0.2.0/resource.go b/otelconf/v0.2.0/resource.go index 84f3e30dab5..889cc974be3 100644 --- a/otelconf/v0.2.0/resource.go +++ b/otelconf/v0.2.0/resource.go @@ -4,47 +4,11 @@ package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.2.0" import ( - "fmt" - "strconv" - "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/sdk/resource" -) -func keyVal(k string, v any) attribute.KeyValue { - switch val := v.(type) { - case bool: - return attribute.Bool(k, val) - case int64: - return attribute.Int64(k, val) - case uint64: - return attribute.String(k, strconv.FormatUint(val, 10)) - case float64: - return attribute.Float64(k, val) - case int8: - return attribute.Int64(k, int64(val)) - case uint8: - return attribute.Int64(k, int64(val)) - case int16: - return attribute.Int64(k, int64(val)) - case uint16: - return attribute.Int64(k, int64(val)) - case int32: - return attribute.Int64(k, int64(val)) - case uint32: - return attribute.Int64(k, int64(val)) - case float32: - return attribute.Float64(k, float64(val)) - case int: - return attribute.Int(k, val) - case uint: - return attribute.String(k, strconv.FormatUint(uint64(val), 10)) - case string: - return attribute.String(k, val) - default: - return attribute.String(k, fmt.Sprint(v)) - } -} + "go.opentelemetry.io/contrib/otelconf/internal/kv" +) func newResource(res *Resource) (*resource.Resource, error) { if res == nil || res.Attributes == nil { @@ -53,7 +17,7 @@ func newResource(res *Resource) (*resource.Resource, error) { var attrs []attribute.KeyValue for k, v := range res.Attributes { - attrs = append(attrs, keyVal(k, v)) + attrs = append(attrs, kv.FromNameValue(k, v)) } return resource.Merge(resource.Default(), diff --git a/otelconf/v0.2.0/resource_test.go b/otelconf/v0.2.0/resource_test.go index 6fd02b5f263..77bd247f9ba 100644 --- a/otelconf/v0.2.0/resource_test.go +++ b/otelconf/v0.2.0/resource_test.go @@ -4,7 +4,6 @@ package otelconf import ( - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -14,33 +13,16 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.37.0" ) -type mockType struct{} - func TestNewResource(t *testing.T) { res, err := resource.Merge(resource.Default(), resource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceName("service-a"), )) - other := mockType{} require.NoError(t, err) resWithAttrs, err := resource.Merge(resource.Default(), resource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceName("service-a"), attribute.Bool("attr-bool", true), - attribute.String("attr-uint64", fmt.Sprintf("%d", 164)), - attribute.Int64("attr-int64", int64(-164)), - attribute.Float64("attr-float64", float64(64.0)), - attribute.Int64("attr-int8", int64(-18)), - attribute.Int64("attr-uint8", int64(18)), - attribute.Int64("attr-int16", int64(-116)), - attribute.Int64("attr-uint16", int64(116)), - attribute.Int64("attr-int32", int64(-132)), - attribute.Int64("attr-uint32", int64(132)), - attribute.Float64("attr-float32", float64(32.0)), - attribute.Int64("attr-int", int64(-1)), - attribute.String("attr-uint", fmt.Sprintf("%d", 1)), - attribute.String("attr-string", "string-val"), - attribute.String("attr-default", fmt.Sprintf("%v", other)), )) require.NoError(t, err) tests := []struct { @@ -61,7 +43,7 @@ func TestNewResource(t *testing.T) { { name: "resource-with-attributes-invalid-schema", config: &Resource{ - SchemaUrl: ptr("https://opentelemetry.io/invalid-schema"), + SchemaUrl: ptr("https://opentelemetry.io/"), Attributes: Attributes{ "service.name": "service-a", }, @@ -85,20 +67,6 @@ func TestNewResource(t *testing.T) { Attributes: Attributes{ "service.name": "service-a", "attr-bool": true, - "attr-int64": int64(-164), - "attr-uint64": uint64(164), - "attr-float64": float64(64.0), - "attr-int8": int8(-18), - "attr-uint8": uint8(18), - "attr-int16": int16(-116), - "attr-uint16": uint16(116), - "attr-int32": int32(-132), - "attr-uint32": uint32(132), - "attr-float32": float32(32.0), - "attr-int": int(-1), - "attr-uint": uint(1), - "attr-string": "string-val", - "attr-default": other, }, SchemaUrl: ptr(semconv.SchemaURL), }, diff --git a/otelconf/v0.3.0/resource.go b/otelconf/v0.3.0/resource.go index 1b0830445de..5dde362b073 100644 --- a/otelconf/v0.3.0/resource.go +++ b/otelconf/v0.3.0/resource.go @@ -4,47 +4,11 @@ package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.3.0" import ( - "fmt" - "strconv" - "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/sdk/resource" -) -func keyVal(k string, v any) attribute.KeyValue { - switch val := v.(type) { - case bool: - return attribute.Bool(k, val) - case int64: - return attribute.Int64(k, val) - case uint64: - return attribute.String(k, strconv.FormatUint(val, 10)) - case float64: - return attribute.Float64(k, val) - case int8: - return attribute.Int64(k, int64(val)) - case uint8: - return attribute.Int64(k, int64(val)) - case int16: - return attribute.Int64(k, int64(val)) - case uint16: - return attribute.Int64(k, int64(val)) - case int32: - return attribute.Int64(k, int64(val)) - case uint32: - return attribute.Int64(k, int64(val)) - case float32: - return attribute.Float64(k, float64(val)) - case int: - return attribute.Int(k, val) - case uint: - return attribute.String(k, strconv.FormatUint(uint64(val), 10)) - case string: - return attribute.String(k, val) - default: - return attribute.String(k, fmt.Sprint(v)) - } -} + "go.opentelemetry.io/contrib/otelconf/internal/kv" +) func newResource(res *Resource) *resource.Resource { if res == nil { @@ -53,7 +17,7 @@ func newResource(res *Resource) *resource.Resource { var attrs []attribute.KeyValue for _, v := range res.Attributes { - attrs = append(attrs, keyVal(v.Name, v.Value)) + attrs = append(attrs, kv.FromNameValue(v.Name, v.Value)) } if res.SchemaUrl == nil { diff --git a/otelconf/v0.3.0/resource_test.go b/otelconf/v0.3.0/resource_test.go index dd9a3fc86c9..9206796dbb7 100644 --- a/otelconf/v0.3.0/resource_test.go +++ b/otelconf/v0.3.0/resource_test.go @@ -4,7 +4,6 @@ package otelconf import ( - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -13,10 +12,7 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.37.0" ) -type mockType struct{} - func TestNewResource(t *testing.T) { - other := mockType{} tests := []struct { name string config *Resource @@ -67,40 +63,12 @@ func TestNewResource(t *testing.T) { Attributes: []AttributeNameValue{ {Name: "service.name", Value: "service-a"}, {Name: "attr-bool", Value: true}, - {Name: "attr-int64", Value: int64(-164)}, - {Name: "attr-uint64", Value: uint64(164)}, - {Name: "attr-float64", Value: float64(64.0)}, - {Name: "attr-int8", Value: int8(-18)}, - {Name: "attr-uint8", Value: uint8(18)}, - {Name: "attr-int16", Value: int16(-116)}, - {Name: "attr-uint16", Value: uint16(116)}, - {Name: "attr-int32", Value: int32(-132)}, - {Name: "attr-uint32", Value: uint32(132)}, - {Name: "attr-float32", Value: float32(32.0)}, - {Name: "attr-int", Value: int(-1)}, - {Name: "attr-uint", Value: uint(1)}, - {Name: "attr-string", Value: "string-val"}, - {Name: "attr-default", Value: other}, }, SchemaUrl: ptr(semconv.SchemaURL), }, wantResource: resource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceName("service-a"), - attribute.Bool("attr-bool", true), - attribute.String("attr-uint64", fmt.Sprintf("%d", 164)), - attribute.Int64("attr-int64", int64(-164)), - attribute.Float64("attr-float64", float64(64.0)), - attribute.Int64("attr-int8", int64(-18)), - attribute.Int64("attr-uint8", int64(18)), - attribute.Int64("attr-int16", int64(-116)), - attribute.Int64("attr-uint16", int64(116)), - attribute.Int64("attr-int32", int64(-132)), - attribute.Int64("attr-uint32", int64(132)), - attribute.Float64("attr-float32", float64(32.0)), - attribute.Int64("attr-int", int64(-1)), - attribute.String("attr-uint", fmt.Sprintf("%d", 1)), - attribute.String("attr-string", "string-val"), - attribute.String("attr-default", fmt.Sprintf("%v", other))), + attribute.Bool("attr-bool", true)), }, } for _, tt := range tests {