-
Notifications
You must be signed in to change notification settings - Fork 816
Add support for calculating and returning user stats #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca4967d
Add per-user ingestion rate computation to ingesters
juliusv 9785f4c
Wire up HTTP interfaces for getting user stats.
juliusv 963a53e
Start UserStats JSON field names lower-cased
juliusv 00ab1bb
Add explanatory comments to ewmaRate type
juliusv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
package ingester | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
// ewmaRate tracks an exponentially weighted moving average of a per-second rate. | ||
type ewmaRate struct { | ||
newEvents int64 | ||
alpha float64 | ||
interval time.Duration | ||
lastRate float64 | ||
init bool | ||
mutex sync.Mutex | ||
} | ||
|
||
func newEWMARate(alpha float64, interval time.Duration) *ewmaRate { | ||
return &ewmaRate{ | ||
alpha: alpha, | ||
interval: interval, | ||
} | ||
} | ||
|
||
// rate returns the per-second rate. | ||
func (r *ewmaRate) rate() float64 { | ||
r.mutex.Lock() | ||
defer r.mutex.Unlock() | ||
return r.lastRate | ||
} | ||
|
||
// tick assumes to be called every r.interval. | ||
func (r *ewmaRate) tick() { | ||
newEvents := atomic.LoadInt64(&r.newEvents) | ||
atomic.AddInt64(&r.newEvents, -newEvents) | ||
instantRate := float64(newEvents) / r.interval.Seconds() | ||
|
||
r.mutex.Lock() | ||
defer r.mutex.Unlock() | ||
|
||
if r.init { | ||
r.lastRate += r.alpha * (instantRate - r.lastRate) | ||
} else { | ||
r.init = true | ||
r.lastRate = instantRate | ||
} | ||
} | ||
|
||
// inc counts one event. | ||
func (r *ewmaRate) inc() { | ||
atomic.AddInt64(&r.newEvents, 1) | ||
} |
This file contains hidden or 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,36 @@ | ||
package ingester | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRate(t *testing.T) { | ||
ticks := []struct { | ||
events int | ||
want float64 | ||
}{ | ||
{60, 1}, | ||
{30, 0.9}, | ||
{0, 0.72}, | ||
{60, 0.776}, | ||
{0, 0.6208}, | ||
{0, 0.49664}, | ||
{0, 0.397312}, | ||
{0, 0.3178496}, | ||
{0, 0.25427968}, | ||
{0, 0.20342374400000002}, | ||
{0, 0.16273899520000001}, | ||
} | ||
r := newEWMARate(0.2, time.Minute) | ||
|
||
for i, tick := range ticks { | ||
for e := 0; e < tick.events; e++ { | ||
r.inc() | ||
} | ||
r.tick() | ||
if r.rate() != tick.want { | ||
t.Fatalf("%d. unexpected rate: want %v, got %v", i, tick.want, r.rate()) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's "ewma"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry - exponentially weighted moving average (https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some comments to the type and its methods.