Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Add TypeFor* APIs to aggregation options
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Jun 14, 2018
1 parent e36fd11 commit 278506a
Show file tree
Hide file tree
Showing 2 changed files with 296 additions and 94 deletions.
34 changes: 34 additions & 0 deletions aggregation/types_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package aggregation

import (
"bytes"
"strconv"
"strings"

Expand Down Expand Up @@ -103,6 +104,15 @@ type TypesOptions interface {
// TypeStringForGauge returns the type string for the aggregation type for gauges.
TypeStringForGauge(value Type) []byte

// TypeForCounter returns the aggregation type for given counter type string.
TypeForCounter(value []byte) Type

// TypeForTimer returns the aggregation type for given timer type string.
TypeForTimer(value []byte) Type

// TypeForGauge returns the aggregation type for given gauge type string.
TypeForGauge(value []byte) Type

// Quantiles returns the quantiles for timers.
Quantiles() []float64

Expand Down Expand Up @@ -296,6 +306,18 @@ func (o *options) TypeStringForGauge(aggType Type) []byte {
return o.gaugeTypeStrings[aggType.ID()]
}

func (o *options) TypeForCounter(value []byte) Type {
return typeFor(value, o.counterTypeStrings)
}

func (o *options) TypeForTimer(value []byte) Type {
return typeFor(value, o.timerTypeStrings)
}

func (o *options) TypeForGauge(value []byte) Type {
return typeFor(value, o.gaugeTypeStrings)
}

func (o *options) Quantiles() []float64 {
return o.quantiles
}
Expand Down Expand Up @@ -354,6 +376,18 @@ func (o *options) computeTypeStrings(transformFn TypeStringTransformFn) [][]byte
return res
}

func typeFor(value []byte, typeStrings [][]byte) Type {
for id, typeString := range typeStrings {
if !bytes.Equal(value, typeString) {
continue
}
if t := Type(id); t.IsValid() {
return t
}
}
return UnknownType
}

// By default we use e.g. "p50", "p95", "p99" for the 50th/95th/99th percentile.
func defaultQuantileTypeStringFn(quantile float64) []byte {
str := strconv.FormatFloat(quantile*100, 'f', -1, 64)
Expand Down
Loading

0 comments on commit 278506a

Please sign in to comment.