Documentation
¶
Overview ¶
Package usage keeps dtctl's opt-in local usage ledger: a per-day tally of the DQL queries this machine's dtctl ran and the data they scanned.
The ledger exists because a user without access to the tenant's billing events (`fetch dt.system.events | filter event.kind == "BILLING_USAGE_EVENT"` needs storage:system:read, which cannot be scoped to one user) otherwise has no running total of what their own scripts and agents consume. It is deliberately minimal:
- opt-in per context (`usage-tracking: true`); with it off nothing is written, not even an empty file
- aggregate counters only, keyed by (UTC date, context name): query count, failed-query count, scanned bytes, records and data points. Never query text, results, user identity, environment URLs or tokens
- one small JSON file under the XDG state directory, mode 0600, updated under a cross-process lock with an atomic temp-file + rename, and pruned to the last RetentionDays days on every write
- best effort: recording never fails or noticeably delays the command that ran the query. Any error skips the update.
It counts only what dtctl itself ran on this machine: queries from the web UI, notebooks, dashboards, apps, MCP servers or other machines are not in it. Billing events remain the authoritative source.
Index ¶
Constants ¶
const ( // FileName is the ledger's file name inside the dtctl state directory. FileName = "usage.json" // RetentionDays is how many UTC days the ledger keeps, today included. // Older days are pruned on every write. RetentionDays = 90 )
const BillingHint = `fetch dt.system.events | filter event.kind == "BILLING_USAGE_EVENT"`
BillingHint names the authoritative source of consumption data. Help text and command output repeat it so nobody mistakes the local tally for a bill.
Variables ¶
var ErrNewerFormat = errors.New("usage ledger was written by a newer dtctl version")
ErrNewerFormat reports a ledger written by a newer dtctl. It is never rewritten, so a downgrade cannot destroy the newer data.
var Windows = []Window{{"today", 1}, {"7d", 7}, {"30d", 30}}
Windows are the spans `dtctl usage` reports.
Functions ¶
func DefaultPath ¶
func DefaultPath() string
DefaultPath is where the ledger lives: $XDG_STATE_HOME/dtctl/usage.json (typically ~/.local/state/dtctl/usage.json). It is state rather than cache or config: losing it is harmless, but it is not regenerable.
func FormatBytes ¶
FormatBytes renders a scanned-data volume in binary units (GiB, MiB, ...), the unit Grail reports scan volume in. It never renders a currency: what a GiB costs depends on the contract.
Types ¶
type Counters ¶
type Counters struct {
Queries int64 `json:"queries"`
Failed int64 `json:"failed,omitempty"`
ScannedBytes int64 `json:"scannedBytes"`
ScannedRecords int64 `json:"scannedRecords"`
ScannedDataPoints int64 `json:"scannedDataPoints,omitempty"`
}
Counters is the aggregate for one context on one day, or a sum of them.
type Ledger ¶
type Ledger struct {
// contains filtered or unexported fields
}
Ledger is the usage file at one path.
func (*Ledger) Read ¶
Read returns the ledger's days. A missing file is an empty ledger, not an error, and reading one creates nothing: with tracking off, even `dtctl usage` leaves the disk untouched.
func (*Ledger) Record ¶
Record adds one query execution for contextName to today's counters and prunes days beyond the retention window, all under the ledger lock.
func (*Ledger) Reset ¶
Reset removes contextName's counters from every day, or the whole ledger when contextName is empty. It reports whether anything was removed.
type Recorder ¶
type Recorder struct {
Ledger *Ledger
Context string
// Debug, when non-nil, receives one line per skipped update.
Debug io.Writer
}
Recorder files samples for one context in a ledger, best effort: errors are reported to debug (when set) and otherwise dropped, because a failed tally must never fail or delay the query that produced it.
func (*Recorder) RecordQuery ¶
RecordQuery files s. It never fails.
type Sample ¶
type Sample struct {
// Failed marks an execution that did not return a result (an error or a
// cancellation). It still counts as a query, and whatever the backend
// reported as scanned before it ended is still added.
Failed bool
ScannedBytes int64
ScannedRecords int64
ScannedDataPoints int64
}
Sample is what one DQL execution contributes to the ledger.