Skip to content

Commit

Permalink
Merge pull request #222 from lightstep/jmacd/factorhisto
Browse files Browse the repository at this point in the history
Factor the exponential histogram into a re-usable package
  • Loading branch information
jmacd authored Jul 18, 2022
2 parents bd1ef35 + 0a34d83 commit aa82048
Show file tree
Hide file tree
Showing 21 changed files with 768 additions and 519 deletions.
2 changes: 1 addition & 1 deletion lightstep/sdk/metric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ however it is not covered by any stability guarantee.

Differences from the OpenTelemetry metrics SDK specification:

1. [ExponentialHistogram](./aggregator/histogram/README.md) is the
1. [ExponentialHistogram](./aggregator/histogram/structure/README.md) is the
default aggregation for Histogram instruments. The
explicit-boundary histogram aggregation is not supported.
2. [MinMaxSumCount](./aggregator/minmaxsumcount/README.md) is an
Expand Down
30 changes: 26 additions & 4 deletions lightstep/sdk/metric/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"

"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/aggregator/aggregation"
histostruct "github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/aggregator/histogram/structure"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/number"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/sdkinstrument"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -59,19 +60,40 @@ func RangeTest[N number.Any, Traits number.Traits[N]](num N, desc sdkinstrument.
return true
}

// HistogramConfig configures the exponential histogram.
type HistogramConfig struct {
// JSONHistogramConfig configures the exponential histogram.
type JSONHistogramConfig struct {
MaxSize int32 `json:"max_size"`
}

// JSONConfig supports the configuration for all aggregators in a single struct.
type JSONConfig struct {
Histogram JSONHistogramConfig `json:"histogram"`
}

// ToConfig returns a Config from the fixed-JSON represented.
func (jc JSONConfig) ToConfig() Config {
return Config{
Histogram: histostruct.NewConfig(histostruct.WithMaxSize(jc.Histogram.MaxSize)),
}
}

// Config supports the configuration for all aggregators in a single struct.
type Config struct {
Histogram HistogramConfig `json:"histogram"`
Histogram histostruct.Config
}

// Valid returns true for valid configurations.
func (c Config) Valid() bool {
return c.Histogram.MaxSize == 0 || c.Histogram.MaxSize >= 2
_, err := c.Validate()
return err == nil
}

// Valid returns a valid Configuration along with an error if there
// were invalid settings. Note that the empty state is considered valid and a correct
func (c Config) Validate() (Config, error) {
var err error
c.Histogram, err = c.Histogram.Validate()
return c, err
}

// Methods implements a specific aggregation behavior for a specific
Expand Down
26 changes: 13 additions & 13 deletions lightstep/sdk/metric/aggregator/gauge/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// colleciton path in this library.

type (
Methods[N number.Any, Traits number.Traits[N], Storage State[N, Traits]] struct{}
Methods[N number.Any, Traits number.Traits[N]] struct{}

State[N number.Any, Traits number.Traits[N]] struct {
lock sync.Mutex
Expand All @@ -45,8 +45,8 @@ type (
Int64 = State[int64, number.Int64Traits]
Float64 = State[float64, number.Float64Traits]

Int64Methods = Methods[int64, number.Int64Traits, Int64]
Float64Methods = Methods[float64, number.Float64Traits, Float64]
Int64Methods = Methods[int64, number.Int64Traits]
Float64Methods = Methods[float64, number.Float64Traits]
)

// initialSequence is the first assigned sequence number, also the
Expand Down Expand Up @@ -108,19 +108,19 @@ func (g *State[N, Traits]) SetSequenceForTesting() {
g.seq = initialSequence
}

func (Methods[N, Traits, Storage]) Kind() aggregation.Kind {
func (Methods[N, Traits]) Kind() aggregation.Kind {
return aggregation.GaugeKind
}

func (Methods[N, Traits, Storage]) Init(state *State[N, Traits], _ aggregator.Config) {
func (Methods[N, Traits]) Init(state *State[N, Traits], _ aggregator.Config) {
// Note: storage is zero to start
}

func (Methods[N, Traits, Storage]) HasChange(ptr *State[N, Traits]) bool {
func (Methods[N, Traits]) HasChange(ptr *State[N, Traits]) bool {
return ptr.seq != 0
}

func (Methods[N, Traits, Storage]) Move(from, to *State[N, Traits]) {
func (Methods[N, Traits]) Move(from, to *State[N, Traits]) {
from.lock.Lock()
defer from.lock.Unlock()

Expand All @@ -130,14 +130,14 @@ func (Methods[N, Traits, Storage]) Move(from, to *State[N, Traits]) {
from.seq = 0
}

func (Methods[N, Traits, Storage]) Copy(from, to *State[N, Traits]) {
func (Methods[N, Traits]) Copy(from, to *State[N, Traits]) {
from.lock.Lock()
defer from.lock.Unlock()
to.value = from.value
to.seq = from.seq
}

func (Methods[N, Traits, Storage]) Update(state *State[N, Traits], number N) {
func (Methods[N, Traits]) Update(state *State[N, Traits], number N) {
newSeq := atomic.AddUint64(&sequenceVar, 1)

state.lock.Lock()
Expand All @@ -147,7 +147,7 @@ func (Methods[N, Traits, Storage]) Update(state *State[N, Traits], number N) {
state.seq = newSeq
}

func (Methods[N, Traits, Storage]) Merge(from, to *State[N, Traits]) {
func (Methods[N, Traits]) Merge(from, to *State[N, Traits]) {
to.lock.Lock()
defer to.lock.Unlock()

Expand All @@ -157,15 +157,15 @@ func (Methods[N, Traits, Storage]) Merge(from, to *State[N, Traits]) {
}
}

func (Methods[N, Traits, Storage]) ToAggregation(state *State[N, Traits]) aggregation.Aggregation {
func (Methods[N, Traits]) ToAggregation(state *State[N, Traits]) aggregation.Aggregation {
return state
}

func (Methods[N, Traits, Storage]) ToStorage(aggr aggregation.Aggregation) (*State[N, Traits], bool) {
func (Methods[N, Traits]) ToStorage(aggr aggregation.Aggregation) (*State[N, Traits], bool) {
r, ok := aggr.(*State[N, Traits])
return r, ok
}

func (Methods[N, Traits, Storage]) SubtractSwap(operand, argument *State[N, Traits]) {
func (Methods[N, Traits]) SubtractSwap(operand, argument *State[N, Traits]) {
panic("not used for non-temporal metrics")
}
Loading

0 comments on commit aa82048

Please sign in to comment.