value.go 5.9 KB

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