kv.go (2458B)
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 import ( 18 "fmt" 19 ) 20 21 // KeyValue holds a key and value pair. 22 type KeyValue struct { 23 Key Key 24 Value Value 25 } 26 27 // Valid returns if kv is a valid OpenTelemetry attribute. 28 func (kv KeyValue) Valid() bool { 29 return kv.Key.Defined() && kv.Value.Type() != INVALID 30 } 31 32 // Bool creates a KeyValue with a BOOL Value type. 33 func Bool(k string, v bool) KeyValue { 34 return Key(k).Bool(v) 35 } 36 37 // BoolSlice creates a KeyValue with a BOOLSLICE Value type. 38 func BoolSlice(k string, v []bool) KeyValue { 39 return Key(k).BoolSlice(v) 40 } 41 42 // Int creates a KeyValue with an INT64 Value type. 43 func Int(k string, v int) KeyValue { 44 return Key(k).Int(v) 45 } 46 47 // IntSlice creates a KeyValue with an INT64SLICE Value type. 48 func IntSlice(k string, v []int) KeyValue { 49 return Key(k).IntSlice(v) 50 } 51 52 // Int64 creates a KeyValue with an INT64 Value type. 53 func Int64(k string, v int64) KeyValue { 54 return Key(k).Int64(v) 55 } 56 57 // Int64Slice creates a KeyValue with an INT64SLICE Value type. 58 func Int64Slice(k string, v []int64) KeyValue { 59 return Key(k).Int64Slice(v) 60 } 61 62 // Float64 creates a KeyValue with a FLOAT64 Value type. 63 func Float64(k string, v float64) KeyValue { 64 return Key(k).Float64(v) 65 } 66 67 // Float64Slice creates a KeyValue with a FLOAT64SLICE Value type. 68 func Float64Slice(k string, v []float64) KeyValue { 69 return Key(k).Float64Slice(v) 70 } 71 72 // String creates a KeyValue with a STRING Value type. 73 func String(k, v string) KeyValue { 74 return Key(k).String(v) 75 } 76 77 // StringSlice creates a KeyValue with a STRINGSLICE Value type. 78 func StringSlice(k string, v []string) KeyValue { 79 return Key(k).StringSlice(v) 80 } 81 82 // Stringer creates a new key-value pair with a passed name and a string 83 // value generated by the passed Stringer interface. 84 func Stringer(k string, v fmt.Stringer) KeyValue { 85 return Key(k).String(v.String()) 86 }