okit/tags.go

136 lines
5.5 KiB
Go
Raw Normal View History

2022-11-01 16:21:43 +00:00
// Copyright (C) 2022 Mya Pitzeruse
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package okit
import (
"time"
"go.pitz.tech/okit/pb"
)
// Tag defines a generic data holder that allows values to be passed around during observation. Both raw values and
// pointers can be used, allowing data to be lazily read and pulled during emission.
type Tag struct {
key string
value interface{}
}
// AsTagPB converts the tag to the proto tag for transport and reporting.
func (t Tag) AsTagPB() *pb.Tag {
if t.value == nil {
return nil
}
switch v := t.value.(type) {
// values
case string:
return &pb.Tag{Key: t.key, Value: &pb.Tag_String_{String_: v}}
case int:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(v)}}
case int8:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(v)}}
case int16:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(v)}}
case int32:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(v)}}
case int64:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: v}}
case float32:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Double{Double: float64(v)}}
case float64:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Double{Double: v}}
case []byte:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Bytes{Bytes: v}}
case bool:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Bool{Bool: v}}
case time.Duration:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Duration{Duration: pb.DurationPB(v)}}
case time.Time:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Timestamp{Timestamp: pb.TimestampPB(v)}}
// pointers
case *string:
return &pb.Tag{Key: t.key, Value: &pb.Tag_String_{String_: *v}}
case *int:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(*v)}}
case *int8:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(*v)}}
case *int16:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(*v)}}
case *int32:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: int64(*v)}}
case *int64:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Int64{Int64: *v}}
case *float32:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Double{Double: float64(*v)}}
case *float64:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Double{Double: *v}}
case *[]byte:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Bytes{Bytes: *v}}
case *bool:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Bool{Bool: *v}}
case *time.Duration:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Duration{Duration: pb.DurationPB(*v)}}
case *time.Time:
return &pb.Tag{Key: t.key, Value: &pb.Tag_Timestamp{Timestamp: pb.TimestampPB(*v)}}
}
return nil
}
func String(key, value string) Tag { return Tag{key, value} }
func Stringp(key string, value *string) Tag { return Tag{key, value} }
func Int(key string, value int) Tag { return Tag{key, value} }
func Intp(key string, value *int) Tag { return Tag{key, value} }
func Int8(key string, value int8) Tag { return Tag{key, value} }
func Int8p(key string, value *int8) Tag { return Tag{key, value} }
func Int16(key string, value int16) Tag { return Tag{key, value} }
func Int16p(key string, value *int16) Tag { return Tag{key, value} }
func Int32(key string, value int32) Tag { return Tag{key, value} }
func Int32p(key string, value *int32) Tag { return Tag{key, value} }
func Int64(key string, value int64) Tag { return Tag{key, value} }
func Int64p(key string, value *int64) Tag { return Tag{key, value} }
func Uint(key string, value uint) Tag { return Tag{key, value} }
func Uintp(key string, value *uint) Tag { return Tag{key, value} }
func Uint8(key string, value uint8) Tag { return Tag{key, value} }
func Uint8p(key string, value *uint8) Tag { return Tag{key, value} }
func Uint16(key string, value uint16) Tag { return Tag{key, value} }
func Uint16p(key string, value *uint16) Tag { return Tag{key, value} }
func Uint32(key string, value uint32) Tag { return Tag{key, value} }
func Uint32p(key string, value *uint32) Tag { return Tag{key, value} }
func Uint64(key string, value uint64) Tag { return Tag{key, value} }
func Uint64p(key string, value *uint64) Tag { return Tag{key, value} }
func Float32(key string, value float32) Tag { return Tag{key, value} }
func Float32p(key string, value *float32) Tag { return Tag{key, value} }
func Float64(key string, value float64) Tag { return Tag{key, value} }
func Float64p(key string, value *float64) Tag { return Tag{key, value} }
func Bytes(key string, value []byte) Tag { return Tag{key, value} }
func Bytesp(key string, value *[]byte) Tag { return Tag{key, value} }
func Bool(key string, value bool) Tag { return Tag{key, value} }
func Boolp(key string, value *bool) Tag { return Tag{key, value} }
func Duration(key string, value time.Duration) Tag { return Tag{key, value} }
func Durationp(key string, value *time.Duration) Tag { return Tag{key, value} }
func Timestamp(key string, value time.Time) Tag { return Tag{key, value} }
func Timestampp(key string, value *time.Time) Tag { return Tag{key, value} }