public void Process(Event ev, Instance instance) { _monik.Measure(TotalMessages, AggregationType.Accumulator, 1); switch (ev.MsgCase) { case Event.MsgOneofCase.None: throw new NotSupportedException("Bad event type"); case Event.MsgOneofCase.Ka: _monik.Measure(KeepAliveCount, AggregationType.Accumulator, 1); var ka = CreateKeepAlive(ev, instance); _cacheKeepAlive.Add(ka); break; case Event.MsgOneofCase.Lg: _monik.Measure(LogCount, AggregationType.Accumulator, 1); var lg = CreateLog(ev, instance); _cacheLog.Add(lg); break; case Event.MsgOneofCase.Mc: _monik.Measure(MeasureCount, AggregationType.Accumulator, 1); _cacheMetric.OnNewMeasure(instance, ev); break; default: throw new NotSupportedException("Bad event type"); } }
public void OnStart() { // Create context for ActiveQueues var context = new ActiveQueueContext { OnError = (errorMessage) => { _monik.ApplicationError(errorMessage); Thread.Sleep(DelayOnException); }, OnVerbose = (verboseMessage) => { _monik.ApplicationVerbose(verboseMessage); }, OnMeasure = (metricName, value) => { _monik.Measure(metricName, AggregationType.Gauge, value); _monik.Measure(metricName + "All", AggregationType.Accumulator, value); }, OnReceivedMessage = (msg) => { _msgBuffer.Enqueue(msg); _newMessageEvent.Set(); }, OnReceivedMessages = (messages) => { foreach (var msg in messages) { _msgBuffer.Enqueue(msg); } _newMessageEvent.Set(); } }; if (_queueReaderSettings != null) { foreach (var it in _queueReaderSettings) { try { var queue = CreateActiveQueueByType(it.Type); if (queue != null) { _queues.Add(queue); queue.Start(it, context); } else { _monik.ApplicationWarning( $"MessagePump.OnStart cannot initialize {it.Name}: unknown type {it.Type}"); } } catch (Exception ex) { _monik.ApplicationError($"MessagePump.OnStart failed initialization {it.Name}: {ex.Message}"); } } //configure all event sources } // Start message processing _pumpTask = Task.Run(OnProcessTask); _monik.ApplicationVerbose("MessagePump started"); }
public void OnNewMeasure(Event metric) { var metTime = DateTimeOffset.FromUnixTimeMilliseconds(metric.Created).UtcDateTime; lock (this) { var actualIntervalStart = _dto.ActualInterval.AddMinutes(-5); var actualIntervalEnd = _dto.ActualInterval; // interval with epsilon var intervalStart = actualIntervalStart.AddMilliseconds(-IntervalEpsilon); var intervalEnd = actualIntervalEnd.AddMilliseconds(IntervalEpsilon); if ((metTime < intervalStart || metTime > intervalEnd) && (metric.Mc.Name.ToLower() != "outtimemeasure")) { // skip event // increase skip metric _monik.Measure("OutTimeMeasure", AggregationType.Accumulator, 1); var ev = NewMeasureEvent(metric.Source, metric.Instance, "OutTimeMeasure", 1, AggregationType.Accumulator); _pump.OnEmbeddedEvents(new Event[1] { ev }); var serverTime = DateTime.UtcNow; var diffInterval = metTime < intervalStart ? (metTime - actualIntervalStart).TotalMilliseconds : (metTime - actualIntervalEnd).TotalMilliseconds; var diffServer = (serverTime - metTime).TotalMilliseconds; _monik.LogicVerbose($@"[OutTime] {metric.Source}.{metric.Instance}::{metric.Mc.Name}, lag:{diffServer}, lagInterval:{diffInterval}, {metric.Created} not in [{new DateTimeOffset(actualIntervalStart).ToUnixTimeMilliseconds()},{new DateTimeOffset(actualIntervalEnd).ToUnixTimeMilliseconds()}), now:{new DateTimeOffset(serverTime).ToUnixTimeMilliseconds()}"); return; } var actualMeasure = GetMeasure(_dto.ActualID); switch (metric.Mc.Aggregation) { case AggregationType.Accumulator: if (actualMeasure.HasValue) { actualMeasure.Value += metric.Mc.Value; } else { actualMeasure.Value = metric.Mc.Value; actualMeasure.HasValue = true; } AccumWindowCalculator accWin = window as AccumWindowCalculator; if (accWin == null) { window = new AccumWindowCalculator(); } window.OnNewValue(metric.Mc.Value); break; case AggregationType.Gauge: if (actualMeasure.HasValue) { actualMeasure.Value = (actualMeasure.Value + metric.Mc.Value) / 2; } else { actualMeasure.Value = metric.Mc.Value; actualMeasure.HasValue = true; } GaugeWindowCalculator gauWin = window as GaugeWindowCalculator; if (gauWin == null) { window = new GaugeWindowCalculator(); } window.OnNewValue(metric.Mc.Value); break; default: // skip event // increase skip metric break; } }//lock }
public void EndAndMeasure(string metricName) { var delta = DateTime.Now - _from; _monik.Measure(metricName, AggregationType.Gauge, delta.TotalMilliseconds); }