loki

package
v0.0.0-...-79486b3 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package loki 提供 Loki 日志查询客户端

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client Loki 客户端

func NewClient

func NewClient(baseURL string, opts ...Option) *Client

NewClient 创建 Loki 客户端

func (*Client) FindLastEvent

func (c *Client) FindLastEvent(ctx context.Context, taskID string, event string, start, end time.Time) (time.Time, error)

FindLastEvent 倒序分页扫描 Loki 日志,找到最后一个匹配 event 的条目,返回其时间戳。 优先从 Loki structured metadata (labels) 中读取 event 字段,回退到解析 JSON body。 end 为搜索的结束时间上界,零值表示 time.Now()。

func (*Client) FindLatestRoundStart

func (c *Client) FindLatestRoundStart(ctx context.Context, taskID string, taskCreatedAt, end time.Time) (time.Time, error)

FindLatestRoundStart 定位 attach 模式下最新论次的起点。 语义固定为:最近一个 user-input;若不存在,则退回任务创建时间。

func (*Client) History

func (c *Client) History(ctx context.Context, taskID string, start time.Time, fn func([]LogEntry)) (time.Time, error)

History 分页获取历史日志 History 分页获取历史日志,返回最后一条日志的时间戳。 如果没有日志,返回零时间戳。

func (*Client) QueryByTaskID

func (c *Client) QueryByTaskID(ctx context.Context, taskID string, start, end time.Time, limit int, direction string) ([]LogEntry, error)

QueryByTaskID 根据 task_id 查询区间日志

func (*Client) QueryRounds

func (c *Client) QueryRounds(ctx context.Context, taskID string, start, end time.Time, limit int) (*QueryRoundsResp, error)

QueryRounds 按 task-started 切分论次,倒序分页查询。 每个论次从 user-input 到 task-ended(user-input → ... → task-started → ... → task-ended)。 从 end 往前扫描到 start,凑满 limit 论后停止,返回正序排列的 chunks。

func (*Client) QueryWindowByTaskID

func (c *Client) QueryWindowByTaskID(ctx context.Context, taskID string, start, end time.Time) ([]LogEntry, error)

QueryWindowByTaskID 按时间窗口正序查询 task 日志,只返回历史数据,不进入实时阶段。

func (*Client) Tail

func (c *Client) Tail(ctx context.Context, taskID string, start time.Time, limit int, lastTS time.Time, fn func([]LogEntry) error) error

Tail 使用 WebSocket 替代 HTTP 轮询,提供完整的实时日志流 策略:

  1. 历史阶段:通过 HTTP 查询从 start 到 now-skew 的所有历史日志
  2. 实时阶段:建立 WebSocket 连接,从 lastTS-skew 开始接收实时日志
  3. 去重机制:基于"时间戳+日志内容"的复合键去重,处理同一纳秒的多条日志
  4. 每收到一条日志立即调用回调函数(无批处理)

start: 日志查询起始时间 limit: 单次查询/接收的最大日志条数 lastTS: 历史阶段已发送的最后一条日志时间戳,用于初始化去重状态,防止重复推送 fn: 日志回调函数,接收单条日志的切片,返回 error 可中断处理

type LogEntry

type LogEntry struct {
	Timestamp time.Time
	Line      string
	Labels    map[string]string
}

LogEntry 日志条目

type Option

type Option func(*Client)

Option 配置选项

func WithBasicAuth

func WithBasicAuth(user, pass string) Option

func WithBearerToken

func WithBearerToken(token string) Option

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

func WithLogger

func WithLogger(logger *slog.Logger) Option

func WithOrgID

func WithOrgID(orgID string) Option

type QueryRoundsResp

type QueryRoundsResp struct {
	Chunks  []*RoundChunk // 正序排列(user-input → task-started → ... → task-ended),论次间最新在前
	HasMore bool
	NextTS  int64 // 下一页起始时间戳(Unix Nano),仅当 HasMore 时有效
}

QueryRoundsResp 查询论次响应

type RoundChunk

type RoundChunk struct {
	Data      []byte            `json:"data,omitempty"`
	Event     string            `json:"event"`
	Kind      string            `json:"kind"`
	Timestamp int64             `json:"timestamp"` // Unix Nano
	Labels    map[string]string `json:"labels,omitempty"`
}

RoundChunk 论次日志条目

Jump to

Keyboard shortcuts

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