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++; }
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); }