usage

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 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

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

View Source
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
)
View Source
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

View Source
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.

View Source
var Windows = []Window{{"today", 1}, {"7d", 7}, {"30d", 30}}

Windows are the spans `dtctl usage` reports.

Functions

func Contexts

func Contexts(days Days) []string

Contexts lists every context name with counters in days, sorted.

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

func FormatBytes(b int64) string

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.

func (*Counters) Add

func (c *Counters) Add(o Counters)

Add folds o into c.

type Days

type Days map[string]map[string]Counters

Days maps a UTC date ("2006-01-02") to the counters of each context name.

type Ledger

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

Ledger is the usage file at one path.

func New

func New(path string) *Ledger

New returns the ledger at path.

func (*Ledger) Path

func (l *Ledger) Path() string

Path is the ledger file's location.

func (*Ledger) Read

func (l *Ledger) Read() (Days, error)

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

func (l *Ledger) Record(contextName string, s Sample) error

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

func (l *Ledger) Reset(contextName string) (bool, error)

Reset removes contextName's counters from every day, or the whole ledger when contextName is empty. It reports whether anything was removed.

func (*Ledger) Today

func (l *Ledger) Today() string

Today is the UTC date the ledger files a sample under right now.

func (*Ledger) WithClock

func (l *Ledger) WithClock(now func() time.Time) *Ledger

WithClock overrides the clock (tests).

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

func (r *Recorder) RecordQuery(s Sample)

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.

type Total

type Total struct {
	Window Window
	// From and To are the first and last UTC date the window covers.
	From, To string
	Counters
}

Total is one window's sum for one context.

func Summarize

func Summarize(days Days, contextName string, now time.Time, windows []Window) []Total

Summarize sums contextName's counters over each window ending at now.

type Window

type Window struct {
	// Name is the window's label ("today", "7d", "30d").
	Name string
	// Days is how many UTC days it covers, today included.
	Days int
}

Window is a span of whole UTC days ending today.

Jump to

Keyboard shortcuts

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