kv.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "fmt"
  17. )
  18. // KeyValue holds a key and value pair.
  19. type KeyValue struct {
  20. Key Key
  21. Value Value
  22. }
  23. // Valid returns if kv is a valid OpenTelemetry attribute.
  24. func (kv KeyValue) Valid() bool {
  25. return kv.Key.Defined() && kv.Value.Type() != INVALID
  26. }
  27. // Bool creates a KeyValue with a BOOL Value type.
  28. func Bool(k string, v bool) KeyValue {
  29. return Key(k).Bool(v)
  30. }
  31. // BoolSlice creates a KeyValue with a BOOLSLICE Value type.
  32. func BoolSlice(k string, v []bool) KeyValue {
  33. return Key(k).BoolSlice(v)
  34. }
  35. // Int creates a KeyValue with an INT64 Value type.
  36. func Int(k string, v int) KeyValue {
  37. return Key(k).Int(v)
  38. }
  39. // IntSlice creates a KeyValue with an INT64SLICE Value type.
  40. func IntSlice(k string, v []int) KeyValue {
  41. return Key(k).IntSlice(v)
  42. }
  43. // Int64 creates a KeyValue with an INT64 Value type.
  44. func Int64(k string, v int64) KeyValue {
  45. return Key(k).Int64(v)
  46. }
  47. // Int64Slice creates a KeyValue with an INT64SLICE Value type.
  48. func Int64Slice(k string, v []int64) KeyValue {
  49. return Key(k).Int64Slice(v)
  50. }
  51. // Float64 creates a KeyValue with a FLOAT64 Value type.
  52. func Float64(k string, v float64) KeyValue {
  53. return Key(k).Float64(v)
  54. }
  55. // Float64Slice creates a KeyValue with a FLOAT64SLICE Value type.
  56. func Float64Slice(k string, v []float64) KeyValue {
  57. return Key(k).Float64Slice(v)
  58. }
  59. // String creates a KeyValue with a STRING Value type.
  60. func String(k, v string) KeyValue {
  61. return Key(k).String(v)
  62. }
  63. // StringSlice creates a KeyValue with a STRINGSLICE Value type.
  64. func StringSlice(k string, v []string) KeyValue {
  65. return Key(k).StringSlice(v)
  66. }
  67. // Stringer creates a new key-value pair with a passed name and a string
  68. // value generated by the passed Stringer interface.
  69. func Stringer(k string, v fmt.Stringer) KeyValue {
  70. return Key(k).String(v.String())
  71. }