clickhouse

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package clickhouse provides a ClickHouse-backed implementation of payload.Sink, payload.Reader, and payload.Closer — the Langfuse-style "text bodies in ClickHouse" backend for the Logs view. Metadata + bodies live in one MergeTree table (bodies as ZSTD String columns); List projects only the metadata columns, Get fetches the body columns by request_id.

Durability mirrors the usage CH sink: a WAL-segment queue. Records append to an active segment; full segments rotate and flush to ClickHouse; a segment is deleted only after CH confirms the insert; leftover segments are drained on boot. Unlike the usage WAL, rotation also triggers on a byte threshold — payload bodies are MB-scale, so a line-count cap alone would let a segment grow to gigabytes.

Out of scope: schema migrations (IF NOT EXISTS), dedup of replayed segments (idempotency is the caller's concern), multi-node coordination (each 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 30 — payload bodies
	// are bulky and short-lived debug/audit artifacts, so a shorter default
	// than the usage sink's 90.
	RetentionDays int

	// WALDir is the directory for WAL segment files.
	WALDir string

	// MaxLines is the number of records per WAL segment before rotation.
	// Default 2000 (lower than usage's 10k — records are large).
	MaxLines int

	// MaxBytes rotates the active segment once it exceeds this size,
	// whichever comes first with MaxLines. Default 64 MiB.
	MaxBytes 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 are dropped and counted in Dropped().
	// Default 256.
	MaxSegments int
}

Config holds all tunables for the ClickHouse payload sink. It mirrors the usage CH sink's config; the WAL knobs are shared, plus a byte-based rotation threshold since payload bodies are MB-scale (a line-count cap alone would let a segment grow to gigabytes before rotating).

type Reader

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

Reader is the read-only half: a ClickHouse connection serving List/Get with no WAL. The /payloads endpoints use this so the read path carries none of the write-side machinery (segment queue, recovery). Implements payload.Reader + payload.Closer.

func NewReader

func NewReader(cfg Config) (*Reader, error)

NewReader opens a read-only connection (no WAL). ensureSchema runs so the Logs page works against an empty table before the first write.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the underlying connection.

func (*Reader) Get

func (s *Reader) Get(ctx context.Context, requestID string) (payload.Record, error)

Get returns the captured record (bodies included) for one request id. The newest row wins if an id was somehow reused. Returns payload.ErrNotFound when absent.

type Sink

type Sink struct {
	*Reader
	// contains filtered or unexported fields
}

Sink is the write half: Reader + a WAL-backed insert path. Implements payload.Sink (and, via the embedded Reader, payload.Reader) + payload.Closer.

func New

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

New opens a ClickHouse connection, ensures the schema, 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 records dropped due to the WAL maxSegments cap.

func (*Sink) Write

func (s *Sink) Write(r payload.Record) error

Write appends r to the WAL. Returns as soon as the record 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