Documentation
¶
Overview ¶
Package logging 提供运行日志事件、控制台输出、请求跟踪与事件订阅
Index ¶
- Constants
- Variables
- func ActiveCount() int
- func CancelRequest(id string) bool
- func Clear()
- func Debug(source string, stage string, fields ...any)
- func Enabled(level slog.Level) bool
- func Error(source string, stage string, fields ...any)
- func FormatDuration(duration time.Duration) string
- func FormatValue(value any) string
- func Info(source string, stage string, fields ...any)
- func ParseLevel(value string) (slog.Level, error)
- func Publish(event Event)
- func SanitizeError(message string) string
- func SetFile(path string, enabled bool, maxTotalMB int) error
- func SetLevel(level slog.Level)
- func SetSourceWidth(sources []string)
- func Setup(options Options) error
- func ShortID(id string) string
- func Warn(source string, stage string, fields ...any)
- func Watch(ctx context.Context)
- func WithRequest(ctx context.Context, request *Request) context.Context
- type Attempt
- type Event
- type Field
- type Hub
- type Options
- type Request
- func (r *Request) AccountReady(account string, model string)
- func (r *Request) AddAttemptUsage(input int, reasoning int, reply int)
- func (r *Request) AdoptHedge(hedge *Request, reason string)
- func (r *Request) AttemptFailed(account string, status int, err error, retry bool)
- func (r *Request) Background(kind string)
- func (r *Request) Begin(protocol string, model string, stream bool, messages int, tools int)
- func (r *Request) Begun() bool
- func (r *Request) Cancel()
- func (r *Request) Detach(cancel context.CancelCauseFunc)
- func (r *Request) Fail(status int, message string)
- func (r *Request) Finish(httpStatus int, clientGone bool) RequestInfo
- func (r *Request) Hedge() *Request
- func (r *Request) HedgeSuperseded(account string, duration time.Duration, reason string)
- func (r *Request) ID() string
- func (r *Request) Progress(level string, source string, message string, fields ...any)
- func (r *Request) SetMedia(count int)
- func (r *Request) SetResponseID(responseID string)
- func (r *Request) SetResult(providerModel string, conversationID string, finishReason string)
- func (r *Request) SetStage(stage string)
- func (r *Request) SetThinking(level string)
- func (r *Request) SetToolCalls(count int)
- func (r *Request) SetUsage(input int, reasoning int, reply int)
- func (r *Request) Snapshot() RequestInfo
- func (r *Request) Stage(stage string, fields ...any)
- func (r *Request) UpstreamEvent(kind string)
- func (r *Request) UpstreamHeartbeat()
- func (r *Request) UpstreamRead(n int)
- func (r *Request) UpstreamResponse(status int)
- type RequestInfo
- type Usage
Constants ¶
const ( SourceService = "service" SourceRequest = "request" )
事件来源
const ( EventMessage = "runtime.message" EventRequestStarted = "request.started" EventRequestProgress = "request.progress" EventRequestFinished = "request.finished" )
稳定事件名
const ( StateRunning = "running" StateCompleted = "completed" StateToolCalls = "tool_calls" StateFailed = "failed" StateCancelled = "cancelled" )
请求状态
const FileMB = 10
FileMB 是单个日志文件的轮转大小
const StatusClientClosed = 499
StatusClientClosed 是客户端在请求完成前断开或请求被取消时记录的状态
Variables ¶
var ErrCancelled = errors.New("request cancelled by the service administrator")
ErrCancelled 是管理端取消请求时请求 context 的取消原因
Functions ¶
func FormatDuration ¶
FormatDuration 把时长四舍五入到毫秒,超过一分钟时到 100 毫秒
func SanitizeError ¶
SanitizeError 去掉错误文本中 URL 的查询串,其中含会话参数
Types ¶
type Attempt ¶
type Attempt struct {
Account string `json:"account"`
Status int `json:"status"`
Error string `json:"error"`
DurationMS int64 `json:"duration_ms"`
// Hedge 表示首事件超时后发起的备用请求,Superseded 表示另一方先返回首事件后被取消
Hedge bool `json:"hedge,omitempty"`
Superseded bool `json:"superseded,omitempty"`
}
Attempt 是一次未成功的上游尝试
type Event ¶
type Event struct {
Time time.Time `json:"time"`
Level string `json:"level"`
Source string `json:"source"`
Message string `json:"message"`
Fields []Field `json:"fields,omitempty"`
Event string `json:"event"`
Request *RequestInfo `json:"request,omitempty"`
}
Event 是一条运行日志事件,Message 为事件名称,Fields 为按顺序排列的字段
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub 保存最近事件,把新事件写到控制台并分发给订阅者
type Options ¶
type Options struct {
// Level 是最低输出级别,取值 debug、info、warn、error
Level string
// Format 是控制台格式,取值 text 或 json
Format string
// Writer 是控制台输出位置
Writer io.Writer
}
Options 是日志输出配置
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request 跟踪一次公开 API 请求从进入服务到结束的全部阶段
func FromContext ¶
FromContext 返回 context 中的请求跟踪,没有时返回 nil,nil 上的方法不执行任何操作
func NewRequest ¶
func NewRequest(parent context.Context, method string, path string, clientIP string) (*Request, context.Context)
NewRequest 登记一个进入服务的请求并返回带请求跟踪与取消函数的 context
func (*Request) AccountReady ¶
AccountReady 记录已取得账号请求槽,开始一次上游尝试
func (*Request) AddAttemptUsage ¶ added in v0.2.0
AddAttemptUsage 累加一次失败尝试的 token 用量,请求的用量随之更新
func (*Request) AdoptHedge ¶ added in v0.1.7
AdoptHedge 在备用请求取得输出时并入其跟踪:reason 非空时原请求的尝试以该原因记为被取代,账号、阶段、网络计数与首事件取备用请求的,此后备用请求跟踪的记录都写入原请求
func (*Request) AttemptFailed ¶
AttemptFailed 记录一次失败的上游尝试,retry 表示随后换账号重试
func (*Request) Detach ¶
func (r *Request) Detach(cancel context.CancelCauseFunc)
Detach 标记请求与客户端连接分离,客户端断开后请求按实际结果结束,管理端取消时以 cancel 取消分离后的 context
func (*Request) Finish ¶
func (r *Request) Finish(httpStatus int, clientGone bool) RequestInfo
Finish 按 HTTP 状态与记录的结果结束请求并输出结束日志,clientGone 表示客户端已断开
func (*Request) HedgeSuperseded ¶ added in v0.1.7
HedgeSuperseded 记录原请求先返回首事件后被取消的备用请求
func (*Request) SetResponseID ¶
SetResponseID 记录协议响应 ID
func (*Request) SetThinking ¶ added in v0.1.6
SetThinking 记录按账号模型目录选定的网页思考强度
func (*Request) UpstreamEvent ¶
UpstreamEvent 记录解码出的语义事件,kind 为 thought、text、code、media 等
func (*Request) UpstreamHeartbeat ¶
func (r *Request) UpstreamHeartbeat()
UpstreamHeartbeat 记录上游等待期间的心跳帧
func (*Request) UpstreamResponse ¶
UpstreamResponse 记录生成请求取得响应头
type RequestInfo ¶
type RequestInfo struct {
ID string `json:"id"`
ResponseID string `json:"response_id,omitempty"`
ConversationID string `json:"conversation_id,omitempty"`
State string `json:"state"`
Protocol string `json:"protocol,omitempty"`
Method string `json:"method"`
Path string `json:"path"`
ClientIP string `json:"client_ip,omitempty"`
Model string `json:"model,omitempty"`
ProviderModel string `json:"provider_model,omitempty"`
Thinking string `json:"thinking,omitempty"`
Account string `json:"account,omitempty"`
Stream bool `json:"stream"`
Status int `json:"status,omitempty"`
StartedAt time.Time `json:"started_at"`
DurationMS int64 `json:"duration_ms"`
QueueMS int64 `json:"queue_ms,omitempty"`
FirstEventMS int64 `json:"first_event_ms,omitempty"`
Stage string `json:"stage,omitempty"`
Messages int `json:"messages,omitempty"`
Media int `json:"media,omitempty"`
Tools int `json:"tools,omitempty"`
ToolCalls int `json:"tool_calls,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Error string `json:"error,omitempty"`
Attempts []Attempt `json:"attempts,omitempty"`
UpstreamBytes int64 `json:"upstream_bytes,omitempty"`
Heartbeats int `json:"heartbeats,omitempty"`
}
RequestInfo 是请求的阶段、账号、用量与结果