Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions otelconf/internal/kv/keyvalue.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
41 changes: 41 additions & 0 deletions otelconf/internal/kv/keyvalue_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
42 changes: 3 additions & 39 deletions otelconf/v0.2.0/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand Down
34 changes: 1 addition & 33 deletions otelconf/v0.2.0/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package otelconf

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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 {
Expand All @@ -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",
},
Expand All @@ -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),
},
Expand Down
42 changes: 3 additions & 39 deletions otelconf/v0.3.0/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
34 changes: 1 addition & 33 deletions otelconf/v0.3.0/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package otelconf

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down