Documentation
¶
Index ¶
- Constants
- func NewDBInserter(opts ...InserterOption) agplusage.Inserter
- type Cron
- type CronJob
- type Generator
- type HeartbeatFunc
- type InserterOption
- type Publisher
- type TallymanPublisherOption
- func PublisherWithClock(clock quartz.Clock) TallymanPublisherOption
- func PublisherWithHTTPClient(httpClient *http.Client) TallymanPublisherOption
- func PublisherWithIngestURL(ingestURL string) TallymanPublisherOption
- func PublisherWithInitialDelay(initialDelay time.Duration) TallymanPublisherOption
- func PublisherWithPrometheusRegisterer(reg prometheus.Registerer) TallymanPublisherOption
Constants ¶
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 )
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
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.
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.
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***.
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.
func PublisherWithPrometheusRegisterer ¶
func PublisherWithPrometheusRegisterer(reg prometheus.Registerer) TallymanPublisherOption
PublisherWithPrometheusRegisterer registers publisher metrics with reg.