base

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GotName    = "got"
	GotVersion = "1.0"
)
View Source
const (
	SpanKeyword = "__GOT_SPAN_KEYWORD__"
)

Variables

View Source
var (
	// DebuggerEnabled
	// 调试模式状态.
	//
	// 当在项目运行目录下发现文件 `.got` 是, 启用调试模式.
	DebuggerEnabled = false
)

Functions

func GotError

func GotError(err error)

func GotFatal

func GotFatal(v interface{})

func GotInfo

func GotInfo(format string, args ...interface{})

Types

type Attr

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

Attr Key/Value 键值对.

用于记录关键数据, 常用的应用场景:

  • authorization: jwt token
  • member.id: 3721
  • request.method: POST
  • request.url: /api/v1/user/123

func NewAttr

func NewAttr() *Attr

func (*Attr) Count

func (o *Attr) Count() int

Count Key/Value 键值对元素数量.

func (*Attr) IsEmpty

func (o *Attr) IsEmpty() bool

IsEmpty 是否空值 Key/Value 键值对.

func (*Attr) Map

func (o *Attr) Map() map[string]interface{}

Map 获取 Key/Value 键值对.

func (*Attr) Set

func (o *Attr) Set(key string, value interface{}) *Attr

Set 设置键值对, 若已存在则覆盖.

type Backtrace

type Backtrace interface {
	// Count
	// 计算栈元素数量.
	Count() int

	// Empty
	// 返回是否为空.
	Empty() bool

	// Items
	// 返回栈元素列表.
	Items() []*BacktraceItem

	// String
	// 以字符串格式返回栈信息.
	//
	//   0 # main.main1() at /workspace/main.go:12
	//   1 # main.main() at /workspace/main.go:5
	String() string
}

Backtrace 异常栈, 当日志级别为 FATAL 时附加到日志中.

type BacktraceItem

type BacktraceItem struct {
	Line int64
	Path string
	Func string
}

BacktraceItem 栈元素.

type ClientSpan

type ClientSpan interface {
	CommonSpan
	LoggerSpan
	Child(name string) ClientSpan
	End()
}

ClientSpan 调用链Span客户端接口.

此接口开放给Span客户端(HTTP、RPC)的生成周期管理方法.

type CommonSpan

type CommonSpan interface {
	Attr() *Attr
	Context() context.Context
	Name() string
	ParentSpanID() SpanID
	SpanID() SpanID
	TraceID() TraceID
}

CommonSpan 调用链Span通用接口.

type Level

type Level string

Level 日志级别.

const (
	LevelDebug Level = "DEBUG"
	LevelError Level = "ERROR"
	LevelFatal Level = "FATAL"
	LevelInfo  Level = "INFO"
	LevelWarn  Level = "WARN"

	DefaultLevel = LevelInfo
)

func (Level) Defaulter

func (o Level) Defaulter() Level

Defaulter 校验日志级别合法性, 若未定义或为空或不合法则返回默认(INFO)级别.

func (Level) IsDebug

func (o Level) IsDebug() bool

func (Level) IsError

func (o Level) IsError() bool

func (Level) IsFatal

func (o Level) IsFatal() bool

func (Level) IsInfo

func (o Level) IsInfo() bool

func (Level) IsWarn

func (o Level) IsWarn() bool

type Log

type Log interface {
	// Clone
	// 从Log上克隆一个新的Log.
	Clone() Log

	// GetBacktrace
	// 读取异常栈.
	GetBacktrace() Backtrace
	GetDuration() time.Duration
	GetLevel() Level
	GetMessage() string
	GetParentSpanID() SpanID
	GetSpanID() SpanID
	GetTime() time.Time
	GetTraceID() TraceID

	Release()

	// CopySpan
	// 复制Span参数.
	//
	// 将Span的链路参数复制到新Log实例中.
	CopySpan(span ReporterSpan) Log

	// CopyDuration
	// 复制Span时长.
	//
	// 将Span的执行时长复制到新Log实例中.
	CopyDuration(duration time.Duration) Log

	// CopyProfile
	// 复制Span信息.
	CopyProfile(p *SpanProfile) Log

	// CopyTime
	// 复制Span时间.
	//
	// 将Span的开始时间复制到新Log实例中.
	CopyTime(t time.Time) Log
}

Log 单条日志接口.

func NewLog

func NewLog(level Level, format string, args ...interface{}) Log

NewLog 从池中取出日志实例, 若未取到(池为空)则创建.

type LogFormatter

type LogFormatter interface {
	// Format
	// 将日志转换成字符串(含JSON字符串).
	Format(log Log) string
}

LogFormatter 日志格式化接口.

type LogSender

type LogSender interface {
	// Send
	// 发送日志.
	Send(logs ...Log)

	// SetFormatter
	// 设置格式化.
	SetFormatter(formatter LogFormatter)

	// Wait
	// 等待发送完成.
	//
	// 一般在服务退出前显性调用. 发送日志(非TERM模式)是异步、批量提交的, 安全退出时需要
	// 等待内存中未发送或发送中的日志全部处理完成.
	Wait()
}

LogSender 日志上报接口.

  • elasticsearch
  • file
  • kafka
  • term (default)

type LoggerSpan

type LoggerSpan interface {
	Debug(format string, args ...interface{})
	Error(format string, args ...interface{})
	Fatal(format string, args ...interface{})
	Info(format string, args ...interface{})
	Warn(format string, args ...interface{})
}

LoggerSpan 调用链Span操作日志接口.

type Memory

type Memory[T any] struct {
	// contains filtered or unexported fields
}

Memory 内存暂存管理.

该组件用于存储Log/Span数据, 异步处理器时从此处取出并上报到指定位置.

func NewMemory

func NewMemory[T any](name string, handler func(items []T)) *Memory[T]

NewMemory 创建内存暂存管理实例.

func (*Memory[T]) Add

func (o *Memory[T]) Add(items ...T)

Add 加入暂存.

若内存中暂存的数据达到阈值时立即触发上报, 反之等待异步任务定时/自动触发.

func (*Memory[T]) SetBatch

func (o *Memory[T]) SetBatch(batch int)

SetBatch 设置批量阈值.

func (*Memory[T]) SetConcurrency

func (o *Memory[T]) SetConcurrency(concurrency int32)

SetConcurrency 设置并行阈值.

func (*Memory[T]) SetFrequency

func (o *Memory[T]) SetFrequency(frequency int)

SetFrequency 设置频率阈值.

func (*Memory[T]) SetSize

func (o *Memory[T]) SetSize(size int)

SetSize 设置暂存空值.

func (*Memory[T]) Start

func (o *Memory[T]) Start(ctx context.Context)

Start 启动内在暂存管理.

func (*Memory[T]) Wait

func (o *Memory[T]) Wait()

Wait 等待数据处理完成.

type ReporterSpan

type ReporterSpan interface {
	CommonSpan
	Duration() time.Duration
	EndTime() time.Time
	Logs() []Log
	Release()
	Sampled() SpanSampled
	StartTime() time.Time
}

ReporterSpan 调用链Span上报接口.

此接口开放给Span上报(Jaeger、Zipkin)的只读方法.

type ReporterType

type ReporterType string

ReporterType Span上报类型.

const (
	JaegerReporter ReporterType = "jaeger"
	NoopReporter   ReporterType = "noop"
	ZipkinReporter ReporterType = "zipkin"
)

func (ReporterType) Defaulter

func (o ReporterType) Defaulter() ReporterType

Defaulter 校验Span上报类型合法性, 若未定义或为空或不合法则返回默认(Noop)类型.

type SenderType

type SenderType string

SenderType 日志上报类型.

const (
	// ElasticSender
	// 上报到<elasticsearch>服务器上.
	ElasticSender SenderType = "elasticsearch"

	// FileSender
	// 写入到本地文件.
	FileSender SenderType = "file"

	// KafkaSender
	// 上报到<kafka>服务器上.
	KafkaSender SenderType = "kafka"

	// TermSender
	// 在终端上打印.
	TermSender SenderType = "term"
)

func (SenderType) Defaulter

func (o SenderType) Defaulter() SenderType

Defaulter 校验日志上报类型合法性, 若未定义或为空或不合法则返回默认(Term)类型.

type SpanID

type SpanID []byte

SpanID Trace 上 Span 的 ID (长度: byte=8, string=16).

func NewSpanID

func NewSpanID() SpanID

NewSpanID 随机生成.

func NewSpanIDFromHex

func NewSpanIDFromHex(str string) SpanID

NewSpanIDFromHex 从字符串上解码.

func (SpanID) Bytes

func (o SpanID) Bytes() []byte

func (SpanID) Empty

func (o SpanID) Empty() bool

func (SpanID) IsValid

func (o SpanID) IsValid() bool

func (SpanID) String

func (o SpanID) String() string

type SpanProfile added in v1.0.2

type SpanProfile struct {
	SpanID, ParentSpanID SpanID
	TraceID              TraceID
}

func NewSpanProfile added in v1.0.2

func NewSpanProfile(span ClientSpan) *SpanProfile

type SpanReporter

type SpanReporter interface {
	// Publish
	// 批量上报.
	Publish(spans ...ReporterSpan)

	// Wait
	// 等待完成.
	//
	// 一般在服务退出前显性调用. 上报 Span 是异步、批量提交的, 安全退出时需要等待
	// 内存中待上报或上报中的 Span 全部处理完成.
	Wait()
}

SpanReporter Span上报接口.

  • jaeger
  • zipkin

type SpanSampled

type SpanSampled string

SpanSampled Span采样.

const (
	SampledNo  SpanSampled = "0"
	SampledYes SpanSampled = "1"
)

func (SpanSampled) Disabled

func (o SpanSampled) Disabled() bool

func (SpanSampled) Enabled

func (o SpanSampled) Enabled() bool

type TraceID

type TraceID []byte

TraceID Trace 的 ID (长度: byte=16, string=32).

func NewTraceID

func NewTraceID() TraceID

NewTraceID 随机生成.

func NewTraceIDFromHex

func NewTraceIDFromHex(str string) TraceID

NewTraceIDFromHex 从字符串上解码.

func (TraceID) Bytes

func (o TraceID) Bytes() []byte

func (TraceID) Empty

func (o TraceID) Empty() bool

func (TraceID) IsValid

func (o TraceID) IsValid() bool

func (TraceID) String

func (o TraceID) String() string

Jump to

Keyboard shortcuts

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