Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move metrics ticker to log pkg for #67
- Loading branch information
Showing
3 changed files
with
84 additions
and
54 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package log | ||
|
||
import ( | ||
"container/list" | ||
"expvar" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// TickerFunc is the type of metrics function accepted by AddTickerFunc | ||
type TickerFunc func() | ||
|
||
var tickerFuncChan = make(chan TickerFunc) | ||
|
||
func init() { | ||
go metricsTicker() | ||
} | ||
|
||
// AddTickerFunc adds a new function callback to the list of metrics TickerFuncs that get | ||
// called each minute. | ||
func AddTickerFunc(f TickerFunc) { | ||
tickerFuncChan <- f | ||
} | ||
|
||
// PushMetric adds the metric to the end of the list and returns a comma separated string of the | ||
// previous 61 entries. We return 61 instead of 60 (an hour) because the chart on the client | ||
// tracks deltas between these values - there is nothing to compare the first value against. | ||
func PushMetric(history *list.List, ev expvar.Var) string { | ||
history.PushBack(ev.String()) | ||
if history.Len() > 61 { | ||
history.Remove(history.Front()) | ||
} | ||
return joinStringList(history) | ||
} | ||
|
||
// joinStringList joins a List containing strings by commas | ||
func joinStringList(listOfStrings *list.List) string { | ||
if listOfStrings.Len() == 0 { | ||
return "" | ||
} | ||
s := make([]string, 0, listOfStrings.Len()) | ||
for e := listOfStrings.Front(); e != nil; e = e.Next() { | ||
s = append(s, e.Value.(string)) | ||
} | ||
return strings.Join(s, ",") | ||
} | ||
|
||
func metricsTicker() { | ||
funcs := make([]TickerFunc, 0) | ||
ticker := time.NewTicker(time.Minute) | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
for _, f := range funcs { | ||
f() | ||
} | ||
case f := <-tickerFuncChan: | ||
funcs = append(funcs, f) | ||
} | ||
} | ||
} |
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