Documentation
¶
Overview ¶
Package client is a Go client for light-tokenmeter's gRPC API: recording usage events and reading summaries, with large windows fetched in parts so a summary is not cut at the server's row cap.
Index ¶
- Constants
- func Merge(rows []*usagev1.UsageSummaryRow, byDay bool) []*usagev1.UsageSummaryRow
- func ParseDuration(s string) (time.Duration, error)
- func USD(nanousd int64) float64
- type Client
- func (c *Client) ByMeta(ctx context.Context, req *usagev1.GetUsageByMetaRequest) (*usagev1.GetUsageByMetaResponse, error)
- func (c *Client) Close() error
- func (c *Client) Record(ctx context.Context, events ...*usagev1.UsageEvent) (created, duplicates int, err error)
- func (c *Client) Service() usagev1.UsageServiceClient
- func (c *Client) Summary(ctx context.Context, req *usagev1.GetUsageSummaryRequest) (rows []*usagev1.UsageSummaryRow, truncated bool, err error)
- type Options
Constants ¶
const MaxBatch = 500
MaxBatch is the most events one RecordUsageEvents call takes.
const MinWindow = time.Hour
MinWindow is the shortest window Summary splits a truncated one into.
Variables ¶
This section is empty.
Functions ¶
func Merge ¶
func Merge(rows []*usagev1.UsageSummaryRow, byDay bool) []*usagev1.UsageSummaryRow
Merge sums rows that have the same dimensions, and the same day when byDay is set; without it every row covers the whole window. The order is that of each key's first row.
func ParseDuration ¶
ParseDuration reads Go durations plus d (days) and w (weeks): 7d, 2w, 36h.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to one light-tokenmeter. It is safe for concurrent use.
func (*Client) ByMeta ¶
func (c *Client) ByMeta(ctx context.Context, req *usagev1.GetUsageByMetaRequest) (*usagev1.GetUsageByMetaResponse, error)
ByMeta returns what the events tagged meta_key=meta_value used.
func (*Client) Record ¶
func (c *Client) Record(ctx context.Context, events ...*usagev1.UsageEvent) (created, duplicates int, err error)
Record sends events in batches of MaxBatch and counts what was new. A refused batch stops it; the batches before it stay recorded.
func (*Client) Service ¶
func (c *Client) Service() usagev1.UsageServiceClient
Service is the generated client, for calls this package does not wrap.
func (*Client) Summary ¶
func (c *Client) Summary(ctx context.Context, req *usagev1.GetUsageSummaryRequest) (rows []*usagev1.UsageSummaryRow, truncated bool, err error)
Summary returns the rows of req's window. When the server caps a window (truncated), Summary fetches its halves and merges them, down to MinWindow; truncated is true only if a window that short is still capped.
type Options ¶
type Options struct {
Addr string // gRPC host:port
Token string // bearer token; empty when the service runs without auth
TLS bool // use TLS
TLSConfig *tls.Config // optional TLS settings; nil means system roots, TLS 1.2+
// DialOptions are added after the client's own, for tests and custom transports.
DialOptions []grpc.DialOption
}
Options say where light-tokenmeter is and how to reach it.