public StatsdService(IList<IBackend> backends, StatsdServiceConfig config = null) { _backends = backends; _handlers.Add(MetricTypeContants.Counter, HandleCounter); _handlers.Add(MetricTypeContants.Gauge, HandleGauge); _handlers.Add(MetricTypeContants.Set, HandleSet); _handlers.Add(MetricTypeContants.Timer, HandleTimer); _activeSnapshot = new ActiveSnapshot(); _config = config ?? new StatsdServiceConfig(); }
private IMetricsSnapshot Snapshot(ref ActiveSnapshot activeSnapshot) { activeSnapshot.AddGauge(string.Format(StatsdServiceStatConstants.ServiceUptimeFormat, _config.ServiceStatsPrefix), (long)(DateTimeOffset.Now - _serviceStartTimestamp).TotalMilliseconds); var newSnapshot = new StaticSnapshot(); newSnapshot.Counters = new Dictionary<string, long>(activeSnapshot.Counters); newSnapshot.Gauges = new Dictionary<string, long>(activeSnapshot.Gauges); newSnapshot.Sets = new Dictionary<string, SortedSet<long>>(activeSnapshot.Sets); newSnapshot.Timers = new Dictionary<string, List<long>>(activeSnapshot.Timers); if (_config.ClearKeys) { activeSnapshot.Counters.Clear(); activeSnapshot.Sets.Clear(); activeSnapshot.Timers.Clear(); activeSnapshot.TimerCounters.Clear(); // TODO: does DeleteGauges trump ClearKeys? activeSnapshot.Gauges.Clear(); } else { foreach (var key in _activeSnapshot.Counters.Keys) { activeSnapshot.Counters[key] = 0; } if (_config.DeleteGauges) { foreach (var key in activeSnapshot.Gauges.Keys) { activeSnapshot.Gauges[key] = 0; } } foreach (var key in activeSnapshot.Sets.Keys) { activeSnapshot.Sets[key].Clear(); } foreach (var key in activeSnapshot.Timers.Keys) { activeSnapshot.Timers[key].Clear(); activeSnapshot.TimerCounters[key] = 0; } } return newSnapshot; }