-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsCollection.cs
More file actions
127 lines (106 loc) · 4.61 KB
/
Copy pathStatsCollection.cs
File metadata and controls
127 lines (106 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
namespace StatsSharp
{
public class StatsCollection : IStatsClient
{
class BucketCollection
{
public readonly ConcurrentDictionary<string, MetricValue> Gauges = new ConcurrentDictionary<string, MetricValue>();
public readonly ConcurrentDictionary<string, ConcurrentBag<MetricValue>> Timers = new ConcurrentDictionary<string, ConcurrentBag<MetricValue>>();
public readonly ConcurrentDictionary<string, long> Counts = new ConcurrentDictionary<string, long>();
readonly StatsCollectionConfig config;
public BucketCollection(StatsCollectionConfig config) {
this.config = config;
}
public StatsSummary Summarize(DateTime timeStamp, TimeSpan flushInterval) {
return new StatsSummary(timeStamp,
SummarizeGauges()
.Concat(SummarizeTimers())
.Concat(SummarizeCounts(flushInterval)).ToArray());
}
IEnumerable<StatsValue> SummarizeGauges() =>
Gauges.Select(item => new StatsValue("stats.gauges." + item.Key, item.Value.AsFloat()));
IEnumerable<StatsValue> SummarizeCounts(TimeSpan flushInterval) {
foreach(var counter in Counts) {
yield return new StatsValue("stats_counts." + counter.Key, counter.Value);
yield return new StatsValue("stats." + counter.Key, counter.Value / flushInterval.TotalSeconds);
}
}
IEnumerable<StatsValue> SummarizeTimers() {
foreach(var timer in Timers) {
var items = timer.Value.Select(x => x.AsFloat()).ToList();
items.Sort();
var prefix = "stats.timers." + timer.Key;
if(config.Percentiles.Count > 0) {
var cumulativeValues = new double[items.Count];
cumulativeValues[0] = items[0];
for (var i = 1; i < items.Count; ++i)
cumulativeValues[i]= items[i] + cumulativeValues[i-1];
var mean = items[0];
var sumAtThreshold = items[items.Count - 1];
var maxAtThreshold = items[items.Count - 1];
var indexScale = items.Count / 100.0;
foreach(var pct in config.Percentiles) {
if(items.Count > 1) {
var thresholdIndex = (100.0 - pct) * indexScale;
var numInThreshold = (int)Math.Round(items.Count - thresholdIndex);
maxAtThreshold = items[numInThreshold - 1];
sumAtThreshold = cumulativeValues[numInThreshold - 1];
mean = sumAtThreshold / numInThreshold;
}
var suffix = pct.ToString(CultureInfo.InvariantCulture).Replace(".", "_");
yield return new StatsValue(prefix + ".mean_" + suffix, mean);
yield return new StatsValue(prefix + ".upper_" + suffix, maxAtThreshold);
yield return new StatsValue(prefix + ".sum_" + suffix, sumAtThreshold);
}
}
var sum = items.Sum();
yield return new StatsValue(prefix + ".upper", items[items.Count - 1]);
yield return new StatsValue(prefix + ".lower", items[0]);
yield return new StatsValue(prefix + ".count", items.Count);
yield return new StatsValue(prefix + ".sum", sum);
yield return new StatsValue(prefix + ".mean", sum / items.Count);
}
}
}
readonly StatsCollectionConfig config;
BucketCollection buckets;
public StatsCollection() : this(new StatsCollectionConfig()) { }
public StatsCollection(StatsCollectionConfig config) {
this.config = config;
this.buckets = new BucketCollection(config);
}
public List<double> Percentiles => config.Percentiles;
public StatsSummary Summarize() => Summarize(DateTime.Now, TimeSpan.FromSeconds(10));
public StatsSummary Summarize(DateTime timestamp, TimeSpan flushInterval) =>
buckets.Summarize(timestamp, flushInterval);
public StatsSummary Flush(DateTime timestamp, TimeSpan flushInterval) =>
Interlocked.Exchange(ref buckets, new BucketCollection(config)).Summarize(timestamp, flushInterval);
void IStatsClient.Send(Metric metric) => SendCore(metric);
void SendCore(Metric metric) {
switch (metric.Value.Type & MetricType.MetricTypeMask) {
default: throw new NotImplementedException();
case MetricType.Gauge:
buckets.Gauges[metric.Name] = metric.Value;
break;
case MetricType.Counter:
buckets.Counts.AddOrUpdate(metric.Name, _ => 1, (_, x) => x + metric.Value.AsInt32());
break;
case MetricType.Time:
buckets.Timers
.GetOrAdd(metric.Name, _ => new ConcurrentBag<MetricValue>())
.Add(metric.Value);
break;
}
}
void IStatsClient.Send(IEnumerable<Metric> metrics) {
foreach(var item in metrics)
SendCore(item);
}
}
}