value.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package attribute // import "go.opentelemetry.io/otel/attribute"
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "reflect"
  8. "strconv"
  9. "go.opentelemetry.io/otel/internal"
  10. "go.opentelemetry.io/otel/internal/attribute"
  11. )
  12. //go:generate stringer -type=Type
  13. // Type describes the type of the data Value holds.
  14. type Type int // nolint: revive // redefines builtin Type.
  15. // Value represents the value part in key-value pairs.
  16. type Value struct {
  17. vtype Type
  18. numeric uint64
  19. stringly string
  20. slice interface{}
  21. }
  22. const (
  23. // INVALID is used for a Value with no value set.
  24. INVALID Type = iota
  25. // BOOL is a boolean Type Value.
  26. BOOL
  27. // INT64 is a 64-bit signed integral Type Value.
  28. INT64
  29. // FLOAT64 is a 64-bit floating point Type Value.
  30. FLOAT64
  31. // STRING is a string Type Value.
  32. STRING
  33. // BOOLSLICE is a slice of booleans Type Value.
  34. BOOLSLICE
  35. // INT64SLICE is a slice of 64-bit signed integral numbers Type Value.
  36. INT64SLICE
  37. // FLOAT64SLICE is a slice of 64-bit floating point numbers Type Value.
  38. FLOAT64SLICE
  39. // STRINGSLICE is a slice of strings Type Value.
  40. STRINGSLICE
  41. )
  42. // BoolValue creates a BOOL Value.
  43. func BoolValue(v bool) Value {
  44. return Value{
  45. vtype: BOOL,
  46. numeric: internal.BoolToRaw(v),
  47. }
  48. }
  49. // BoolSliceValue creates a BOOLSLICE Value.
  50. func BoolSliceValue(v []bool) Value {
  51. return Value{vtype: BOOLSLICE, slice: attribute.BoolSliceValue(v)}
  52. }
  53. // IntValue creates an INT64 Value.
  54. func IntValue(v int) Value {
  55. return Int64Value(int64(v))
  56. }
  57. // IntSliceValue creates an INTSLICE Value.
  58. func IntSliceValue(v []int) Value {
  59. var int64Val int64
  60. cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val)))
  61. for i, val := range v {
  62. cp.Elem().Index(i).SetInt(int64(val))
  63. }
  64. return Value{
  65. vtype: INT64SLICE,
  66. slice: cp.Elem().Interface(),
  67. }
  68. }
  69. // Int64Value creates an INT64 Value.
  70. func Int64Value(v int64) Value {
  71. return Value{
  72. vtype: INT64,
  73. numeric: internal.Int64ToRaw(v),
  74. }
  75. }
  76. // Int64SliceValue creates an INT64SLICE Value.
  77. func Int64SliceValue(v []int64) Value {
  78. return Value{vtype: INT64SLICE, slice: attribute.Int64SliceValue(v)}
  79. }
  80. // Float64Value creates a FLOAT64 Value.
  81. func Float64Value(v float64) Value {
  82. return Value{
  83. vtype: FLOAT64,
  84. numeric: internal.Float64ToRaw(v),
  85. }
  86. }
  87. // Float64SliceValue creates a FLOAT64SLICE Value.
  88. func Float64SliceValue(v []float64) Value {
  89. return Value{vtype: FLOAT64SLICE, slice: attribute.Float64SliceValue(v)}
  90. }
  91. // StringValue creates a STRING Value.
  92. func StringValue(v string) Value {
  93. return Value{
  94. vtype: STRING,
  95. stringly: v,
  96. }
  97. }
  98. // StringSliceValue creates a STRINGSLICE Value.
  99. func StringSliceValue(v []string) Value {
  100. return Value{vtype: STRINGSLICE, slice: attribute.StringSliceValue(v)}
  101. }
  102. // Type returns a type of the Value.
  103. func (v Value) Type() Type {
  104. return v.vtype
  105. }
  106. // AsBool returns the bool value. Make sure that the Value's type is
  107. // BOOL.
  108. func (v Value) AsBool() bool {
  109. return internal.RawToBool(v.numeric)
  110. }
  111. // AsBoolSlice returns the []bool value. Make sure that the Value's type is
  112. // BOOLSLICE.
  113. func (v Value) AsBoolSlice() []bool {
  114. if v.vtype != BOOLSLICE {
  115. return nil
  116. }
  117. return v.asBoolSlice()
  118. }
  119. func (v Value) asBoolSlice() []bool {
  120. return attribute.AsBoolSlice(v.slice)
  121. }
  122. // AsInt64 returns the int64 value. Make sure that the Value's type is
  123. // INT64.
  124. func (v Value) AsInt64() int64 {
  125. return internal.RawToInt64(v.numeric)
  126. }
  127. // AsInt64Slice returns the []int64 value. Make sure that the Value's type is
  128. // INT64SLICE.
  129. func (v Value) AsInt64Slice() []int64 {
  130. if v.vtype != INT64SLICE {
  131. return nil
  132. }
  133. return v.asInt64Slice()
  134. }
  135. func (v Value) asInt64Slice() []int64 {
  136. return attribute.AsInt64Slice(v.slice)
  137. }
  138. // AsFloat64 returns the float64 value. Make sure that the Value's
  139. // type is FLOAT64.
  140. func (v Value) AsFloat64() float64 {
  141. return internal.RawToFloat64(v.numeric)
  142. }
  143. // AsFloat64Slice returns the []float64 value. Make sure that the Value's type is
  144. // FLOAT64SLICE.
  145. func (v Value) AsFloat64Slice() []float64 {
  146. if v.vtype != FLOAT64SLICE {
  147. return nil
  148. }
  149. return v.asFloat64Slice()
  150. }
  151. func (v Value) asFloat64Slice() []float64 {
  152. return attribute.AsFloat64Slice(v.slice)
  153. }
  154. // AsString returns the string value. Make sure that the Value's type
  155. // is STRING.
  156. func (v Value) AsString() string {
  157. return v.stringly
  158. }
  159. // AsStringSlice returns the []string value. Make sure that the Value's type is
  160. // STRINGSLICE.
  161. func (v Value) AsStringSlice() []string {
  162. if v.vtype != STRINGSLICE {
  163. return nil
  164. }
  165. return v.asStringSlice()
  166. }
  167. func (v Value) asStringSlice() []string {
  168. return attribute.AsStringSlice(v.slice)
  169. }
  170. type unknownValueType struct{}
  171. // AsInterface returns Value's data as interface{}.
  172. func (v Value) AsInterface() interface{} {
  173. switch v.Type() {
  174. case BOOL:
  175. return v.AsBool()
  176. case BOOLSLICE:
  177. return v.asBoolSlice()
  178. case INT64:
  179. return v.AsInt64()
  180. case INT64SLICE:
  181. return v.asInt64Slice()
  182. case FLOAT64:
  183. return v.AsFloat64()
  184. case FLOAT64SLICE:
  185. return v.asFloat64Slice()
  186. case STRING:
  187. return v.stringly
  188. case STRINGSLICE:
  189. return v.asStringSlice()
  190. }
  191. return unknownValueType{}
  192. }
  193. // Emit returns a string representation of Value's data.
  194. func (v Value) Emit() string {
  195. switch v.Type() {
  196. case BOOLSLICE:
  197. return fmt.Sprint(v.asBoolSlice())
  198. case BOOL:
  199. return strconv.FormatBool(v.AsBool())
  200. case INT64SLICE:
  201. j, err := json.Marshal(v.asInt64Slice())
  202. if err != nil {
  203. return fmt.Sprintf("invalid: %v", v.asInt64Slice())
  204. }
  205. return string(j)
  206. case INT64:
  207. return strconv.FormatInt(v.AsInt64(), 10)
  208. case FLOAT64SLICE:
  209. j, err := json.Marshal(v.asFloat64Slice())
  210. if err != nil {
  211. return fmt.Sprintf("invalid: %v", v.asFloat64Slice())
  212. }
  213. return string(j)
  214. case FLOAT64:
  215. return fmt.Sprint(v.AsFloat64())
  216. case STRINGSLICE:
  217. j, err := json.Marshal(v.asStringSlice())
  218. if err != nil {
  219. return fmt.Sprintf("invalid: %v", v.asStringSlice())
  220. }
  221. return string(j)
  222. case STRING:
  223. return v.stringly
  224. default:
  225. return "unknown"
  226. }
  227. }
  228. // MarshalJSON returns the JSON encoding of the Value.
  229. func (v Value) MarshalJSON() ([]byte, error) {
  230. var jsonVal struct {
  231. Type string
  232. Value interface{}
  233. }
  234. jsonVal.Type = v.Type().String()
  235. jsonVal.Value = v.AsInterface()
  236. return json.Marshal(jsonVal)
  237. }