value.go 6.4 KB

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