key.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package attribute // import "go.opentelemetry.io/otel/attribute"
  4. // Key represents the key part in key-value pairs. It's a string. The
  5. // allowed character set in the key depends on the use of the key.
  6. type Key string
  7. // Bool creates a KeyValue instance with a BOOL Value.
  8. //
  9. // If creating both a key and value at the same time, use the provided
  10. // convenience function instead -- Bool(name, value).
  11. func (k Key) Bool(v bool) KeyValue {
  12. return KeyValue{
  13. Key: k,
  14. Value: BoolValue(v),
  15. }
  16. }
  17. // BoolSlice creates a KeyValue instance with a BOOLSLICE Value.
  18. //
  19. // If creating both a key and value at the same time, use the provided
  20. // convenience function instead -- BoolSlice(name, value).
  21. func (k Key) BoolSlice(v []bool) KeyValue {
  22. return KeyValue{
  23. Key: k,
  24. Value: BoolSliceValue(v),
  25. }
  26. }
  27. // Int creates a KeyValue instance with an INT64 Value.
  28. //
  29. // If creating both a key and value at the same time, use the provided
  30. // convenience function instead -- Int(name, value).
  31. func (k Key) Int(v int) KeyValue {
  32. return KeyValue{
  33. Key: k,
  34. Value: IntValue(v),
  35. }
  36. }
  37. // IntSlice creates a KeyValue instance with an INT64SLICE Value.
  38. //
  39. // If creating both a key and value at the same time, use the provided
  40. // convenience function instead -- IntSlice(name, value).
  41. func (k Key) IntSlice(v []int) KeyValue {
  42. return KeyValue{
  43. Key: k,
  44. Value: IntSliceValue(v),
  45. }
  46. }
  47. // Int64 creates a KeyValue instance with an INT64 Value.
  48. //
  49. // If creating both a key and value at the same time, use the provided
  50. // convenience function instead -- Int64(name, value).
  51. func (k Key) Int64(v int64) KeyValue {
  52. return KeyValue{
  53. Key: k,
  54. Value: Int64Value(v),
  55. }
  56. }
  57. // Int64Slice creates a KeyValue instance with an INT64SLICE Value.
  58. //
  59. // If creating both a key and value at the same time, use the provided
  60. // convenience function instead -- Int64Slice(name, value).
  61. func (k Key) Int64Slice(v []int64) KeyValue {
  62. return KeyValue{
  63. Key: k,
  64. Value: Int64SliceValue(v),
  65. }
  66. }
  67. // Float64 creates a KeyValue instance with a FLOAT64 Value.
  68. //
  69. // If creating both a key and value at the same time, use the provided
  70. // convenience function instead -- Float64(name, value).
  71. func (k Key) Float64(v float64) KeyValue {
  72. return KeyValue{
  73. Key: k,
  74. Value: Float64Value(v),
  75. }
  76. }
  77. // Float64Slice creates a KeyValue instance with a FLOAT64SLICE Value.
  78. //
  79. // If creating both a key and value at the same time, use the provided
  80. // convenience function instead -- Float64(name, value).
  81. func (k Key) Float64Slice(v []float64) KeyValue {
  82. return KeyValue{
  83. Key: k,
  84. Value: Float64SliceValue(v),
  85. }
  86. }
  87. // String creates a KeyValue instance with a STRING Value.
  88. //
  89. // If creating both a key and value at the same time, use the provided
  90. // convenience function instead -- String(name, value).
  91. func (k Key) String(v string) KeyValue {
  92. return KeyValue{
  93. Key: k,
  94. Value: StringValue(v),
  95. }
  96. }
  97. // StringSlice creates a KeyValue instance with a STRINGSLICE Value.
  98. //
  99. // If creating both a key and value at the same time, use the provided
  100. // convenience function instead -- StringSlice(name, value).
  101. func (k Key) StringSlice(v []string) KeyValue {
  102. return KeyValue{
  103. Key: k,
  104. Value: StringSliceValue(v),
  105. }
  106. }
  107. // Defined returns true for non-empty keys.
  108. func (k Key) Defined() bool {
  109. return len(k) != 0
  110. }