blob: 3e2764f473579571874c1fddc8e944142b8fa3e9 [file] [log] [blame]
Joey Armstrong903c69d2024-02-01 19:46:39 -05001// 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
15package label
16
17import (
18 "encoding/json"
19 "fmt"
20 "reflect"
21)
22
23// KeyValue holds a key and value pair.
24type KeyValue struct {
25 Key Key
26 Value Value
27}
28
29// Bool creates a new key-value pair with a passed name and a bool
30// value.
31func Bool(k string, v bool) KeyValue {
32 return Key(k).Bool(v)
33}
34
35// Int64 creates a new key-value pair with a passed name and an int64
36// value.
37func Int64(k string, v int64) KeyValue {
38 return Key(k).Int64(v)
39}
40
41// Uint64 creates a new key-value pair with a passed name and a uint64
42// value.
43func Uint64(k string, v uint64) KeyValue {
44 return Key(k).Uint64(v)
45}
46
47// Float64 creates a new key-value pair with a passed name and a float64
48// value.
49func Float64(k string, v float64) KeyValue {
50 return Key(k).Float64(v)
51}
52
53// Int32 creates a new key-value pair with a passed name and an int32
54// value.
55func Int32(k string, v int32) KeyValue {
56 return Key(k).Int32(v)
57}
58
59// Uint32 creates a new key-value pair with a passed name and a uint32
60// value.
61func Uint32(k string, v uint32) KeyValue {
62 return Key(k).Uint32(v)
63}
64
65// Float32 creates a new key-value pair with a passed name and a float32
66// value.
67func Float32(k string, v float32) KeyValue {
68 return Key(k).Float32(v)
69}
70
71// String creates a new key-value pair with a passed name and a string
72// value.
73func String(k, v string) KeyValue {
74 return Key(k).String(v)
75}
76
77// Stringer creates a new key-value pair with a passed name and a string
78// value generated by the passed Stringer interface.
79func Stringer(k string, v fmt.Stringer) KeyValue {
80 return Key(k).String(v.String())
81}
82
83// Int creates a new key-value pair instance with a passed name and
84// either an int32 or an int64 value, depending on whether the int
85// type is 32 or 64 bits wide.
86func Int(k string, v int) KeyValue {
87 return Key(k).Int(v)
88}
89
90// Uint creates a new key-value pair instance with a passed name and
91// either an uint32 or an uint64 value, depending on whether the uint
92// type is 32 or 64 bits wide.
93func Uint(k string, v uint) KeyValue {
94 return Key(k).Uint(v)
95}
96
97// Array creates a new key-value pair with a passed name and a array.
98// Only arrays of primitive type are supported.
99func Array(k string, v interface{}) KeyValue {
100 return Key(k).Array(v)
101}
102
103// Any creates a new key-value pair instance with a passed name and
104// automatic type inference. This is slower, and not type-safe.
105func Any(k string, value interface{}) KeyValue {
106 if value == nil {
107 return String(k, "<nil>")
108 }
109
110 if stringer, ok := value.(fmt.Stringer); ok {
111 return String(k, stringer.String())
112 }
113
114 rv := reflect.ValueOf(value)
115
116 switch rv.Kind() {
117 case reflect.Array, reflect.Slice:
118 return Array(k, value)
119 case reflect.Bool:
120 return Bool(k, rv.Bool())
121 case reflect.Int, reflect.Int8, reflect.Int16:
122 return Int(k, int(rv.Int()))
123 case reflect.Int32:
124 return Int32(k, int32(rv.Int()))
125 case reflect.Int64:
126 return Int64(k, int64(rv.Int()))
127 case reflect.Uint, reflect.Uint8, reflect.Uint16:
128 return Uint(k, uint(rv.Uint()))
129 case reflect.Uint32:
130 return Uint32(k, uint32(rv.Uint()))
131 case reflect.Uint64, reflect.Uintptr:
132 return Uint64(k, rv.Uint())
133 case reflect.Float32:
134 return Float32(k, float32(rv.Float()))
135 case reflect.Float64:
136 return Float64(k, rv.Float())
137 case reflect.String:
138 return String(k, rv.String())
139 }
140 if b, err := json.Marshal(value); value != nil && err == nil {
141 return String(k, string(b))
142 }
143 return String(k, fmt.Sprint(value))
144}