kv.go raw

   1  // Copyright The OpenTelemetry Authors
   2  // SPDX-License-Identifier: Apache-2.0
   3  
   4  package attribute // import "go.opentelemetry.io/otel/attribute"
   5  
   6  import (
   7  	"fmt"
   8  )
   9  
  10  // KeyValue holds a key and value pair.
  11  type KeyValue struct {
  12  	Key   Key
  13  	Value Value
  14  }
  15  
  16  // Valid reports whether kv is a valid OpenTelemetry attribute.
  17  func (kv KeyValue) Valid() bool {
  18  	return kv.Key.Defined() && kv.Value.Type() != INVALID
  19  }
  20  
  21  // Bool creates a KeyValue with a BOOL Value type.
  22  func Bool(k string, v bool) KeyValue {
  23  	return Key(k).Bool(v)
  24  }
  25  
  26  // BoolSlice creates a KeyValue with a BOOLSLICE Value type.
  27  func BoolSlice(k string, v []bool) KeyValue {
  28  	return Key(k).BoolSlice(v)
  29  }
  30  
  31  // Int creates a KeyValue with an INT64 Value type.
  32  func Int(k string, v int) KeyValue {
  33  	return Key(k).Int(v)
  34  }
  35  
  36  // IntSlice creates a KeyValue with an INT64SLICE Value type.
  37  func IntSlice(k string, v []int) KeyValue {
  38  	return Key(k).IntSlice(v)
  39  }
  40  
  41  // Int64 creates a KeyValue with an INT64 Value type.
  42  func Int64(k string, v int64) KeyValue {
  43  	return Key(k).Int64(v)
  44  }
  45  
  46  // Int64Slice creates a KeyValue with an INT64SLICE Value type.
  47  func Int64Slice(k string, v []int64) KeyValue {
  48  	return Key(k).Int64Slice(v)
  49  }
  50  
  51  // Float64 creates a KeyValue with a FLOAT64 Value type.
  52  func Float64(k string, v float64) KeyValue {
  53  	return Key(k).Float64(v)
  54  }
  55  
  56  // Float64Slice creates a KeyValue with a FLOAT64SLICE Value type.
  57  func Float64Slice(k string, v []float64) KeyValue {
  58  	return Key(k).Float64Slice(v)
  59  }
  60  
  61  // String creates a KeyValue with a STRING Value type.
  62  func String(k, v string) KeyValue {
  63  	return Key(k).String(v)
  64  }
  65  
  66  // StringSlice creates a KeyValue with a STRINGSLICE Value type.
  67  func StringSlice(k string, v []string) KeyValue {
  68  	return Key(k).StringSlice(v)
  69  }
  70  
  71  // Stringer creates a new key-value pair with a passed name and a string
  72  // value generated by the passed Stringer interface.
  73  func Stringer(k string, v fmt.Stringer) KeyValue {
  74  	return Key(k).String(v.String())
  75  }
  76