Documentation
¶
Overview ¶
Package opencensus normalizes the use of OpenCensus through local, "sharded" aggregation that is GENERIC over the labels key K.
Instead of calling stats.Record per event (each call builds a tag.Map and sends a recordReq to the global worker), we accumulate per key K across N shards and emit in bursts every `interval`. The key K is any comparable struct you define; a Schema[K] (Strategy pattern) projects it onto OpenCensus.
Three variants behind the SAME Aggregator[K, N] interface (SumCount and Distribution here; LastValue in lastvalue.go). The hot path (Add) does not allocate on the heap after a key is seen for the first time; the flush swaps the map to avoid blocking writers and reuses the per-key context via ctxCache.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Aggregator ¶
type Aggregator[K comparable, N Number] interface { Add(k K, value N) Stop() }
Aggregator is the common interface, generic over the labels key K and the measure value type N.
type Config ¶
type Config[K comparable] struct { Shards int // rounded up to a power of 2. Default 16. // Interval is the flush cadence. Default 10s. // // Tuning rule: set Interval to an exact divisor of the exporter reporting // period (view.SetReportingPeriod) and at most half of it. For the common // 30s period use 10s or 15s; 10s also divides the common 60s period, hence // the default. Interval equal to the reporting period causes phase races // (export windows with no fresh flush); non-divisor intervals cause sawtooth // in delta/rate charts. // // For LastValue gauges, Interval is the maximum staleness at export time. // For distributions with MaxSamplesPerKey, the reservoir resets each flush, // so shorter intervals improve percentile fidelity. Interval time.Duration Schema Schema[K] // key projection strategy }
Config holds the settings shared by every aggregator variant: the shard count, the flush interval and the key projection Schema.
Cardinality is the dimension that drives cost. Flush is O(distinct keys): each flush does one ctxCache lookup plus one stats.Record per key, and every record is funneled to the single global OpenCensus worker goroutine. The ctxCache (see ctxcache.go) grows with the number of distinct keys ever seen and is never evicted, so with unbounded label cardinality you must cap or project keys via Schema. Rough guidance:
- ≲1k keys: Interval 5–10s is fine.
- 10k–100k+ keys: prefer Interval 10–15s and set MaxSamplesPerKey on distributions to bound memory.
- Do not go below ~5s at high cardinality: flush bursts can saturate the OpenCensus worker channel and block the flusher goroutine.
Shards affect writer contention (concurrent Add goroutines), not key count.
type CountAggregator ¶
type CountAggregator[K comparable, N Number] struct { // contains filtered or unexported fields }
CountAggregator accumulates the running count per key K across sharded stores and flushes both to OpenCensus on the configured interval.
func NewCountAggregator ¶
func NewCountAggregator[K comparable, N Number](cfg CountConfig[K, N]) *CountAggregator[K, N]
NewCountAggregator builds a CountAggregator from cfg, applying defaults and starting the background flusher.
func (*CountAggregator[K, N]) Add ¶
func (a *CountAggregator[K, N]) Add(k K, _ N)
Add adds value to the running increments its count.
func (*CountAggregator[K, N]) Stop ¶
func (a *CountAggregator[K, N]) Stop()
Stop halts the background flusher.
type CountConfig ¶
type CountConfig[K comparable, N Number] struct { Config[K] CountMeasure Measure[N] }
CountConfig configures a SumCountAggregator, pairing the shared Config with the measures used to record the accumulated count.
type DistributionAggregator ¶
type DistributionAggregator[K comparable, N Number] struct { // contains filtered or unexported fields }
DistributionAggregator collects per-key samples across sharded stores and flushes them to OpenCensus on the configured interval. When MaxSamplesPerKey is set it keeps a bounded reservoir sample per key.
func NewDistributionAggregator ¶
func NewDistributionAggregator[K comparable, N Number](cfg DistributionConfig[K, N]) *DistributionAggregator[K, N]
NewDistributionAggregator builds a DistributionAggregator from cfg, applying defaults and starting the background flusher.
func (*DistributionAggregator[K, N]) Add ¶
func (a *DistributionAggregator[K, N]) Add(k K, value N)
Add records value as a sample for k, using reservoir sampling once the per-key sample cap is reached.
func (*DistributionAggregator[K, N]) Stop ¶
func (a *DistributionAggregator[K, N]) Stop()
Stop halts the background flusher.
type DistributionConfig ¶
type DistributionConfig[K comparable, N Number] struct { Config[K] Measure Measure[N] MaxSamplesPerKey int // 0 = exact; >0 = reservoir sampling (bounded memory) }
DistributionConfig configures a DistributionAggregator with the shared Config, the measure to record samples against and the optional per-key sample cap.
type LastValueAggregator ¶
type LastValueAggregator[K comparable, N Number] struct { // contains filtered or unexported fields }
LastValueAggregator keeps the last value recorded per key K across sharded stores and flushes it to OpenCensus on the configured interval.
func NewLastValueAggregator ¶
func NewLastValueAggregator[K comparable, N Number](cfg LastValueConfig[K, N]) *LastValueAggregator[K, N]
NewLastValueAggregator builds a LastValueAggregator from cfg, applying defaults and starting the background flusher.
func (*LastValueAggregator[K, N]) Add ¶
func (a *LastValueAggregator[K, N]) Add(k K, value N)
Add overwrites the value of the key. The shard lock serializes the writes: "last-write-wins" is well defined by the acquisition order.
func (*LastValueAggregator[K, N]) Stop ¶
func (a *LastValueAggregator[K, N]) Stop()
Stop halts the background flusher.
type LastValueConfig ¶
type LastValueConfig[K comparable, N Number] struct { Config[K] // Measure must be backed by a view of type view.LastValue(). Measure Measure[N] }
LastValueConfig configures a LastValueAggregator: it embeds the shared Config and the measure whose view must be of type view.LastValue().
type Measure ¶ added in v1.1.0
type Measure[N Number] interface { M(v N) stats.Measurement }
Measure is satisfied by *stats.Float64Measure (N=float64) and *stats.Int64Measure (N=int64). A parametrized interface is needed because M has a different signature on each concrete measure, so a union constraint alone cannot expose it directly.
type Schema ¶
type Schema[K comparable] interface { Hash(k K) uint64 Mutators(k K) []tag.Mutator }
Schema is the strategy that projects a labels key K onto OpenCensus: Hash distributes the key across shards on the hot path, and Mutators builds the tag.Mutator values used to derive the recording context.
type SumAggregator ¶
type SumAggregator[K comparable, N Number] struct { // contains filtered or unexported fields }
SumAggregator accumulates the running sum per key K across sharded stores and flushes both to OpenCensus on the configured interval.
func NewSumAggregator ¶
func NewSumAggregator[K comparable, N Number](cfg SumConfig[K, N]) *SumAggregator[K, N]
NewSumAggregator builds a SumCountAggregator from cfg, applying defaults and starting the background flusher.
func (*SumAggregator[K, N]) Add ¶
func (a *SumAggregator[K, N]) Add(k K, value N)
Add adds value to the running sum for k.
func (*SumAggregator[K, N]) Stop ¶
func (a *SumAggregator[K, N]) Stop()
Stop halts the background flusher.