kv.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. )
  20. // KeyValue holds a key and value pair.
  21. type KeyValue struct {
  22. Key Key
  23. Value Value
  24. }
  25. // Valid returns if kv is a valid OpenTelemetry attribute.
  26. func (kv KeyValue) Valid() bool {
  27. return kv.Key != "" && kv.Value.Type() != INVALID
  28. }
  29. // Bool creates a new key-value pair with a passed name and a bool
  30. // value.
  31. func Bool(k string, v bool) KeyValue {
  32. return Key(k).Bool(v)
  33. }
  34. // Int64 creates a new key-value pair with a passed name and an int64
  35. // value.
  36. func Int64(k string, v int64) KeyValue {
  37. return Key(k).Int64(v)
  38. }
  39. // Float64 creates a new key-value pair with a passed name and a float64
  40. // value.
  41. func Float64(k string, v float64) KeyValue {
  42. return Key(k).Float64(v)
  43. }
  44. // String creates a new key-value pair with a passed name and a string
  45. // value.
  46. func String(k, v string) KeyValue {
  47. return Key(k).String(v)
  48. }
  49. // Stringer creates a new key-value pair with a passed name and a string
  50. // value generated by the passed Stringer interface.
  51. func Stringer(k string, v fmt.Stringer) KeyValue {
  52. return Key(k).String(v.String())
  53. }
  54. // Int creates a new key-value pair instance with a passed name and
  55. // either an int32 or an int64 value, depending on whether the int
  56. // type is 32 or 64 bits wide.
  57. func Int(k string, v int) KeyValue {
  58. return Key(k).Int(v)
  59. }
  60. // Array creates a new key-value pair with a passed name and a array.
  61. // Only arrays of primitive type are supported.
  62. func Array(k string, v interface{}) KeyValue {
  63. return Key(k).Array(v)
  64. }
  65. // Any creates a new key-value pair instance with a passed name and
  66. // automatic type inference. This is slower, and not type-safe.
  67. func Any(k string, value interface{}) KeyValue {
  68. if value == nil {
  69. return String(k, "<nil>")
  70. }
  71. if stringer, ok := value.(fmt.Stringer); ok {
  72. return String(k, stringer.String())
  73. }
  74. rv := reflect.ValueOf(value)
  75. switch rv.Kind() {
  76. case reflect.Array, reflect.Slice:
  77. return Array(k, value)
  78. case reflect.Bool:
  79. return Bool(k, rv.Bool())
  80. case reflect.Int, reflect.Int8, reflect.Int16:
  81. return Int(k, int(rv.Int()))
  82. case reflect.Int64:
  83. return Int64(k, rv.Int())
  84. case reflect.Float64:
  85. return Float64(k, rv.Float())
  86. case reflect.String:
  87. return String(k, rv.String())
  88. }
  89. if b, err := json.Marshal(value); b != nil && err == nil {
  90. return String(k, string(b))
  91. }
  92. return String(k, fmt.Sprint(value))
  93. }