clickhouse

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

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

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

func New(cfg Config) (*Sink, error)

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) Close

func (s *Sink) Close() error

Close flushes the WAL and closes the ClickHouse connection.

func (*Sink) Dropped

func (s *Sink) Dropped() uint64

Dropped returns the number of events dropped due to WAL maxSegments cap.

func (*Sink) Events

func (s *Sink) Events(ctx context.Context, q usage.EventQuery) ([]usage.Event, error)

Events returns raw usage events matching q, newest-first.

func (*Sink) Summary

Summary returns aggregated rows grouped by q.GroupBy.

func (*Sink) TimeSeries

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.

func (*Sink) Write

func (s *Sink) Write(ev usage.Event) error

Write appends ev to the WAL. The call returns as soon as the event is durable on the local filesystem; CH delivery is asynchronous.

Jump to

Keyboard shortcuts

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