Documentation
¶
Overview ¶
sqlite.go 提供 Store 的默认本地 SQLite 实现。 基于现代 doc(modernc.org/sqlite,无 cgo) 打开本地文件,表结构极简,仅承载 上述最小字段的时序样本。设计为「单写者」:Insert 在调用方 goroutine 同步执行, 上层每个目标对应独立目标,单库单写天然无并发写冲突(见 NewSQLiteStore 设置 SetMaxOpenConns(1))。
Package sqlstore 提供极简的「时序样本」存储抽象与默认的 SQLite 落盘实现。
定位:把「按目标 + 时间窗写入 / 查询结构化样本」这件在监控领域反复出现的事 抽象成一个通用公共模块,供多个应用 / 模块复用,同时让实现可替换—— 默认用现代 doc(ModernC.org/sqlite,无 cgo) 落本地文件;需要对接远端时序库 (ClickHouse / Loki / InfluxDB 等) 时,只要提供满足 Store 接口的实现并替换注入点 即可,调用方(写入 / 查询)不感知。
与其他模块的边界:本包**不依赖任何 idcu-go 领域/应用类型**,只依赖数据库驱动; 上层负责把「领域样本」(如 monitor.Sample) 转换为存储无关的 Sample。
Index ¶
Constants ¶
const DefaultLimit = 1000
DefaultLimit 是 Store.Query 在 limit<=0 时的默认返回条数。
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SQLiteStore ¶
type SQLiteStore struct {
// contains filtered or unexported fields
}
SQLiteStore 是 Store 的默认 SQLite 实现。
func NewSQLiteStore ¶
func NewSQLiteStore(path string) (*SQLiteStore, error)
NewSQLiteStore 打开(或创建) SQLite 数据库并建表。 path 为本地数据库文件路径(需调用方保证所在目录已存在)。
type Sample ¶
type Sample struct {
Name string `json:"name"` // 序列/目标名,Query 按此维度过滤
Host string `json:"host"` // 目标主机
Ts int64 `json:"ts"` // 采样时间,Unix 毫秒
OK bool `json:"ok"` // 是否正常
RTTMs float64 `json:"rtt_ms"` // 往返时延,毫秒
Loss float64 `json:"loss"` // 丢包率,[0,1]
Status string `json:"status"` // 状态文本,如 up/down
}
Sample 是「存储无关」的一条时序样本。 各存储实现(本地 SQLite / 远端时序库)都基于它读写,保证对外 Query 契约稳定。 Ts 为 Unix 毫秒;字段为监控时序的常见最小集,便于多应用按 name 维度聚合。
type Store ¶
type Store interface {
// Insert 写入一条样本。
Insert(s Sample) error
// Query 返回某目标(按 name)在 [since, until] 时间窗(Unix 毫秒,含边界)内的
// 样本,最新在前;name 为空返回全部目标;since/until 为 0 表示该侧不设边界;
// limit<=0 时使用实现默认(如 1000)。
Query(name string, since, until int64, limit int) ([]Sample, error)
// Close 释放底层资源。
Close() error
}
Store 是时序样本存储的抽象。 DefaultSQLiteStore 为默认实现;实现远端时序库后端时,仅需满足本接口并替换 构造点,上层(写入 / 查询) 不感知。