mirror of
https://github.com/hanzoai/otel-collector.git
synced 2026-10-30 18:26:25 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
133 lines
3.0 KiB
Go
133 lines
3.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/hex"
|
|
encodingjson "encoding/json"
|
|
"math"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"go.opentelemetry.io/collector/pdata/pcommon"
|
|
)
|
|
|
|
func TraceIDToHexOrEmptyString(traceID pcommon.TraceID) string {
|
|
if !traceID.IsEmpty() {
|
|
return hex.EncodeToString(traceID[:])
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func SpanIDToHexOrEmptyString(spanID pcommon.SpanID) string {
|
|
if !spanID.IsEmpty() {
|
|
return hex.EncodeToString(spanID[:])
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func IsValidFloat(value float64) bool {
|
|
// Check for NaN, +/-Inf
|
|
return !math.IsNaN(value) && !math.IsInf(value, 0)
|
|
}
|
|
|
|
type TagType string
|
|
|
|
const (
|
|
TagTypeAttribute TagType = "tag"
|
|
TagTypeScope TagType = "scope"
|
|
TagTypeResource TagType = "resource"
|
|
|
|
TagTypeSpanField TagType = "spanfield"
|
|
TagTypeLogField TagType = "logfield"
|
|
TagTypeBodyField TagType = "body"
|
|
)
|
|
|
|
func (t TagType) String() string {
|
|
return string(t)
|
|
}
|
|
|
|
type FieldDataType string
|
|
|
|
const (
|
|
FieldDataTypeString FieldDataType = "string"
|
|
FieldDataTypeFloat64 FieldDataType = "float64"
|
|
FieldDataTypeInt64 FieldDataType = "int64"
|
|
FieldDataTypeBool FieldDataType = "bool"
|
|
FieldDataTypeArrayString FieldDataType = "[]string"
|
|
FieldDataTypeArrayInt64 FieldDataType = "[]int64"
|
|
FieldDataTypeArrayFloat64 FieldDataType = "[]float64"
|
|
FieldDataTypeArrayBool FieldDataType = "[]bool"
|
|
FieldDataTypeArrayJSON FieldDataType = "[]json"
|
|
FieldDataTypeArrayDynamic FieldDataType = "[]dynamic"
|
|
)
|
|
|
|
func (t FieldDataType) String() string {
|
|
return string(t)
|
|
}
|
|
|
|
func MakeKeyForRFCache(bucketTs int64, fingerprint string) string {
|
|
var v strings.Builder
|
|
v.WriteString(strconv.Itoa(int(bucketTs)))
|
|
v.WriteString(":")
|
|
v.WriteString(fingerprint)
|
|
return v.String()
|
|
}
|
|
|
|
func MakeKeyForAttributeKeys(tagKey string, tagType TagType, dataType FieldDataType) string {
|
|
var key strings.Builder
|
|
key.WriteString(tagKey)
|
|
key.WriteString(":")
|
|
key.WriteString(string(tagType))
|
|
key.WriteString(":")
|
|
key.WriteString(string(dataType))
|
|
return key.String()
|
|
}
|
|
|
|
func MakeKeyForSkipKeys(context TagType, tagKey string) string {
|
|
var key strings.Builder
|
|
key.WriteString(string(context))
|
|
key.WriteString(":")
|
|
key.WriteString(tagKey)
|
|
return key.String()
|
|
}
|
|
|
|
// Unquote attempts to unquote the string if not then returns the original
|
|
func Unquote(value string) string {
|
|
unquoted, err := strconv.Unquote(value)
|
|
if err == nil {
|
|
return unquoted
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
// IsJSON works with encoding/json instead of goccy because of better benchmarking specifically for Valid function with default package
|
|
// read here: https://github.com/hanzoai/otel-collector/pull/641#issuecomment-3068629067
|
|
func IsJSON(v any) bool {
|
|
switch val := v.(type) {
|
|
case string:
|
|
return encodingjson.Valid([]byte(val))
|
|
case []byte:
|
|
return encodingjson.Valid(val)
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func Concurrency() int {
|
|
return int(math.Max(1, float64(runtime.GOMAXPROCS(0))))
|
|
}
|
|
|
|
//go:fix inline
|
|
func ToPointer[T any](v T) *T {
|
|
return new(v)
|
|
}
|
|
|
|
func ToLookUpMap[T comparable](s []T) map[T]struct{} {
|
|
lookUp := map[T]struct{}{}
|
|
for _, e := range s {
|
|
lookUp[e] = struct{}{}
|
|
}
|
|
return lookUp
|
|
}
|