util

package
v0.0.0-...-4030fd3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 25 Imported by: 1

Documentation

Index

Constants

View Source
const (
	InputCollectAlarm   = "INPUT_COLLECT_ALARM"
	CategoryConfigAlarm = "CATEGORY_CONFIG_ALARM"
)
View Source
const (
	ShardHashTagKey = "__shardhash__"
	PackIDTagKey    = "__pack_id__"
)
View Source
const BufferSize = 4096

Variables

View Source
var (
	ErrCommandTimeout = errors.New("command time out")
	ErrNotImplemented = errors.New("not implemented yet")
	ErrInvalidEnvType = errors.New("invalid env type")
)
View Source
var ErrReaderTimeout = errors.New("timeout")
View Source
var RegisterAlarms map[string]*Alarm

Functions

func CombinedOutputTimeout

func CombinedOutputTimeout(c *exec.Cmd, timeout time.Duration) ([]byte, error)

CombinedOutputTimeout runs the given command with the given timeout and returns the combined output of stdout and stderr. If the command times out, it attempts to kill the process.

func Contains

func Contains[T comparable](s []T, e T) bool

func CutString

func CutString(val string, maxLen int) string

func DeepCopy

func DeepCopy(src *map[string]interface{}) *map[string]interface{}

func DeleteAlarm

func DeleteAlarm(key string)

func DoFuncWithTimeout

func DoFuncWithTimeout(timeout time.Duration, workFunc func() error) error

func GetHostName

func GetHostName() string

func GetIPAddress

func GetIPAddress() string

func GetTLSConfig

func GetTLSConfig(sslCert, sslKey, sslCA string, insecureSkipVerify bool) (*tls.Config, error)

GetTLSConfig gets a tls.Config object from the given certs, key, and CA files. you must give the full path to the files. If all files are blank and InsecureSkipVerify=false, returns a nil pointer.

func GuessRegionByEndpoint

func GuessRegionByEndpoint(endPoint, defaultRegion string) string

GuessRegionByEndpoint guess region cn-hangzhou.log.aliyuncs.com cn-hangzhou-intranet.log.aliyuncs.com cn-hangzhou-vpc.log.aliyuncs.com cn-hangzhou-share.log.aliyuncs.com

func InitFromEnvBool

func InitFromEnvBool(key string, value *bool, defaultValue bool) error

func InitFromEnvInt

func InitFromEnvInt(key string, value *int, defaultValue int) error

func InitFromEnvString

func InitFromEnvString(key string, value *string, defaultValue string) error

func InterfaceToJSONString

func InterfaceToJSONString(val interface{}) (string, error)

func InterfaceToString

func InterfaceToString(val interface{}) (string, bool)

func IsSafeString

func IsSafeString(str1, str2 string) bool

func MinInt

func MinInt(a, b int) int

func NewLogTagForPackID

func NewLogTagForPackID(prefix string, seqNum *int64) *protocol.LogTag

func NewPackIDPrefix

func NewPackIDPrefix(text string) string

func PathExists

func PathExists(path string) (bool, error)

func RandomSleep

func RandomSleep(base time.Duration, precisionLose float64, shutdown <-chan struct{}) bool

return true if shutdown is signaled

func RandomString

func RandomString(n int) string

RandomString returns a random string of alpha-numeric characters

func ReadFirstBlock

func ReadFirstBlock(line string) string

ReadFirstBlock read first \S+ from head of line

func ReadLines

func ReadLines(filename string) ([]string, error)

ReadLines reads contents from a file and splits them by new lines. A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).

func ReadLinesOffsetN

func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error)

ReadLinesOffsetN reads contents from file and splits them by new line. The offset tells at which line number to start. The count determines the number of lines to read (starting from offset): n >= 0: at most n lines n < 0: whole file

func RegisterAlarm

func RegisterAlarm(key string, alarm *Alarm)

func RegisterAlarmsSerializeToPb

func RegisterAlarmsSerializeToPb(logGroup *protocol.LogGroup)

func RunTimeout

func RunTimeout(c *exec.Cmd, timeout time.Duration) error

RunTimeout runs the given command with the given timeout. If the command times out, it attempts to kill the process.

func SetNetworkIdentification

func SetNetworkIdentification(hostIP, hostname string)

SetNetworkIdentification updates return values of GetIPAddress and GetHostName. NOTE: It is not thread-safety, this is called by plugin_manager.LoadGlobalConfig only.

func Sleep

func Sleep(interval time.Duration, shutdown <-chan struct{}) bool

Sleep returns true if shutdown is signaled.

func SnakeCase

func SnakeCase(in string) string

SnakeCase converts the given string to snake case following the Golang format: acronyms are converted to lower-case and preceded by an underscore.

func SplitPath

func SplitPath(path string) (dir string, filename string)

func StringDeepCopy

func StringDeepCopy(src string) string

StringDeepCopy returns a deep copy or src. Because we can not make sure the life cycle of string passed from C++, so we have to make a deep copy of them so that they are always valid in Go.

func StringPointer

func StringPointer(s string) unsafe.Pointer

StringPointer returns the pointer of the given string. nolint:gosec

func TryConvertLocalhost2RealIP

func TryConvertLocalhost2RealIP(ip string) string

func UniqueStrings

func UniqueStrings(strSlices ...[]string) []string

UniqueStrings Merge (append) slices and remove duplicate from them!

func WaitTimeout

func WaitTimeout(c *exec.Cmd, timeout time.Duration) error

WaitTimeout waits for the given command to finish with a timeout. It assumes the command has already been started. If the command times out, it attempts to kill the process.

func ZeroCopyBytesToString

func ZeroCopyBytesToString(b []byte) (s string)

func ZeroCopyStringToBytes

func ZeroCopyStringToBytes(s string) (b []byte)

Types

type Alarm

type Alarm struct {
	AlarmMap map[string]*AlarmItem
	Project  string
	Logstore string
}
var GlobalAlarm *Alarm

func (*Alarm) Init

func (p *Alarm) Init(project, logstore string)

func (*Alarm) Record

func (p *Alarm) Record(alarmType, message string)

func (*Alarm) SerializeToPb

func (p *Alarm) SerializeToPb(logGroup *protocol.LogGroup)

func (*Alarm) Update

func (p *Alarm) Update(project, logstore string)

type AlarmItem

type AlarmItem struct {
	Message string
	Count   int
}

type TimeoutReader

type TimeoutReader struct {
	// contains filtered or unexported fields
}

func NewTimeoutReader

func NewTimeoutReader(r io.Reader, to time.Duration) *TimeoutReader

func (*TimeoutReader) Read

func (r *TimeoutReader) Read(b []byte) (n int, err error)

Jump to

Keyboard shortcuts

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