Documentation
¶
Overview ¶
Package clickhouse provides a ClickHouse-backed implementation of usage.Sink, usage.Reader, and usage.Closer.
Durability is provided by a WAL-segment queue: events are appended to an active segment file; full segments are rotated and flushed to ClickHouse; a segment is deleted only after ClickHouse confirms the insert. Crash recovery is automatic — leftover segments from a previous run are drained on startup.
Out of scope: schema migrations (the table is created with IF NOT EXISTS), deduplication of replayed segments (idempotency is the caller's concern), and multi-node coordination (each relay pod runs its own WAL in its own directory).
Index ¶
- type Config
- type Sink
- func (s *Sink) Close() error
- func (s *Sink) Dropped() uint64
- func (s *Sink) Events(ctx context.Context, q usage.EventQuery) ([]usage.Event, error)
- func (s *Sink) Summary(ctx context.Context, q usage.SummaryQuery) (usage.SummaryResult, error)
- func (s *Sink) TimeSeries(ctx context.Context, q usage.TimeSeriesQuery) (usage.TimeSeriesResult, error)
- func (s *Sink) Write(ev usage.Event) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// DSN is the ClickHouse connection string (clickhouse://host:port/db).
DSN string
// RetentionDays controls the MergeTree TTL. Default 90.
RetentionDays int
// WALDir is the directory for WAL segment files.
WALDir string
// MaxLines is the number of events per WAL segment before rotation.
// Default 10000.
MaxLines int
// FlushInterval is how often the background goroutine rotates and
// flushes pending segments. Default 10s.
FlushInterval time.Duration
// MaxSegments caps how many pending segment files may accumulate on
// disk. When exceeded, the oldest segments are dropped and counted in
// Dropped(). Default 1024.
MaxSegments int
}
Config holds all tunables for the ClickHouse sink.
type Sink ¶
type Sink struct {
// contains filtered or unexported fields
}
Sink is the ClickHouse-backed implementation of usage.Sink, usage.Reader, and usage.Closer.
func New ¶
New opens a ClickHouse connection, ensures the schema exists, constructs the WAL segment queue, and drains any segments left from a previous run.
func (*Sink) Summary ¶
func (s *Sink) Summary(ctx context.Context, q usage.SummaryQuery) (usage.SummaryResult, error)
Summary returns aggregated rows grouped by q.GroupBy.
func (*Sink) TimeSeries ¶
func (s *Sink) TimeSeries(ctx context.Context, q usage.TimeSeriesQuery) (usage.TimeSeriesResult, error)
TimeSeries buckets matched events by q.Interval (epoch-aligned via toStartOfInterval), optionally split into one series per q.GroupBy value. Empty buckets are not emitted. Series are ordered by total requests desc; points within a series oldest-first.