kv.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package attribute // import "go.opentelemetry.io/otel/attribute"
  4. import (
  5. "fmt"
  6. )
  7. // KeyValue holds a key and value pair.
  8. type KeyValue struct {
  9. Key Key
  10. Value Value
  11. }
  12. // Valid returns if kv is a valid OpenTelemetry attribute.
  13. func (kv KeyValue) Valid() bool {
  14. return kv.Key.Defined() && kv.Value.Type() != INVALID
  15. }
  16. // Bool creates a KeyValue with a BOOL Value type.
  17. func Bool(k string, v bool) KeyValue {
  18. return Key(k).Bool(v)
  19. }
  20. // BoolSlice creates a KeyValue with a BOOLSLICE Value type.
  21. func BoolSlice(k string, v []bool) KeyValue {
  22. return Key(k).BoolSlice(v)
  23. }
  24. // Int creates a KeyValue with an INT64 Value type.
  25. func Int(k string, v int) KeyValue {
  26. return Key(k).Int(v)
  27. }
  28. // IntSlice creates a KeyValue with an INT64SLICE Value type.
  29. func IntSlice(k string, v []int) KeyValue {
  30. return Key(k).IntSlice(v)
  31. }
  32. // Int64 creates a KeyValue with an INT64 Value type.
  33. func Int64(k string, v int64) KeyValue {
  34. return Key(k).Int64(v)
  35. }
  36. // Int64Slice creates a KeyValue with an INT64SLICE Value type.
  37. func Int64Slice(k string, v []int64) KeyValue {
  38. return Key(k).Int64Slice(v)
  39. }
  40. // Float64 creates a KeyValue with a FLOAT64 Value type.
  41. func Float64(k string, v float64) KeyValue {
  42. return Key(k).Float64(v)
  43. }
  44. // Float64Slice creates a KeyValue with a FLOAT64SLICE Value type.
  45. func Float64Slice(k string, v []float64) KeyValue {
  46. return Key(k).Float64Slice(v)
  47. }
  48. // String creates a KeyValue with a STRING Value type.
  49. func String(k, v string) KeyValue {
  50. return Key(k).String(v)
  51. }
  52. // StringSlice creates a KeyValue with a STRINGSLICE Value type.
  53. func StringSlice(k string, v []string) KeyValue {
  54. return Key(k).StringSlice(v)
  55. }
  56. // Stringer creates a new key-value pair with a passed name and a string
  57. // value generated by the passed Stringer interface.
  58. func Stringer(k string, v fmt.Stringer) KeyValue {
  59. return Key(k).String(v.String())
  60. }