key.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Key represents the key part in key-value pairs. It's a string. The
  16. // allowed character set in the key depends on the use of the key.
  17. type Key string
  18. // Bool creates a KeyValue instance with a BOOL Value.
  19. //
  20. // If creating both a key and value at the same time, use the provided
  21. // convenience function instead -- Bool(name, value).
  22. func (k Key) Bool(v bool) KeyValue {
  23. return KeyValue{
  24. Key: k,
  25. Value: BoolValue(v),
  26. }
  27. }
  28. // BoolSlice creates a KeyValue instance with a BOOLSLICE Value.
  29. //
  30. // If creating both a key and value at the same time, use the provided
  31. // convenience function instead -- BoolSlice(name, value).
  32. func (k Key) BoolSlice(v []bool) KeyValue {
  33. return KeyValue{
  34. Key: k,
  35. Value: BoolSliceValue(v),
  36. }
  37. }
  38. // Int creates a KeyValue instance with an INT64 Value.
  39. //
  40. // If creating both a key and value at the same time, use the provided
  41. // convenience function instead -- Int(name, value).
  42. func (k Key) Int(v int) KeyValue {
  43. return KeyValue{
  44. Key: k,
  45. Value: IntValue(v),
  46. }
  47. }
  48. // IntSlice creates a KeyValue instance with an INT64SLICE Value.
  49. //
  50. // If creating both a key and value at the same time, use the provided
  51. // convenience function instead -- IntSlice(name, value).
  52. func (k Key) IntSlice(v []int) KeyValue {
  53. return KeyValue{
  54. Key: k,
  55. Value: IntSliceValue(v),
  56. }
  57. }
  58. // Int64 creates a KeyValue instance with an INT64 Value.
  59. //
  60. // If creating both a key and value at the same time, use the provided
  61. // convenience function instead -- Int64(name, value).
  62. func (k Key) Int64(v int64) KeyValue {
  63. return KeyValue{
  64. Key: k,
  65. Value: Int64Value(v),
  66. }
  67. }
  68. // Int64Slice creates a KeyValue instance with an INT64SLICE Value.
  69. //
  70. // If creating both a key and value at the same time, use the provided
  71. // convenience function instead -- Int64Slice(name, value).
  72. func (k Key) Int64Slice(v []int64) KeyValue {
  73. return KeyValue{
  74. Key: k,
  75. Value: Int64SliceValue(v),
  76. }
  77. }
  78. // Float64 creates a KeyValue instance with a FLOAT64 Value.
  79. //
  80. // If creating both a key and value at the same time, use the provided
  81. // convenience function instead -- Float64(name, value).
  82. func (k Key) Float64(v float64) KeyValue {
  83. return KeyValue{
  84. Key: k,
  85. Value: Float64Value(v),
  86. }
  87. }
  88. // Float64Slice creates a KeyValue instance with a FLOAT64SLICE Value.
  89. //
  90. // If creating both a key and value at the same time, use the provided
  91. // convenience function instead -- Float64(name, value).
  92. func (k Key) Float64Slice(v []float64) KeyValue {
  93. return KeyValue{
  94. Key: k,
  95. Value: Float64SliceValue(v),
  96. }
  97. }
  98. // String creates a KeyValue instance with a STRING Value.
  99. //
  100. // If creating both a key and value at the same time, use the provided
  101. // convenience function instead -- String(name, value).
  102. func (k Key) String(v string) KeyValue {
  103. return KeyValue{
  104. Key: k,
  105. Value: StringValue(v),
  106. }
  107. }
  108. // StringSlice creates a KeyValue instance with a STRINGSLICE Value.
  109. //
  110. // If creating both a key and value at the same time, use the provided
  111. // convenience function instead -- StringSlice(name, value).
  112. func (k Key) StringSlice(v []string) KeyValue {
  113. return KeyValue{
  114. Key: k,
  115. Value: StringSliceValue(v),
  116. }
  117. }
  118. // Defined returns true for non-empty keys.
  119. func (k Key) Defined() bool {
  120. return len(k) != 0
  121. }