-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a TotalThroughputSampler. (#185)
* Add a TotalThroughputSampler to try to avoid being rate limited when we hit sudden peaks
- Loading branch information
1 parent
c621909
commit 96843de
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package sample | ||
|
||
import ( | ||
"math/rand" | ||
|
||
dynsampler "github.com/honeycombio/dynsampler-go" | ||
|
||
"github.com/honeycombio/refinery/config" | ||
"github.com/honeycombio/refinery/logger" | ||
"github.com/honeycombio/refinery/metrics" | ||
"github.com/honeycombio/refinery/types" | ||
) | ||
|
||
type TotalThroughputSampler struct { | ||
Config *config.TotalThroughputSamplerConfig | ||
Logger logger.Logger | ||
Metrics metrics.Metrics | ||
|
||
goalThroughputPerSec int64 | ||
clearFrequencySec int64 | ||
|
||
key *traceKey | ||
|
||
dynsampler dynsampler.Sampler | ||
} | ||
|
||
func (d *TotalThroughputSampler) Start() error { | ||
d.Logger.Debug().Logf("Starting TotalThroughputSampler") | ||
defer func() { d.Logger.Debug().Logf("Finished starting TotalThroughputSampler") }() | ||
if d.Config.GoalThroughputPerSec < 1 { | ||
d.Logger.Debug().Logf("configured sample rate for dynamic sampler was %d; forcing to 100", d.Config.GoalThroughputPerSec) | ||
d.Config.GoalThroughputPerSec = 100 | ||
} | ||
d.goalThroughputPerSec = d.Config.GoalThroughputPerSec | ||
if d.Config.ClearFrequencySec == 0 { | ||
d.Config.ClearFrequencySec = 30 | ||
} | ||
d.clearFrequencySec = d.Config.ClearFrequencySec | ||
d.key = newTraceKey(d.Config.FieldList, d.Config.UseTraceLength, d.Config.AddSampleRateKeyToTrace, d.Config.AddSampleRateKeyToTraceField) | ||
|
||
// spin up the actual dynamic sampler | ||
d.dynsampler = &dynsampler.TotalThroughput{ | ||
GoalThroughputPerSec: int(d.goalThroughputPerSec), | ||
ClearFrequencySec: int(d.clearFrequencySec), | ||
} | ||
d.dynsampler.Start() | ||
|
||
// Register stastics this package will produce | ||
d.Metrics.Register("dynsampler_num_dropped", "counter") | ||
d.Metrics.Register("dynsampler_num_kept", "counter") | ||
d.Metrics.Register("dynsampler_sample_rate", "histogram") | ||
|
||
return nil | ||
} | ||
|
||
func (d *TotalThroughputSampler) GetSampleRate(trace *types.Trace) (uint, bool) { | ||
key := d.key.buildAndAdd(trace) | ||
rate := d.dynsampler.GetSampleRate(key) | ||
if rate < 1 { // protect against dynsampler being broken even though it shouldn't be | ||
rate = 1 | ||
} | ||
shouldKeep := rand.Intn(int(rate)) == 0 | ||
d.Logger.Debug().WithFields(map[string]interface{}{ | ||
"sample_key": key, | ||
"sample_rate": rate, | ||
"sample_keep": shouldKeep, | ||
"trace_id": trace.TraceID, | ||
}).Logf("got sample rate and decision") | ||
if shouldKeep { | ||
d.Metrics.IncrementCounter("dynsampler_num_kept") | ||
} else { | ||
d.Metrics.IncrementCounter("dynsampler_num_dropped") | ||
} | ||
d.Metrics.Histogram("dynsampler_sample_rate", float64(rate)) | ||
return uint(rate), shouldKeep | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// +build all race | ||
|
||
package sample | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/honeycombio/refinery/config" | ||
"github.com/honeycombio/refinery/logger" | ||
"github.com/honeycombio/refinery/metrics" | ||
"github.com/honeycombio/refinery/types" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTotalThroughputAddSampleRateKeyToTrace(t *testing.T) { | ||
const spanCount = 5 | ||
|
||
metrics := metrics.MockMetrics{} | ||
metrics.Start() | ||
|
||
sampler := &TotalThroughputSampler{ | ||
Config: &config.TotalThroughputSamplerConfig{ | ||
FieldList: []string{"http.status_code"}, | ||
AddSampleRateKeyToTrace: true, | ||
AddSampleRateKeyToTraceField: "meta.key", | ||
}, | ||
Logger: &logger.NullLogger{}, | ||
Metrics: &metrics, | ||
} | ||
|
||
trace := &types.Trace{} | ||
for i := 0; i < spanCount; i++ { | ||
trace.AddSpan(&types.Span{ | ||
Event: types.Event{ | ||
Data: map[string]interface{}{ | ||
"http.status_code": "200", | ||
}, | ||
}, | ||
}) | ||
} | ||
sampler.Start() | ||
sampler.GetSampleRate(trace) | ||
|
||
spans := trace.GetSpans() | ||
assert.Len(t, spans, spanCount, "should have the same number of spans as input") | ||
for _, span := range spans { | ||
assert.Equal(t, span.Event.Data, map[string]interface{}{ | ||
"http.status_code": "200", | ||
"meta.key": "200•,", | ||
}, "should add the sampling key to all spans in the trace") | ||
} | ||
} |