| 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 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
"strconv" |
| 10 |
|
| 11 |
attribute "go.opentelemetry.io/otel/attribute/internal" |
| 12 |
) |
| 13 |
|
| 14 |
//go:generate stringer -type=Type |
| 15 |
|
| 16 |
// Type describes the type of the data Value holds. |
| 17 |
type Type int // nolint: revive // redefines builtin Type. |
| 18 |
|
| 19 |
// Value represents the value part in key-value pairs. |
| 20 |
// |
| 21 |
// Note that the zero value is a valid empty value. |
| 22 |
type Value struct { |
| 23 |
vtype Type |
| 24 |
numeric uint64 |
| 25 |
stringly string |
| 26 |
slice any |
| 27 |
} |
| 28 |
|
| 29 |
const ( |
| 30 |
// EMPTY is used for a Value with no value set. |
| 31 |
EMPTY Type = iota |
| 32 |
// BOOL is a boolean Type Value. |
| 33 |
BOOL |
| 34 |
// INT64 is a 64-bit signed integral Type Value. |
| 35 |
INT64 |
| 36 |
// FLOAT64 is a 64-bit floating point Type Value. |
| 37 |
FLOAT64 |
| 38 |
// STRING is a string Type Value. |
| 39 |
STRING |
| 40 |
// BOOLSLICE is a slice of booleans Type Value. |
| 41 |
BOOLSLICE |
| 42 |
// INT64SLICE is a slice of 64-bit signed integral numbers Type Value. |
| 43 |
INT64SLICE |
| 44 |
// FLOAT64SLICE is a slice of 64-bit floating point numbers Type Value. |
| 45 |
FLOAT64SLICE |
| 46 |
// STRINGSLICE is a slice of strings Type Value. |
| 47 |
STRINGSLICE |
| 48 |
// INVALID is used for a Value with no value set. |
| 49 |
// |
| 50 |
// Deprecated: Use EMPTY instead as an empty value is a valid value. |
| 51 |
INVALID = EMPTY |
| 52 |
) |
| 53 |
|
| 54 |
// BoolValue creates a BOOL Value. |
| 55 |
func BoolValue(v bool) Value { |
| 56 |
return Value{ |
| 57 |
vtype: BOOL, |
| 58 |
numeric: boolToRaw(v), |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// BoolSliceValue creates a BOOLSLICE Value. |
| 63 |
func BoolSliceValue(v []bool) Value { |
| 64 |
return Value{vtype: BOOLSLICE, slice: attribute.SliceValue(v)} |
| 65 |
} |
| 66 |
|
| 67 |
// IntValue creates an INT64 Value. |
| 68 |
func IntValue(v int) Value { |
| 69 |
return Int64Value(int64(v)) |
| 70 |
} |
| 71 |
|
| 72 |
// IntSliceValue creates an INT64SLICE Value. |
| 73 |
func IntSliceValue(v []int) Value { |
| 74 |
val := Value{vtype: INT64SLICE} |
| 75 |
|
| 76 |
// Avoid the common tiny-slice cases from allocating a new slice. |
| 77 |
switch len(v) { |
| 78 |
case 0: |
| 79 |
val.slice = [0]int64{} |
| 80 |
case 1: |
| 81 |
val.slice = [1]int64{int64(v[0])} |
| 82 |
case 2: |
| 83 |
val.slice = [2]int64{int64(v[0]), int64(v[1])} |
| 84 |
case 3: |
| 85 |
val.slice = [3]int64{int64(v[0]), int64(v[1]), int64(v[2])} |
| 86 |
default: |
| 87 |
// Fallback to a new slice for larger slices. |
| 88 |
cp := make([]int64, len(v)) |
| 89 |
for i, val := range v { |
| 90 |
cp[i] = int64(val) |
| 91 |
} |
| 92 |
val.slice = attribute.SliceValue(cp) |
| 93 |
} |
| 94 |
|
| 95 |
return val |
| 96 |
} |
| 97 |
|
| 98 |
// Int64Value creates an INT64 Value. |
| 99 |
func Int64Value(v int64) Value { |
| 100 |
return Value{ |
| 101 |
vtype: INT64, |
| 102 |
numeric: int64ToRaw(v), |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Int64SliceValue creates an INT64SLICE Value. |
| 107 |
func Int64SliceValue(v []int64) Value { |
| 108 |
return Value{vtype: INT64SLICE, slice: attribute.SliceValue(v)} |
| 109 |
} |
| 110 |
|
| 111 |
// Float64Value creates a FLOAT64 Value. |
| 112 |
func Float64Value(v float64) Value { |
| 113 |
return Value{ |
| 114 |
vtype: FLOAT64, |
| 115 |
numeric: float64ToRaw(v), |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Float64SliceValue creates a FLOAT64SLICE Value. |
| 120 |
func Float64SliceValue(v []float64) Value { |
| 121 |
return Value{vtype: FLOAT64SLICE, slice: attribute.SliceValue(v)} |
| 122 |
} |
| 123 |
|
| 124 |
// StringValue creates a STRING Value. |
| 125 |
func StringValue(v string) Value { |
| 126 |
return Value{ |
| 127 |
vtype: STRING, |
| 128 |
stringly: v, |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// StringSliceValue creates a STRINGSLICE Value. |
| 133 |
func StringSliceValue(v []string) Value { |
| 134 |
return Value{vtype: STRINGSLICE, slice: attribute.SliceValue(v)} |
| 135 |
} |
| 136 |
|
| 137 |
// Type returns a type of the Value. |
| 138 |
func (v Value) Type() Type { |
| 139 |
return v.vtype |
| 140 |
} |
| 141 |
|
| 142 |
// AsBool returns the bool value. Make sure that the Value's type is |
| 143 |
// BOOL. |
| 144 |
func (v Value) AsBool() bool { |
| 145 |
return rawToBool(v.numeric) |
| 146 |
} |
| 147 |
|
| 148 |
// AsBoolSlice returns the []bool value. Make sure that the Value's type is |
| 149 |
// BOOLSLICE. |
| 150 |
func (v Value) AsBoolSlice() []bool { |
| 151 |
if v.vtype != BOOLSLICE { |
| 152 |
return nil |
| 153 |
} |
| 154 |
return v.asBoolSlice() |
| 155 |
} |
| 156 |
|
| 157 |
func (v Value) asBoolSlice() []bool { |
| 158 |
return attribute.AsSlice[bool](v.slice) |
| 159 |
} |
| 160 |
|
| 161 |
// AsInt64 returns the int64 value. Make sure that the Value's type is |
| 162 |
// INT64. |
| 163 |
func (v Value) AsInt64() int64 { |
| 164 |
return rawToInt64(v.numeric) |
| 165 |
} |
| 166 |
|
| 167 |
// AsInt64Slice returns the []int64 value. Make sure that the Value's type is |
| 168 |
// INT64SLICE. |
| 169 |
func (v Value) AsInt64Slice() []int64 { |
| 170 |
if v.vtype != INT64SLICE { |
| 171 |
return nil |
| 172 |
} |
| 173 |
return v.asInt64Slice() |
| 174 |
} |
| 175 |
|
| 176 |
func (v Value) asInt64Slice() []int64 { |
| 177 |
return attribute.AsSlice[int64](v.slice) |
| 178 |
} |
| 179 |
|
| 180 |
// AsFloat64 returns the float64 value. Make sure that the Value's |
| 181 |
// type is FLOAT64. |
| 182 |
func (v Value) AsFloat64() float64 { |
| 183 |
return rawToFloat64(v.numeric) |
| 184 |
} |
| 185 |
|
| 186 |
// AsFloat64Slice returns the []float64 value. Make sure that the Value's type is |
| 187 |
// FLOAT64SLICE. |
| 188 |
func (v Value) AsFloat64Slice() []float64 { |
| 189 |
if v.vtype != FLOAT64SLICE { |
| 190 |
return nil |
| 191 |
} |
| 192 |
return v.asFloat64Slice() |
| 193 |
} |
| 194 |
|
| 195 |
func (v Value) asFloat64Slice() []float64 { |
| 196 |
return attribute.AsSlice[float64](v.slice) |
| 197 |
} |
| 198 |
|
| 199 |
// AsString returns the string value. Make sure that the Value's type |
| 200 |
// is STRING. |
| 201 |
func (v Value) AsString() string { |
| 202 |
return v.stringly |
| 203 |
} |
| 204 |
|
| 205 |
// AsStringSlice returns the []string value. Make sure that the Value's type is |
| 206 |
// STRINGSLICE. |
| 207 |
func (v Value) AsStringSlice() []string { |
| 208 |
if v.vtype != STRINGSLICE { |
| 209 |
return nil |
| 210 |
} |
| 211 |
return v.asStringSlice() |
| 212 |
} |
| 213 |
|
| 214 |
func (v Value) asStringSlice() []string { |
| 215 |
return attribute.AsSlice[string](v.slice) |
| 216 |
} |
| 217 |
|
| 218 |
type unknownValueType struct{} |
| 219 |
|
| 220 |
// AsInterface returns Value's data as any. |
| 221 |
func (v Value) AsInterface() any { |
| 222 |
switch v.Type() { |
| 223 |
case BOOL: |
| 224 |
return v.AsBool() |
| 225 |
case BOOLSLICE: |
| 226 |
return v.asBoolSlice() |
| 227 |
case INT64: |
| 228 |
return v.AsInt64() |
| 229 |
case INT64SLICE: |
| 230 |
return v.asInt64Slice() |
| 231 |
case FLOAT64: |
| 232 |
return v.AsFloat64() |
| 233 |
case FLOAT64SLICE: |
| 234 |
return v.asFloat64Slice() |
| 235 |
case STRING: |
| 236 |
return v.stringly |
| 237 |
case STRINGSLICE: |
| 238 |
return v.asStringSlice() |
| 239 |
case EMPTY: |
| 240 |
return nil |
| 241 |
} |
| 242 |
return unknownValueType{} |
| 243 |
} |
| 244 |
|
| 245 |
// Emit returns a string representation of Value's data. |
| 246 |
func (v Value) Emit() string { |
| 247 |
switch v.Type() { |
| 248 |
case BOOLSLICE: |
| 249 |
return fmt.Sprint(v.asBoolSlice()) |
| 250 |
case BOOL: |
| 251 |
return strconv.FormatBool(v.AsBool()) |
| 252 |
case INT64SLICE: |
| 253 |
j, err := json.Marshal(v.asInt64Slice()) |
| 254 |
if err != nil { |
| 255 |
return fmt.Sprintf("invalid: %v", v.asInt64Slice()) |
| 256 |
} |
| 257 |
return string(j) |
| 258 |
case INT64: |
| 259 |
return strconv.FormatInt(v.AsInt64(), 10) |
| 260 |
case FLOAT64SLICE: |
| 261 |
j, err := json.Marshal(v.asFloat64Slice()) |
| 262 |
if err != nil { |
| 263 |
return fmt.Sprintf("invalid: %v", v.asFloat64Slice()) |
| 264 |
} |
| 265 |
return string(j) |
| 266 |
case FLOAT64: |
| 267 |
return fmt.Sprint(v.AsFloat64()) |
| 268 |
case STRINGSLICE: |
| 269 |
j, err := json.Marshal(v.asStringSlice()) |
| 270 |
if err != nil { |
| 271 |
return fmt.Sprintf("invalid: %v", v.asStringSlice()) |
| 272 |
} |
| 273 |
return string(j) |
| 274 |
case STRING: |
| 275 |
return v.stringly |
| 276 |
case EMPTY: |
| 277 |
return "" |
| 278 |
default: |
| 279 |
return "unknown" |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// MarshalJSON returns the JSON encoding of the Value. |
| 284 |
func (v Value) MarshalJSON() ([]byte, error) { |
| 285 |
var jsonVal struct { |
| 286 |
Type string |
| 287 |
Value any |
| 288 |
} |
| 289 |
jsonVal.Type = v.Type().String() |
| 290 |
jsonVal.Value = v.AsInterface() |
| 291 |
return json.Marshal(jsonVal) |
| 292 |
} |