Debian Codegraph @

bridges/otelslog/convert.go

raw symbols SCIP
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/logutil/convert.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package otelslog // import "go.opentelemetry.io/contrib/bridges/otelslog"
8
9 import (
10 "fmt"
11 "math"
12 "reflect"
13 "strconv"
14 "time"
15
16 "go.opentelemetry.io/otel/attribute"
17 "go.opentelemetry.io/otel/log"
18 )
19
20 // convertValue converts various types to log.Value.
21 func convertValue(v any) log.Value {
22 // Handling the most common types without reflect is a small perf win.
23 switch val := v.(type) {
24 case bool:
25 return log.BoolValue(val)
26 case string:
27 return log.StringValue(val)
28 case int:
29 return log.Int64Value(int64(val))
30 case int8:
31 return log.Int64Value(int64(val))
32 case int16:
33 return log.Int64Value(int64(val))
34 case int32:
35 return log.Int64Value(int64(val))
36 case int64:
37 return log.Int64Value(val)
38 case uint:
39 return convertUintValue(uint64(val))
40 case uint8:
41 return log.Int64Value(int64(val))
42 case uint16:
43 return log.Int64Value(int64(val))
44 case uint32:
45 return log.Int64Value(int64(val))
46 case uint64:
47 return convertUintValue(val)
48 case uintptr:
49 return convertUintValue(uint64(val))
50 case float32:
51 return log.Float64Value(float64(val))
52 case float64:
53 return log.Float64Value(val)
54 case time.Duration:
55 return log.Int64Value(val.Nanoseconds())
56 case complex64:
57 r := log.Float64("r", real(complex128(val)))
58 i := log.Float64("i", imag(complex128(val)))
59 return log.MapValue(r, i)
60 case complex128:
61 r := log.Float64("r", real(val))
62 i := log.Float64("i", imag(val))
63 return log.MapValue(r, i)
64 case time.Time:
65 return log.Int64Value(val.UnixNano())
66 case []byte:
67 return log.BytesValue(val)
68 case error:
69 return log.StringValue(val.Error())
70 case attribute.Value:
71 return log.ValueFromAttribute(val)
72 case log.Value:
73 return val
74 }
75
76 t := reflect.TypeOf(v)
77 if t == nil {
78 return log.Value{}
79 }
80 val := reflect.ValueOf(v)
81 switch t.Kind() {
82 case reflect.Struct:
83 return log.StringValue(fmt.Sprintf("%+v", v))
84 case reflect.Slice, reflect.Array:
85 items := make([]log.Value, 0, val.Len())
86 for i := 0; i < val.Len(); i++ {
87 items = append(items, convertValue(val.Index(i).Interface()))
88 }
89 return log.SliceValue(items...)
90 case reflect.Map:
91 kvs := make([]log.KeyValue, 0, val.Len())
92 for _, k := range val.MapKeys() {
93 var key string
94 switch k.Kind() {
95 case reflect.String:
96 key = k.String()
97 default:
98 key = fmt.Sprintf("%+v", k.Interface())
99 }
100 kvs = append(kvs, log.KeyValue{
101 Key: key,
102 Value: convertValue(val.MapIndex(k).Interface()),
103 })
104 }
105 return log.MapValue(kvs...)
106 case reflect.Ptr, reflect.Interface:
107 if val.IsNil() {
108 return log.Value{}
109 }
110 return convertValue(val.Elem().Interface())
111 }
112
113 // Try to handle this as gracefully as possible.
114 //
115 // Don't panic here. it is preferable to have user's open issue
116 // asking why their attributes have a "unhandled: " prefix than
117 // say that their code is panicking.
118 return log.StringValue(fmt.Sprintf("unhandled: (%s) %+v", t, v))
119 }
120
121 // convertUintValue converts a uint64 to a log.Value.
122 // If the value is too large to fit in an int64, it is converted to a string.
123 func convertUintValue(v uint64) log.Value {
124 if v > math.MaxInt64 {
125 return log.StringValue(strconv.FormatUint(v, 10))
126 }
127 return log.Int64Value(int64(v))
128 }