Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Merge branch 'christian/fixsamplermetric'
Browse files Browse the repository at this point in the history
  • Loading branch information
ufoot committed Mar 20, 2017
2 parents db1fa8c + 92f5107 commit 73a2c4f
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions agent/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"sync"
"time"

log "github.com/cihub/seelog"

Expand All @@ -15,16 +16,17 @@ type Sampler struct {
mu sync.Mutex
sampledTraces []model.Trace
traceCount int
lastFlush time.Time

samplerEngine SamplerEngine
}

// samplerStats contains sampler statistics
type samplerStats struct {
// TracesKept is the number of traces kept (last flush only)
TracesKept int
// TracesTotal is the total number of traces (last flush only)
TracesTotal int
// KeptTPS is the number of traces kept (average per second for last flush)
KeptTPS float64
// TotalTPS is the total number of traces (average per second for last flush)
TotalTPS float64
}

type samplerInfo struct {
Expand Down Expand Up @@ -73,25 +75,31 @@ func (s *Sampler) Stop() {
// Flush returns representative spans based on GetSamples and reset its internal memory
func (s *Sampler) Flush() []model.Trace {
s.mu.Lock()

traces := s.sampledTraces
s.sampledTraces = []model.Trace{}
traceCount := s.traceCount
s.traceCount = 0

now := time.Now()
duration := now.Sub(s.lastFlush)
s.lastFlush = now

s.mu.Unlock()

state := s.samplerEngine.(*sampler.Sampler).GetState()
var stats samplerStats
if duration > 0 {
stats.KeptTPS = float64(len(traces)) / duration.Seconds()
stats.TotalTPS = float64(traceCount) / duration.Seconds()
}

log.Debugf("flushed %d sampled traces out of %v", len(traces), traceCount)
log.Debugf("flushed %d sampled traces out of %d", len(traces), traceCount)
log.Debugf("inTPS: %f, outTPS: %f, maxTPS: %f, offset: %f, slope: %f, cardinality: %d",
state.InTPS, state.OutTPS, state.MaxTPS, state.Offset, state.Slope, state.Cardinality)

// publish through expvar
updateSamplerInfo(samplerInfo{
Stats: samplerStats{
TracesKept: len(traces),
TracesTotal: traceCount,
},
State: state})
updateSamplerInfo(samplerInfo{Stats: stats, State: state})

return traces
}

0 comments on commit 73a2c4f

Please sign in to comment.