-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMetrics.cs
More file actions
217 lines (193 loc) · 11 KB
/
Metrics.cs
File metadata and controls
217 lines (193 loc) · 11 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StackifyLib.Internal.Metrics;
using StackifyLib.Models;
namespace StackifyLib
{
/*
Different types of metrics
Count or Sum of values over a minte
Guage - incremented or decremented - reports the last value
Increment guage function
Average - Reports an average of a sample of values
Rolling average - Could report an average over a longer time span than 1 minute #FUTURE?
Time - Average time someting took in 1 minute
Rolling average time - Rolling average over longer period of time #FUTURE?
### Percentile not currently supported ###
Percentiles in time span - calculated over last X metrics received in a minute
Rolling percentiles - calculated over last X metrics over a longer timepsan
*/
/// <summary>
/// Used to log custom metrics
/// </summary>
public class Metrics
{
public static LatestAggregate GetLatest(string category, string metricName)
{
return MetricClient.GetLatestMetric(category, metricName);
}
public static List<LatestAggregate> GetLatestAllMetrics()
{
return MetricClient.GetLatestMetrics();
}
/// <summary>
/// Guage type metric that reports the last value reported once a minute
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="value">Explicit value to set the metric to</param>
//public static void SetGauge(string category, string metricName, double value, MetricSetting advancedSettings = null)
//{
// var m = new Metric(category, metricName, MetricType.MetricLast) { Value = value, Settings = advancedSettings };
// StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
//}
/// <summary>
/// Guage type metric that reports the last value reported once a minute
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="value">Explicit value to set the metric to</param>
/// <param name="autoResendLastValueIfNothingReported">Every minute resend the last value if nothing reported</param>
public static void SetGauge(string category, string metricName, double value, bool autoResendLastValueIfNothingReported = false)
{
var m = new Metric(category, metricName, MetricType.MetricLast) { Value = value, Settings = new MetricSetting() { AutoReportLastValueIfNothingReported = autoResendLastValueIfNothingReported } };
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
}
/// <summary>
/// Increment or decrement a guage metric type
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="incrementBy">Value can be positive or negative to decrement. Defaults to 1</param>
public static void IncrementGauge(string category, string metricName, double incrementBy, MetricSetting advancedSettings)
{
//leaving the count as 1 below because when it gets processed later it would sum up the count there
var m = new Metric(category, metricName, MetricType.MetricLast);
m.Value = incrementBy;
m.IsIncrement = true;
m.Settings = advancedSettings;
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
}
/// <summary>
/// Increment or decrement a guage metric type
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="incrementBy">Value can be positive or negative to decrement. Defaults to 1</param>
/// <param name="autoResendLastValueIfNothingReported">Every minute resend the last value if nothing reported</param>
public static void IncrementGauge(string category, string metricName, double incrementBy = 1, bool autoResendLastValueIfNothingReported = false)
{
IncrementGauge(category, metricName, incrementBy,
new MetricSetting() {AutoReportLastValueIfNothingReported = autoResendLastValueIfNothingReported});
}
/// <summary>
/// Sums up the values passed in and reports the average of the values once per minute
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="value"></param>
public static void Average(string category, string metricName, double value, MetricSetting advancedSettings = null)
{
var m = new Metric(category, metricName, MetricType.MetricAverage) { Value = value, Settings = advancedSettings };
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
}
/// <summary>
/// Sums up the values passed in and reports the total once per minute
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="value"></param>
public static void Sum(string category, string metricName, double value, MetricSetting advancedSettings = null)
{
var m = new Metric(category, metricName, MetricType.Counter);
m.Value = value;
m.Settings = advancedSettings;
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
}
/// <summary>
/// Counts how many times something happens per minute
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="incrementBy"></param>
/// <param name="autoSendIfZero">If nothing is reported for a minute, should we report a 0?</param>
//public static void Count(string category, string metricName, int incrementBy = 1, MetricSetting advancedSettings = null)
//{
// var m = new Metric(category, metricName, MetricType.Counter);
// m.Value = incrementBy;
// m.Settings = advancedSettings;
// StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
//}
/// <summary>
/// Counts how many times something happens per minute
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="incrementBy"></param>
/// <param name="autoSendIfZero">If nothing is reported for a minute, should we report a 0?</param>
public static void Count(string category, string metricName, int incrementBy = 1, bool autoReportZeroIfNothingReported = false)
{
var m = new Metric(category, metricName, MetricType.Counter);
m.Value = incrementBy;
m.Settings = new MetricSetting() { AutoReportZeroIfNothingReported = autoReportZeroIfNothingReported };
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
}
/// <summary>
/// Report when something started and this type of metric will calculate the average time it takes
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="startTime">When the event being tracked started</param>
public static void Time(string category, string metricName, DateTimeOffset startTime)
{
TimeSpan timeTaken = DateTimeOffset.UtcNow.Subtract(startTime.ToUniversalTime());
Time(category, metricName, timeTaken);
}
/// <summary>
/// Report how long something took to happen and this type of metric will calculate the average time it takes
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="timeTaken">How long the event being tracked took</param>
public static void Time(string category, string metricName, TimeSpan timeTaken)
{
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(new Metric(category, metricName, MetricType.CounterTime) { Value = timeTaken.TotalSeconds});
}
/// <summary>
/// Calculate average time taken and a second metric for how many times it occurred
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="startTime">When the event being tracked started</param>
public static void CountAndTime(string category, string metricName, DateTimeOffset startTime, bool autoReportZeroIfNothingReported = false)
{
TimeSpan timeTaken = DateTimeOffset.UtcNow.Subtract(startTime.ToUniversalTime());
CountAndTime(category, metricName, timeTaken, autoReportZeroIfNothingReported);
}
/// <summary>
/// Calculate average time taken and a second metric for how many times it occurred
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="timeTaken">How long the event being tracked took</param>
//public static void CountAndTime(string category, string metricName, TimeSpan timeTaken, MetricSetting advancedSettings = null)
//{
// StackifyLib.Internal.Metrics.MetricClient.QueueMetric(new Metric(category, metricName, MetricType.Counter) { Value = 1, Settings = advancedSettings});
// StackifyLib.Internal.Metrics.MetricClient.QueueMetric(new Metric(category, metricName + " Time", MetricType.CounterTime) { Value = timeTaken.TotalSeconds, Settings = advancedSettings});
//}
/// <summary>
/// Calculate average time taken and a second metric for how many times it occurred
/// </summary>
/// <param name="category">Category of the metric</param>
/// <param name="metricName">Name of the metric</param>
/// <param name="timeTaken">How long the event being tracked took</param>
public static void CountAndTime(string category, string metricName, TimeSpan timeTaken, bool autoReportZeroIfNothingReported = false)
{
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(new Metric(category, metricName, MetricType.Counter) { Value = 1, Settings = new MetricSetting() {AutoReportZeroIfNothingReported = autoReportZeroIfNothingReported} });
StackifyLib.Internal.Metrics.MetricClient.QueueMetric(new Metric(category, metricName + " Time", MetricType.CounterTime) { Value = timeTaken.TotalSeconds });
}
}
}