Skip to content

Commit

Permalink
Update internal/shared/logutil/convert.go.tmpl
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Pająk <[email protected]>
  • Loading branch information
m1heng and pellared committed Oct 14, 2024
1 parent 9bca788 commit 27601bc
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 35 deletions.
12 changes: 6 additions & 6 deletions bridges/otellogr/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"go.opentelemetry.io/otel/log"
)

// Convert various types to log.Value.
func ConvertValue(v any) log.Value {
// convertValue converts various types to log.Value.
func convertValue(v any) log.Value {
// Handling the most common types without reflect is a small perf win.
switch val := v.(type) {
case bool:
Expand Down Expand Up @@ -79,7 +79,7 @@ func ConvertValue(v any) log.Value {
case reflect.Slice, reflect.Array:
items := make([]log.Value, 0, val.Len())
for i := 0; i < val.Len(); i++ {
items = append(items, ConvertValue(val.Index(i).Interface()))
items = append(items, convertValue(val.Index(i).Interface()))
}
return log.SliceValue(items...)
case reflect.Map:
Expand All @@ -94,15 +94,15 @@ func ConvertValue(v any) log.Value {
}
kvs = append(kvs, log.KeyValue{
Key: key,
Value: ConvertValue(val.MapIndex(k).Interface()),
Value: convertValue(val.MapIndex(k).Interface()),
})
}
return log.MapValue(kvs...)
case reflect.Ptr, reflect.Interface:
if val.IsNil() {
return log.Value{}
}
return ConvertValue(val.Elem().Interface())
return convertValue(val.Elem().Interface())
}

// Try to handle this as gracefully as possible.
Expand All @@ -113,7 +113,7 @@ func ConvertValue(v any) log.Value {
return log.StringValue(fmt.Sprintf("unhandled: (%s) %+v", t, v))
}

// ConvertUintValue converts a uint64 to a log.Value.
// convertUintValue converts a uint64 to a log.Value.
// If the value is too large to fit in an int64, it is converted to a string.
func convertUintValue(v uint64) log.Value {
if v > math.MaxInt64 {
Expand Down
4 changes: 2 additions & 2 deletions bridges/otellogr/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ func TestConvertValue(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantValue, ConvertValue(tt.value))
assert.Equal(t, tt.wantValue, convertValue(tt.value))
})
}
}

func TestConvertValueFloat32(t *testing.T) {
value := ConvertValue(float32(3.14))
value := convertValue(float32(3.14))
want := log.Float64Value(3.14)

assert.InDelta(t, value.AsFloat64(), want.AsFloat64(), 0.0001)
Expand Down
2 changes: 1 addition & 1 deletion bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func convertKVs(ctx context.Context, keysAndValues ...any) (context.Context, []l

kvs = append(kvs, log.KeyValue{
Key: k,
Value: ConvertValue(v),
Value: convertValue(v),
})
}

Expand Down
12 changes: 6 additions & 6 deletions bridges/otellogrus/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"go.opentelemetry.io/otel/log"
)

// Convert various types to log.Value.
func ConvertValue(v any) log.Value {
// convertValue converts various types to log.Value.
func convertValue(v any) log.Value {
// Handling the most common types without reflect is a small perf win.
switch val := v.(type) {
case bool:
Expand Down Expand Up @@ -79,7 +79,7 @@ func ConvertValue(v any) log.Value {
case reflect.Slice, reflect.Array:
items := make([]log.Value, 0, val.Len())
for i := 0; i < val.Len(); i++ {
items = append(items, ConvertValue(val.Index(i).Interface()))
items = append(items, convertValue(val.Index(i).Interface()))
}
return log.SliceValue(items...)
case reflect.Map:
Expand All @@ -94,15 +94,15 @@ func ConvertValue(v any) log.Value {
}
kvs = append(kvs, log.KeyValue{
Key: key,
Value: ConvertValue(val.MapIndex(k).Interface()),
Value: convertValue(val.MapIndex(k).Interface()),
})
}
return log.MapValue(kvs...)
case reflect.Ptr, reflect.Interface:
if val.IsNil() {
return log.Value{}
}
return ConvertValue(val.Elem().Interface())
return convertValue(val.Elem().Interface())
}

// Try to handle this as gracefully as possible.
Expand All @@ -113,7 +113,7 @@ func ConvertValue(v any) log.Value {
return log.StringValue(fmt.Sprintf("unhandled: (%s) %+v", t, v))
}

// ConvertUintValue converts a uint64 to a log.Value.
// convertUintValue converts a uint64 to a log.Value.
// If the value is too large to fit in an int64, it is converted to a string.
func convertUintValue(v uint64) log.Value {
if v > math.MaxInt64 {
Expand Down
4 changes: 2 additions & 2 deletions bridges/otellogrus/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ func TestConvertValue(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantValue, ConvertValue(tt.value))
assert.Equal(t, tt.wantValue, convertValue(tt.value))
})
}
}

func TestConvertValueFloat32(t *testing.T) {
value := ConvertValue(float32(3.14))
value := convertValue(float32(3.14))
want := log.Float64Value(3.14)

assert.InDelta(t, value.AsFloat64(), want.AsFloat64(), 0.0001)
Expand Down
2 changes: 1 addition & 1 deletion bridges/otellogrus/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func convertFields(fields logrus.Fields) []log.KeyValue {
for k, v := range fields {
kvs = append(kvs, log.KeyValue{
Key: k,
Value: ConvertValue(v),
Value: convertValue(v),
})
}
return kvs
Expand Down
12 changes: 6 additions & 6 deletions bridges/otelzap/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"go.opentelemetry.io/otel/log"
)

// Convert various types to log.Value.
func ConvertValue(v any) log.Value {
// convertValue converts various types to log.Value.
func convertValue(v any) log.Value {
// Handling the most common types without reflect is a small perf win.
switch val := v.(type) {
case bool:
Expand Down Expand Up @@ -79,7 +79,7 @@ func ConvertValue(v any) log.Value {
case reflect.Slice, reflect.Array:
items := make([]log.Value, 0, val.Len())
for i := 0; i < val.Len(); i++ {
items = append(items, ConvertValue(val.Index(i).Interface()))
items = append(items, convertValue(val.Index(i).Interface()))
}
return log.SliceValue(items...)
case reflect.Map:
Expand All @@ -94,15 +94,15 @@ func ConvertValue(v any) log.Value {
}
kvs = append(kvs, log.KeyValue{
Key: key,
Value: ConvertValue(val.MapIndex(k).Interface()),
Value: convertValue(val.MapIndex(k).Interface()),
})
}
return log.MapValue(kvs...)
case reflect.Ptr, reflect.Interface:
if val.IsNil() {
return log.Value{}
}
return ConvertValue(val.Elem().Interface())
return convertValue(val.Elem().Interface())
}

// Try to handle this as gracefully as possible.
Expand All @@ -113,7 +113,7 @@ func ConvertValue(v any) log.Value {
return log.StringValue(fmt.Sprintf("unhandled: (%s) %+v", t, v))
}

// ConvertUintValue converts a uint64 to a log.Value.
// convertUintValue converts a uint64 to a log.Value.
// If the value is too large to fit in an int64, it is converted to a string.
func convertUintValue(v uint64) log.Value {
if v > math.MaxInt64 {
Expand Down
4 changes: 2 additions & 2 deletions bridges/otelzap/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ func TestConvertValue(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantValue, ConvertValue(tt.value))
assert.Equal(t, tt.wantValue, convertValue(tt.value))
})
}
}

func TestConvertValueFloat32(t *testing.T) {
value := ConvertValue(float32(3.14))
value := convertValue(float32(3.14))
want := log.Float64Value(3.14)

assert.InDelta(t, value.AsFloat64(), want.AsFloat64(), 0.0001)
Expand Down
4 changes: 2 additions & 2 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (m *objectEncoder) AddReflected(k string, v interface{}) error {
m.cur.attrs = append(m.cur.attrs,
log.KeyValue{
Key: k,
Value: ConvertValue(v),
Value: convertValue(v),
})
return nil
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func (a *arrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {
}

func (a *arrayEncoder) AppendReflected(v interface{}) error {
a.elems = append(a.elems, ConvertValue(v))
a.elems = append(a.elems, convertValue(v))
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions internal/shared/logutil/convert.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"go.opentelemetry.io/otel/log"
)

// Convert various types to log.Value.
func ConvertValue(v any) log.Value {
// convertValue converts various types to log.Value.
func convertValue(v any) log.Value {
// Handling the most common types without reflect is a small perf win.
switch val := v.(type) {
case bool:
Expand Down Expand Up @@ -79,7 +79,7 @@ func ConvertValue(v any) log.Value {
case reflect.Slice, reflect.Array:
items := make([]log.Value, 0, val.Len())
for i := 0; i < val.Len(); i++ {
items = append(items, ConvertValue(val.Index(i).Interface()))
items = append(items, convertValue(val.Index(i).Interface()))
}
return log.SliceValue(items...)
case reflect.Map:
Expand All @@ -94,15 +94,15 @@ func ConvertValue(v any) log.Value {
}
kvs = append(kvs, log.KeyValue{
Key: key,
Value: ConvertValue(val.MapIndex(k).Interface()),
Value: convertValue(val.MapIndex(k).Interface()),
})
}
return log.MapValue(kvs...)
case reflect.Ptr, reflect.Interface:
if val.IsNil() {
return log.Value{}
}
return ConvertValue(val.Elem().Interface())
return convertValue(val.Elem().Interface())
}

// Try to handle this as gracefully as possible.
Expand Down
4 changes: 2 additions & 2 deletions internal/shared/logutil/convert_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ func TestConvertValue(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantValue, ConvertValue(tt.value))
assert.Equal(t, tt.wantValue, convertValue(tt.value))
})
}
}

func TestConvertValueFloat32(t *testing.T) {
value := ConvertValue(float32(3.14))
value := convertValue(float32(3.14))
want := log.Float64Value(3.14)

assert.InDelta(t, value.AsFloat64(), want.AsFloat64(), 0.0001)
Expand Down

0 comments on commit 27601bc

Please sign in to comment.