hutils

package module
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2023 License: MIT Imports: 36 Imported by: 2

README

HUtils For Go

CI codecov Go Report Card

Installtion

$ go get -u github.com/zaihui/go-hutils

Documentation

Index

Constants

View Source
const (
	ReqTag  = "[请求参数]"
	RespTag = "[响应结果]"

	HTTP2Protocol         = "HTTP/2"
	LocalHost             = "127.0.0.1"
	ComponentIDGrpcClient = 5013
	ComponentIDGrpcGo     = 23
)
View Source
const (
	// DateLayout 日期序列化.
	DateLayout = "2006-01-02"
	// DateTimeLayout 时间序列化
	DateTimeLayout = "2006-01-02T15:04:05"
	// DateTimeLayoutWithoutT 没有T的时间序列化
	// Deprecated
	DateTimeLayoutWithoutT = ISO8601Local
	// DateTimeLayoutChinese 中文时间序列化
	DateTimeLayoutChinese = "2006年01月02日 15时04分05秒"

	// ISO8601Local 本地ISO8601日期序列化.
	ISO8601Local = "2006-01-02 15:04:05"
)

Variables

View Source
var (
	ComponentIDGOHttpServer int32 = 5004
)

Functions

func CaptureStdout

func CaptureStdout(f func()) ([]string, error)

CaptureStdout 获取func执行的标准输出

func DefaultEncoderConfig added in v0.0.10

func DefaultEncoderConfig(WithStack bool) zapcore.EncoderConfig

func DefaultParseTime added in v0.0.8

func DefaultParseTime(value string) (time.Time, error)

DefaultParseTime 默认时间解析.

func DiffDay added in v0.0.23

func DiffDay(t1, t2 time.Time) int

DiffDay 两个时间差多少天.

func DiffDayWithLayout added in v0.0.23

func DiffDayWithLayout(t1, t2 string, layout string) (int, error)

DiffDayWithLayout 两个时间差多少天.

func Error

func Error(logger *zap.SugaredLogger, err error)

func FilterMethod added in v0.0.23

func FilterMethod(filterMethods []string, method string) bool

FilterMethod filter method not proceed trace.

func FilterURL added in v0.0.23

func FilterURL(filterURLs []string, url string) bool

FilterURL filter url not proceed trace.

func GetDayStartAndLatest added in v0.0.23

func GetDayStartAndLatest(day time.Time) (time.Time, time.Time)

GetDayStartAndLatest 获取某天的开始和结束.

func GetEnv

func GetEnv(key, fallback string) string

GetEnv 获取环境变量,不存在则使用默认值

func JSONMarshal

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

JSONMarshal 类似json.Marshal(), 但不转义特殊符号

func MarshalJSON

func MarshalJSON(msg interface{}) ([]byte, error)

func MarshalParam added in v0.0.23

func MarshalParam(v interface{}) string

func MaxRuneCount added in v0.0.9

func MaxRuneCount(maxLen int) func(s string) error

MaxRuneCount 字符串最大长度(包括中文)

func NewApmSpan

func NewApmSpan(ctx context.Context, name, spanType string) *apm.Span

NewApmSpan 根据context,在当前transaction中生成新的span记录

func NewServerSkywalkingHTTPMiddleware added in v0.0.23

func NewServerSkywalkingHTTPMiddleware(tracer *go2sky.Tracer, opts ...func(*handler)) (func(http.Handler) http.Handler, error)

func NewUUID

func NewUUID() string

NewUUID 生成string类型的uuid

func NewUnaryClientSkywalkingInterceptor added in v0.0.23

func NewUnaryClientSkywalkingInterceptor(tracer *go2sky.Tracer) grpc.UnaryClientInterceptor

NewUnaryClientSkywalkingInterceptor skywalking client interceptor.

func NewUnaryServerAccessLogInterceptor

func NewUnaryServerAccessLogInterceptor(logger *zap.SugaredLogger, apmTracer *apm.Tracer) grpc.UnaryServerInterceptor

NewUnaryServerAccessLogInterceptor returns a new unary server interceptors tha log access log

func NewUnaryServerSkywalkingInterceptor added in v0.0.23

func NewUnaryServerSkywalkingInterceptor(tracer *go2sky.Tracer, opts ...Option) grpc.UnaryServerInterceptor

NewUnaryServerSkywalkingInterceptor skywalking server interceptor. nolint: govet

func Param added in v0.0.23

func Param(param []byte) string

func ParseTime added in v0.0.8

func ParseTime(layout string, value string) (time.Time, error)

ParseTime 当地时区解析时间字符串.

func SetServiceName

func SetServiceName(name string)

func SetTrace added in v0.0.20

func SetTrace(ctx context.Context, name string, apmTracer *apm.Tracer) context.Context

func SpanIDFromContext added in v0.0.9

func SpanIDFromContext(ctx context.Context) string

func Time added in v0.0.8

func Time(year int, month time.Month, day, hour, min int) time.Time

Time time.Date的快捷方法,省略sec,nsec,loc.

func Tomorrow added in v0.0.8

func Tomorrow() time.Time

Tomorrow 明天同一时间.

func TraceIDFromContext added in v0.0.9

func TraceIDFromContext(ctx context.Context) string

func Track

func Track(logger *zap.SugaredLogger, message interface{})

func TrackEncoderConfig added in v0.0.10

func TrackEncoderConfig() zapcore.EncoderConfig

func WithExtraTags added in v0.0.23

func WithExtraTags(tags map[string]string) func(*handler)

func WithFilterMethod added in v0.0.23

func WithFilterMethod(methods []string) func(*options)

func WithFilterURL added in v0.0.23

func WithFilterURL(urls []string) func(*handler)

func WithOperation added in v0.0.23

func WithOperation(operation operation) func(*handler)

func WithReportTags added in v0.0.23

func WithReportTags(tags []string) func(*options)

func Yesterday added in v0.0.8

func Yesterday() time.Time

Yesterday 昨天同一时间.

Types

type AccessLog

type AccessLog struct {
	ClientIP   string
	Method     string
	Request    string
	Protocol   string
	Agent      string
	LogType    string
	GrpcStatus string
	Payload    []byte
	Response   []byte
	Duration   int64
	StatusCode int
}

func (AccessLog) Log

func (l AccessLog) Log(logger *zap.SugaredLogger)

func (AccessLog) LogWithContext added in v0.0.9

func (l AccessLog) LogWithContext(ctx context.Context, logger *zap.SugaredLogger)

type BaseMixin

type BaseMixin struct {
	mixin.Schema
}

BaseMixin model补充一些套路字段

func (BaseMixin) Fields

func (BaseMixin) Fields() []ent.Field

type BaseSchema

type BaseSchema struct {
	ent.Schema
}

BaseSchema 替换ent.Schema使用,包含套路字段

func (BaseSchema) Mixin

func (BaseSchema) Mixin() []ent.Mixin

type CodeError added in v0.0.21

type CodeError interface {
	Error() string
	ErrCode() string
	ErrMessage() string
}

type GetExtraField added in v0.0.22

type GetExtraField func(ctx context.Context) string

type LogType

type LogType string
const (
	ACCESS  LogType = "access"
	REQUEST LogType = "request"
	TRACK   LogType = "track"
	ERROR   LogType = "error"
	UNION   LogType = "union"
)

type Logger

type Logger struct {
	Type    LogType
	LogPath string
}

func (*Logger) Init

func (l *Logger) Init(opt LoggerOpt) (logger *zap.Logger)

type LoggerOpt

type LoggerOpt struct {
	CustomEncoderConfig  *zapcore.EncoderConfig
	CustomEncoderConfigs map[zapcore.Level]*zapcore.EncoderConfig
	EnableStdout         bool
	EnableFile           bool
	IsUnion              bool
	IsJSONEncoder        bool
}

type Option added in v0.0.23

type Option func(*options)

type Period added in v0.0.8

type Period struct {
	Start, End time.Time
}

Period 时间区间.

func ParseDateTimePeriod added in v0.0.8

func ParseDateTimePeriod(start, end string) (*Period, error)

ParseDateTimePeriod 解析时间区间.

type RequestLog

type RequestLog struct {
	Method            string
	Request           string
	StatusDescription string
	Payload           []byte
	Response          []byte
	Duration          int64
}

func (RequestLog) Log

func (l RequestLog) Log(logger *zap.SugaredLogger)

func (RequestLog) LogWithContext added in v0.0.9

func (l RequestLog) LogWithContext(ctx context.Context, logger *zap.SugaredLogger)

type UnionLog added in v0.0.13

type UnionLog struct {
	ExtraFields map[string]GetExtraField
	ClientIP    string
	Protocol    string
	Agent       string
	Method      string
	Request     string
	GrpcStatus  string
	LogType     string
	Payload     []byte
	Response    []byte
	Duration    int64
	StatusCode  int
}

func (UnionLog) Error added in v0.0.13

func (l UnionLog) Error(ctx context.Context, logger *zap.SugaredLogger, err error)

func (UnionLog) Errorf added in v0.0.18

func (l UnionLog) Errorf(ctx context.Context, logger *zap.SugaredLogger, template string, args ...interface{})

func (UnionLog) GetExtraFields added in v0.0.22

func (l UnionLog) GetExtraFields(ctx context.Context, baseInfo []interface{}) []interface{}

func (UnionLog) Log added in v0.0.13

func (l UnionLog) Log(ctx context.Context, logger *zap.SugaredLogger)

func (UnionLog) Track added in v0.0.13

func (l UnionLog) Track(ctx context.Context, logger *zap.SugaredLogger, msg interface{})

func (UnionLog) Trackf added in v0.0.18

func (l UnionLog) Trackf(ctx context.Context, logger *zap.SugaredLogger, template string, args ...interface{})

type ZError added in v0.0.16

type ZError struct {
	Err     error
	Code    string
	Message string
	TraceID string
	SpanID  string
}

ZError nolint: govet // may be we need err stack

func NewZError added in v0.0.16

func NewZError(ctx context.Context, code interface{}, message string, options ...ZErrorOption) *ZError

func (ZError) ErrCode added in v0.0.21

func (z ZError) ErrCode() string

func (ZError) ErrMessage added in v0.0.21

func (z ZError) ErrMessage() string

func (ZError) Error added in v0.0.16

func (z ZError) Error() string

type ZErrorOption added in v0.0.18

type ZErrorOption func(*ZError)

func WithError added in v0.0.18

func WithError(err error) ZErrorOption

Jump to

Keyboard shortcuts

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