usage

package
v2.37.13 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 24 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// AgentRuntimeInterval is the bucket size of hb_agent_runtime_v1 events.
	AgentRuntimeInterval = time.Hour
	// AgentRuntimeWindow is the trailing window scanned for missing buckets.
	// Buckets still missing beyond this window (e.g. because the deployment
	// was down for longer) are forfeited, which can only ever undercount
	// usage.
	AgentRuntimeWindow = 7 * 24 * time.Hour
	// AgentRuntimeEligibilityLag is how long after a bucket closes before it
	// becomes eligible for generation, giving replicas time to commit
	// in-flight chat messages with timestamps inside the bucket. This
	// assumes chat message inserts commit within the lag of their
	// statement-time created_at; a message committing later than the lag
	// after its bucket closes lands in an already-sealed bucket and its
	// runtime is dropped (undercount-only).
	AgentRuntimeEligibilityLag = 5 * time.Minute
)
View Source
const (
	AISeatsInterval = 4 * time.Hour
)

Variables

This section is empty.

Functions

func NewDBInserter

func NewDBInserter(opts ...InserterOption) agplusage.Inserter

NewDBInserter creates a new database-backed usage event inserter.

Types

type Cron added in v2.32.0

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

Cron runs registered CronJobs on the dbInserter's clock. Stopping the context passed to Start cancels all jobs. Daemon restarts naturally restart the timers since Start() creates them fresh — there is no state to persist or recover.

func NewCron added in v2.32.0

func NewCron(clock quartz.Clock, log slog.Logger, db database.Store, ins agplusage.Inserter) *Cron

NewCron creates a Cron that periodically generates and inserts heartbeat events. The clock controls all timers so that tests can advance time deterministically via quartz.Mock.

func (*Cron) Close added in v2.32.0

func (c *Cron) Close() error

Close cancels all jobs and waits for goroutines to exit.

func (*Cron) Register added in v2.32.0

func (c *Cron) Register(job CronJob) error

Register adds a job. It must be called before Start; calling it after Start returns an error.

func (*Cron) Start added in v2.32.0

func (c *Cron) Start(ctx context.Context)

Start launches a goroutine per job. Subsequent calls are no-ops. On daemon restart a new Cron should be created.

type CronJob added in v2.32.0

type CronJob struct {
	// Name is a human-readable label used in logs.
	Name string
	// Interval is the base duration between ticks.
	Interval time.Duration
	// EventType must match the events generated by the Fn.
	EventType usagetypes.UsageEventType
	// Jitter is the maximum random delay added after the boundary.
	// The actual offset is uniformly distributed in [0, Jitter).
	// This staggers replicas so one is likely to complete the work
	// before others attempt it, allowing them to skip via the
	// existence check (heartbeat inserts are idempotent).
	Jitter time.Duration
	// Fn produces the heartbeat event.
	Fn HeartbeatFunc
}

CronJob defines a periodic heartbeat job.

type Generator added in v2.37.0

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

Generator reconciles hb_agent_runtime_v1 heartbeat usage events. Unlike Cron jobs, which sample live state when they fire, the Generator derives events from data already persisted in the database, so it can deterministically backfill hours missed while the deployment was down, zero-filling idle hours. Deterministic event IDs make concurrent replicas safe without locking: the insert's ON CONFLICT (id) arbiter turns a re-insert of a bucket into a no-op, even when the competing insert is still in flight (once its arbiter index entry is visible, PostgreSQL waits on that transaction and takes the DO NOTHING path if it commits). Only the narrow speculative-insertion race, before the competing row's arbiter entry exists, surfaces a bucket unique violation instead, which generateBucket recognizes as the other replica winning.

Events are generated unconditionally in enterprise builds; the publish_usage_data license flag only gates publishing to Tallyman.

func NewGenerator added in v2.37.0

func NewGenerator(clock quartz.Clock, log slog.Logger, db database.Store, ins agplusage.Inserter) *Generator

NewGenerator creates an unstarted Generator.

func (*Generator) Close added in v2.37.0

func (g *Generator) Close() error

Close stops the Generator and waits for its goroutine to exit. It always returns nil; the error return exists to satisfy io.Closer, as the Generator is registered with the server's closer list.

func (*Generator) Start added in v2.37.0

func (g *Generator) Start(ctx context.Context)

Start launches the reconciliation goroutine. Subsequent calls are no-ops; a closed Generator cannot be restarted.

type HeartbeatFunc added in v2.32.0

type HeartbeatFunc func(ctx context.Context) (event usagetypes.HeartbeatEvent, err error)

HeartbeatFunc generates a heartbeat event and its stable ID. It is called periodically by the cron. Returning an error skips the insert for that tick and logs a warning.

func AISeatsHeartbeat added in v2.32.0

func AISeatsHeartbeat(db database.Store) HeartbeatFunc

AISeatsHeartbeat returns a HeartbeatFunc that queries the active AI seat count and emits it as an HBAISeats heartbeat event.

type InserterOption

type InserterOption func(*dbInserter)

func InserterWithClock

func InserterWithClock(clock quartz.Clock) InserterOption

InserterWithClock sets the quartz clock to use for the inserter.

type Publisher

type Publisher interface {
	// Close closes the publisher and waits for it to finish.
	io.Closer
	// Start starts the publisher. It must only be called once.
	Start() error
}

Publisher publishes usage events ***somewhere***.

func NewTallymanPublisher

func NewTallymanPublisher(ctx context.Context, log slog.Logger, db database.Store, keys map[string]ed25519.PublicKey, opts ...TallymanPublisherOption) Publisher

NewTallymanPublisher creates a Publisher that publishes usage events to Coder's Tallyman service.

type TallymanPublisherOption

type TallymanPublisherOption func(*tallymanPublisher)

func PublisherWithClock

func PublisherWithClock(clock quartz.Clock) TallymanPublisherOption

PublisherWithClock sets the clock to use for publishing usage events.

func PublisherWithHTTPClient

func PublisherWithHTTPClient(httpClient *http.Client) TallymanPublisherOption

PublisherWithHTTPClient sets the HTTP client to use for publishing usage events.

func PublisherWithIngestURL

func PublisherWithIngestURL(ingestURL string) TallymanPublisherOption

PublisherWithIngestURL sets the ingest URL to use for publishing usage events.

func PublisherWithInitialDelay

func PublisherWithInitialDelay(initialDelay time.Duration) TallymanPublisherOption

PublisherWithInitialDelay sets the initial delay for the publisher.

Jump to

Keyboard shortcuts

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