utils

package
v0.0.0-...-3fb3ecb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2023 License: Apache-2.0 Imports: 18 Imported by: 4

Documentation

Index

Constants

View Source
const (
	LogLevel_ERROR = 1 << iota
	LogLevel_WARN
	LogLevel_INFO
	LogLevel_DEBUG
	LogLevel_TRACE
	LogLevel_Type_TRACE = "trace"
	LogLevel_Type_DEBUG = "debug"
	LogLevel_Type_INFO  = "info"
	LogLevel_Type_WARN  = "warn"
	LogLevel_Type_ERROR = "error"
)

Variables

View Source
var (
	FunctionDurationTimeCostTotal *prometheus.GaugeVec
	FunctionDurationTimeCostCount *prometheus.GaugeVec
)

Functions

func Contains

func Contains(items []string, item string) bool

func DeepCopy

func DeepCopy(value interface{}) (interface{}, error)

performance: coping follow object 1000000 times cost: 2554 ms

sub := map[string]interface{}{
	"suba": "string",
	"subb": 12345,
	"x":    true,
}

function:

a, e := DeepCopy(nil)
a, e := DeepCopy([]string{"1", "2"})
a, e := DeepCopy(true)
a, e := DeepCopy(123)
a, e := DeepCopy(3.14)
a, e := DeepCopy(map[string]interface{}{})
a, e := DeepCopy("123")

func DeepEqual

func DeepEqual(a, b map[string]interface{}) bool

func Diff

func Diff(a, b []string) (c, d, u []string)

func FieldsIsExpected

func FieldsIsExpected(fields, expected interface{}) bool

func HandleCrash

func HandleCrash(slog *SLOG)

func HttpRequest

func HttpRequest(client *http.Client, url, method, payload string, headers map[string]string) (int, []byte, error)

func IsIpv6

func IsIpv6(ipstr string) bool

func Keyname

func Keyname(s ...string) string

func MD5

func MD5(v []byte) string

func MarshalJson

func MarshalJson(v interface{}) (map[string]interface{}, error)

func MarshalNoEscaping

func MarshalNoEscaping(v interface{}) ([]byte, error)

func NeedRetry

func NeedRetry(err error) bool

func RequestIdFromContext

func RequestIdFromContext(ctx context.Context) string

func RetryErrorf

func RetryErrorf(format string, v ...interface{}) error

func SortIt

func SortIt(s *[]interface{}) []interface{}

TODO: test it with

A: []interface{}{
	{"name": "customHTTPProfile"},
	{"name": "customTCPProfile"},
}
B: []interface{}{
	{"name": "customTCPProfile"},
	{"name": "customHTTPProfile"},
}

reflect.DeepEqual(SortIt(A), SortIt(B)) == true

func Split

func Split(str string, size int) []string

func ThisFuncName

func ThisFuncName() string

func TimeIt

func TimeIt(slog *SLOG) func(format string, a ...interface{}) int64

func TimeItToPrometheus

func TimeItToPrometheus() func()

func TimeItTrace

func TimeItTrace(slog *SLOG) func(format string, a ...interface{}) int64

func TimeItWithLogFunc

func TimeItWithLogFunc(lf func(format string, v ...interface{}), skip int) func(format string, a ...interface{}) int64

func Unified

func Unified(a []string) []string

func UnmarshalJson

func UnmarshalJson(data interface{}, v interface{}) error

Types

type CtxKeyType

type CtxKeyType string
const (
	CtxKey_RequestID CtxKeyType = "request_id"
	CtxKey_Logger    CtxKeyType = "logger"
)

type SLOG

type SLOG struct {
	Level int
	// contains filtered or unexported fields
}

func LogFromContext

func LogFromContext(ctx context.Context) *SLOG

func NewLog

func NewLog() *SLOG

func (*SLOG) Debugf

func (slog *SLOG) Debugf(format string, v ...interface{})

func (*SLOG) Errorf

func (slog *SLOG) Errorf(format string, v ...interface{})

func (*SLOG) Infof

func (slog *SLOG) Infof(format string, v ...interface{})

func (*SLOG) Tracef

func (slog *SLOG) Tracef(format string, v ...interface{})

func (*SLOG) Warnf

func (slog *SLOG) Warnf(format string, v ...interface{})

func (*SLOG) WithLevel

func (slog *SLOG) WithLevel(level string) *SLOG

func (*SLOG) WithRequestID

func (slog *SLOG) WithRequestID(reqid string) *SLOG

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL