key.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 key and a bool value at the same time, then
  21. // instead of calling Key(name).Bool(value) consider using a
  22. // convenience function provided by the api/key package -
  23. // key.Bool(name, value).
  24. func (k Key) Bool(v bool) KeyValue {
  25. return KeyValue{
  26. Key: k,
  27. Value: BoolValue(v),
  28. }
  29. }
  30. // Int64 creates a KeyValue instance with an INT64 Value.
  31. //
  32. // If creating both key and an int64 value at the same time, then
  33. // instead of calling Key(name).Int64(value) consider using a
  34. // convenience function provided by the api/key package -
  35. // key.Int64(name, value).
  36. func (k Key) Int64(v int64) KeyValue {
  37. return KeyValue{
  38. Key: k,
  39. Value: Int64Value(v),
  40. }
  41. }
  42. // Float64 creates a KeyValue instance with a FLOAT64 Value.
  43. //
  44. // If creating both key and a float64 value at the same time, then
  45. // instead of calling Key(name).Float64(value) consider using a
  46. // convenience function provided by the api/key package -
  47. // key.Float64(name, value).
  48. func (k Key) Float64(v float64) KeyValue {
  49. return KeyValue{
  50. Key: k,
  51. Value: Float64Value(v),
  52. }
  53. }
  54. // String creates a KeyValue instance with a STRING Value.
  55. //
  56. // If creating both key and a string value at the same time, then
  57. // instead of calling Key(name).String(value) consider using a
  58. // convenience function provided by the api/key package -
  59. // key.String(name, value).
  60. func (k Key) String(v string) KeyValue {
  61. return KeyValue{
  62. Key: k,
  63. Value: StringValue(v),
  64. }
  65. }
  66. // Int creates a KeyValue instance with an INT64 Value.
  67. //
  68. // If creating both key and an int value at the same time, then
  69. // instead of calling Key(name).Int(value) consider using a
  70. // convenience function provided by the api/key package -
  71. // key.Int(name, value).
  72. func (k Key) Int(v int) KeyValue {
  73. return KeyValue{
  74. Key: k,
  75. Value: IntValue(v),
  76. }
  77. }
  78. // Defined returns true for non-empty keys.
  79. func (k Key) Defined() bool {
  80. return len(k) != 0
  81. }
  82. // Array creates a KeyValue instance with a ARRAY Value.
  83. //
  84. // If creating both key and a array value at the same time, then
  85. // instead of calling Key(name).String(value) consider using a
  86. // convenience function provided by the api/key package -
  87. // key.Array(name, value).
  88. func (k Key) Array(v interface{}) KeyValue {
  89. return KeyValue{
  90. Key: k,
  91. Value: ArrayValue(v),
  92. }
  93. }