2020-05-14 01:06:03 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
package attribute // import "go.opentelemetry.io/otel/attribute"
|
2020-05-14 01:06:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-13 16:34:02 +02:00
|
|
|
"reflect"
|
2020-05-14 01:06:03 +02:00
|
|
|
"strconv"
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/internal"
|
2022-10-13 16:34:02 +02:00
|
|
|
"go.opentelemetry.io/otel/internal/attribute"
|
2020-05-14 01:06:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate stringer -type=Type
|
|
|
|
|
|
|
|
// Type describes the type of the data Value holds.
|
2022-05-19 22:15:07 +02:00
|
|
|
type Type int // nolint: revive // redefines builtin Type.
|
2020-05-14 01:06:03 +02:00
|
|
|
|
|
|
|
// Value represents the value part in key-value pairs.
|
|
|
|
type Value struct {
|
|
|
|
vtype Type
|
|
|
|
numeric uint64
|
|
|
|
stringly string
|
2021-08-12 17:05:42 +02:00
|
|
|
slice interface{}
|
2020-05-14 01:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-10-12 17:39:12 +02:00
|
|
|
// INVALID is used for a Value with no value set.
|
|
|
|
INVALID Type = iota
|
|
|
|
// BOOL is a boolean Type Value.
|
|
|
|
BOOL
|
|
|
|
// INT64 is a 64-bit signed integral Type Value.
|
|
|
|
INT64
|
2021-04-12 17:10:00 +02:00
|
|
|
// FLOAT64 is a 64-bit floating point Type Value.
|
2020-10-12 17:39:12 +02:00
|
|
|
FLOAT64
|
|
|
|
// STRING is a string Type Value.
|
|
|
|
STRING
|
2021-08-12 17:05:42 +02:00
|
|
|
// BOOLSLICE is a slice of booleans Type Value.
|
|
|
|
BOOLSLICE
|
|
|
|
// INT64SLICE is a slice of 64-bit signed integral numbers Type Value.
|
|
|
|
INT64SLICE
|
|
|
|
// FLOAT64SLICE is a slice of 64-bit floating point numbers Type Value.
|
|
|
|
FLOAT64SLICE
|
|
|
|
// STRINGSLICE is a slice of strings Type Value.
|
|
|
|
STRINGSLICE
|
2020-05-14 01:06:03 +02:00
|
|
|
)
|
|
|
|
|
2020-07-24 21:25:27 +02:00
|
|
|
// BoolValue creates a BOOL Value.
|
|
|
|
func BoolValue(v bool) Value {
|
2020-05-14 01:06:03 +02:00
|
|
|
return Value{
|
|
|
|
vtype: BOOL,
|
|
|
|
numeric: internal.BoolToRaw(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// BoolSliceValue creates a BOOLSLICE Value.
|
|
|
|
func BoolSliceValue(v []bool) Value {
|
2022-10-13 16:34:02 +02:00
|
|
|
return Value{vtype: BOOLSLICE, slice: attribute.SliceValue(v)}
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 19:08:46 +02:00
|
|
|
// IntValue creates an INT64 Value.
|
|
|
|
func IntValue(v int) Value {
|
|
|
|
return Int64Value(int64(v))
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// IntSliceValue creates an INTSLICE Value.
|
|
|
|
func IntSliceValue(v []int) Value {
|
2022-10-13 16:34:02 +02:00
|
|
|
var int64Val int64
|
|
|
|
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val)))
|
|
|
|
for i, val := range v {
|
|
|
|
cp.Elem().Index(i).SetInt(int64(val))
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
return Value{
|
|
|
|
vtype: INT64SLICE,
|
2022-10-13 16:34:02 +02:00
|
|
|
slice: cp.Elem().Interface(),
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:25:27 +02:00
|
|
|
// Int64Value creates an INT64 Value.
|
|
|
|
func Int64Value(v int64) Value {
|
2020-05-14 01:06:03 +02:00
|
|
|
return Value{
|
|
|
|
vtype: INT64,
|
|
|
|
numeric: internal.Int64ToRaw(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// Int64SliceValue creates an INT64SLICE Value.
|
|
|
|
func Int64SliceValue(v []int64) Value {
|
2022-10-13 16:34:02 +02:00
|
|
|
return Value{vtype: INT64SLICE, slice: attribute.SliceValue(v)}
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:25:27 +02:00
|
|
|
// Float64Value creates a FLOAT64 Value.
|
|
|
|
func Float64Value(v float64) Value {
|
2020-05-14 01:06:03 +02:00
|
|
|
return Value{
|
|
|
|
vtype: FLOAT64,
|
|
|
|
numeric: internal.Float64ToRaw(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// Float64SliceValue creates a FLOAT64SLICE Value.
|
|
|
|
func Float64SliceValue(v []float64) Value {
|
2022-10-13 16:34:02 +02:00
|
|
|
return Value{vtype: FLOAT64SLICE, slice: attribute.SliceValue(v)}
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 02:09:03 +02:00
|
|
|
// StringValue creates a STRING Value.
|
2020-07-24 21:25:27 +02:00
|
|
|
func StringValue(v string) Value {
|
2020-05-14 01:06:03 +02:00
|
|
|
return Value{
|
|
|
|
vtype: STRING,
|
|
|
|
stringly: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// StringSliceValue creates a STRINGSLICE Value.
|
|
|
|
func StringSliceValue(v []string) Value {
|
2022-10-13 16:34:02 +02:00
|
|
|
return Value{vtype: STRINGSLICE, slice: attribute.SliceValue(v)}
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
// Type returns a type of the Value.
|
|
|
|
func (v Value) Type() Type {
|
|
|
|
return v.vtype
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsBool returns the bool value. Make sure that the Value's type is
|
|
|
|
// BOOL.
|
|
|
|
func (v Value) AsBool() bool {
|
|
|
|
return internal.RawToBool(v.numeric)
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// AsBoolSlice returns the []bool value. Make sure that the Value's type is
|
|
|
|
// BOOLSLICE.
|
|
|
|
func (v Value) AsBoolSlice() []bool {
|
2022-11-26 01:27:47 +02:00
|
|
|
if v.vtype != BOOLSLICE {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.asBoolSlice()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Value) asBoolSlice() []bool {
|
2022-10-13 16:34:02 +02:00
|
|
|
return attribute.AsSlice[bool](v.slice)
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
// AsInt64 returns the int64 value. Make sure that the Value's type is
|
|
|
|
// INT64.
|
|
|
|
func (v Value) AsInt64() int64 {
|
|
|
|
return internal.RawToInt64(v.numeric)
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// AsInt64Slice returns the []int64 value. Make sure that the Value's type is
|
|
|
|
// INT64SLICE.
|
|
|
|
func (v Value) AsInt64Slice() []int64 {
|
2022-11-26 01:27:47 +02:00
|
|
|
if v.vtype != INT64SLICE {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.asInt64Slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Value) asInt64Slice() []int64 {
|
2022-10-13 16:34:02 +02:00
|
|
|
return attribute.AsSlice[int64](v.slice)
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
// AsFloat64 returns the float64 value. Make sure that the Value's
|
|
|
|
// type is FLOAT64.
|
|
|
|
func (v Value) AsFloat64() float64 {
|
|
|
|
return internal.RawToFloat64(v.numeric)
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// AsFloat64Slice returns the []float64 value. Make sure that the Value's type is
|
2021-12-20 18:10:34 +02:00
|
|
|
// FLOAT64SLICE.
|
2021-08-12 17:05:42 +02:00
|
|
|
func (v Value) AsFloat64Slice() []float64 {
|
2022-11-26 01:27:47 +02:00
|
|
|
if v.vtype != FLOAT64SLICE {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.asFloat64Slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Value) asFloat64Slice() []float64 {
|
2022-10-13 16:34:02 +02:00
|
|
|
return attribute.AsSlice[float64](v.slice)
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
// AsString returns the string value. Make sure that the Value's type
|
|
|
|
// is STRING.
|
|
|
|
func (v Value) AsString() string {
|
|
|
|
return v.stringly
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
// AsStringSlice returns the []string value. Make sure that the Value's type is
|
2021-12-20 18:10:34 +02:00
|
|
|
// STRINGSLICE.
|
2021-08-12 17:05:42 +02:00
|
|
|
func (v Value) AsStringSlice() []string {
|
2022-11-26 01:27:47 +02:00
|
|
|
if v.vtype != STRINGSLICE {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.asStringSlice()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Value) asStringSlice() []string {
|
2022-10-13 16:34:02 +02:00
|
|
|
return attribute.AsSlice[string](v.slice)
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
type unknownValueType struct{}
|
|
|
|
|
|
|
|
// AsInterface returns Value's data as interface{}.
|
|
|
|
func (v Value) AsInterface() interface{} {
|
|
|
|
switch v.Type() {
|
|
|
|
case BOOL:
|
|
|
|
return v.AsBool()
|
2021-08-12 17:05:42 +02:00
|
|
|
case BOOLSLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return v.asBoolSlice()
|
2020-05-14 01:06:03 +02:00
|
|
|
case INT64:
|
|
|
|
return v.AsInt64()
|
2021-08-12 17:05:42 +02:00
|
|
|
case INT64SLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return v.asInt64Slice()
|
2020-05-14 01:06:03 +02:00
|
|
|
case FLOAT64:
|
|
|
|
return v.AsFloat64()
|
2021-08-12 17:05:42 +02:00
|
|
|
case FLOAT64SLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return v.asFloat64Slice()
|
2020-05-14 01:06:03 +02:00
|
|
|
case STRING:
|
|
|
|
return v.stringly
|
2021-08-12 17:05:42 +02:00
|
|
|
case STRINGSLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return v.asStringSlice()
|
2020-05-14 01:06:03 +02:00
|
|
|
}
|
|
|
|
return unknownValueType{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit returns a string representation of Value's data.
|
|
|
|
func (v Value) Emit() string {
|
|
|
|
switch v.Type() {
|
2021-09-09 17:42:47 +02:00
|
|
|
case BOOLSLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return fmt.Sprint(v.asBoolSlice())
|
2020-05-14 01:06:03 +02:00
|
|
|
case BOOL:
|
|
|
|
return strconv.FormatBool(v.AsBool())
|
2021-09-09 17:42:47 +02:00
|
|
|
case INT64SLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return fmt.Sprint(v.asInt64Slice())
|
2020-05-14 01:06:03 +02:00
|
|
|
case INT64:
|
|
|
|
return strconv.FormatInt(v.AsInt64(), 10)
|
2021-09-09 17:42:47 +02:00
|
|
|
case FLOAT64SLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return fmt.Sprint(v.asFloat64Slice())
|
2020-05-14 01:06:03 +02:00
|
|
|
case FLOAT64:
|
|
|
|
return fmt.Sprint(v.AsFloat64())
|
2021-09-09 17:42:47 +02:00
|
|
|
case STRINGSLICE:
|
2022-11-26 01:27:47 +02:00
|
|
|
return fmt.Sprint(v.asStringSlice())
|
2020-05-14 01:06:03 +02:00
|
|
|
case STRING:
|
|
|
|
return v.stringly
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of the Value.
|
|
|
|
func (v Value) MarshalJSON() ([]byte, error) {
|
|
|
|
var jsonVal struct {
|
|
|
|
Type string
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
jsonVal.Type = v.Type().String()
|
|
|
|
jsonVal.Value = v.AsInterface()
|
|
|
|
return json.Marshal(jsonVal)
|
|
|
|
}
|