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