private static void ProcessCounterEmit(CounterEmit emit) { // This is an expensive call, so make sure that we only make it once string counterName = emit.GetCounterName(); int hashCode = CounterInstance.GetCounterHashCode(emit.Name, emit.Dimensions); CounterInstance instance; if (!instanceByNameCache.TryGetValue(counterName, out instance)) { // The counter is not in the cache, so we need to get/create is in the database using (var db = new CounterDatabase()) { if (!TryGetInstanceFromDatabase(emit, hashCode, db, out instance)) { CounterInstanceData counterInstanceData = new CounterInstanceData() { Name = emit.Name, InstanceHashCode = hashCode }; if (counterInstanceData.Dimensions == null) { counterInstanceData.Dimensions = new List <CounterDimensionData>(); } foreach (KeyValuePair <string, string> emitDimension in emit.Dimensions) { counterInstanceData.Dimensions.Add( new CounterDimensionData(emitDimension.Key, emitDimension.Value)); } db.Instances.Add(counterInstanceData); db.SaveChanges(); instance = new CounterInstance(counterInstanceData); } // Add the newly created/retrieved counter instance to the cache // TODO: Where does the TTL go??? instanceByNameCache.TryAdd(counterName, instance); } } long ts = Convert.ToInt64(Math.Floor(emit.Timestamp.Subtract(Epoch).TotalSeconds)); CounterValueSet valueSet; // Check if there is a valueSet for this particular timestamp. If not, add one if (!instance.Values.TryGetValue(ts, out valueSet)) { valueSet = new CounterValueSet(emit.Value, 1); instance.Values.Add(ts, valueSet); return; } valueSet.Sum += emit.Value; valueSet.Count++; }
protected override void ProcessRecord() { DateTime epoch = new DateTime(1970, 1, 1); using (var db = new CounterDatabase()) { List <CounterInstanceData> ins = db.Instances .Where(i => i.Id == this.CounterId) .Include(i => i.Dimensions) .ToList(); if (!ins.Any()) { throw new Exception("No counters found with ID " + this.CounterId); } CounterInstanceData instance = ins.First(); foreach (var val in db.Values.Where(v => v.CounterInstanceId == instance.Id)) { SyncProCounterValue temp = new SyncProCounterValue { Count = val.Count, Timestamp = epoch.AddSeconds(val.Timestamp), Value = val.Value, }; this.WriteObject(temp); } } }
public static void Start() { string databasePath = CounterDatabase.GetDatabaseFilePath(); string folderPath = Path.GetDirectoryName(databasePath); // Create the directory containing the counters database (if it does not exist). Pre.Assert(folderPath != null, "folderPath != null"); Directory.CreateDirectory(folderPath); processingTask = Task.Run(() => { ProcessCounterEmitsMain(); }); }
private static void FlushCountersToDatabase() { DateTime now = DateTime.UtcNow; long timestampThreadhold = Convert.ToInt64(Math.Floor(now.Subtract(Epoch).TotalSeconds)) - 2; using (var db = new CounterDatabase()) { bool valuesAdded = false; foreach (string counterName in instanceByNameCache.Keys) { CounterInstance instance = instanceByNameCache[counterName]; // Push any values older than the threshold into the db and remove from the cache List <long> removeList = new List <long>(); foreach (long timestamp in instance.Values.Keys) { if (timestamp < timestampThreadhold) { CounterValueSet valueSet = instance.Values[timestamp]; db.Values.Add( new CounterValueData() { Count = valueSet.Count, CounterInstanceId = instance.Id, Flags = (int)CounterValueFlags.Aggregate1Second, Timestamp = timestamp, Value = valueSet.Sum }); // Remove the value set from the instance, since we already pushed it to the DB removeList.Add(timestamp); valuesAdded = true; } } foreach (long timestamp in removeList) { instance.Values.Remove(timestamp); } if (instance.CacheExpiryDateTime < now && !instance.Values.Any()) { instanceByNameCache.TryRemove(counterName, out CounterInstance _); } } if (valuesAdded) { db.SaveChanges(); } } }
protected override void ProcessRecord() { using (var db = new CounterDatabase()) { var allCounters = db.Instances.Include(i => i.Dimensions).ToList(); foreach (CounterInstanceData counterInstance in allCounters) { SyncProCounterInstance counter = new SyncProCounterInstance { Id = counterInstance.Id, Name = counterInstance.Name, Dimensions = string.Join( "", counterInstance.Dimensions.Select(d => string.Format("[{0}={1}]", d.Name, d.Value))) }; this.WriteObject(counter); } } }
private static bool TryGetInstanceFromDatabase( CounterEmit emit, int emitHashCode, CounterDatabase db, out CounterInstance instance) { var instances = db.Instances .Where(i => i.InstanceHashCode == emitHashCode) .Include(i => i.Dimensions); foreach (CounterInstanceData counterInstance in instances) { if (emit.IsMatch(counterInstance)) { instance = new CounterInstance(counterInstance); return(true); } } instance = null; return(false); }