Example #1
0
        public void Tick(int add = 1)
        {
            MeterAggregator currAggregator = _aggregator;

            if (currAggregator != null)
            {
                currAggregator.Tick(add);
            }
        }
Example #2
0
        public Meter(string name, ITelemetrySink[] sinks, TimeSpan?aggregationPeriod = null, Int32 historyKeep = 30, IDictionary <string, string> properties = null)
        {
            if (aggregationPeriod == TimeSpan.Zero)
            {
                throw new ArgumentException("Aggregation Period can't be zero", "aggregationPeriod");
            }


            _aggregationPeriod = aggregationPeriod ?? TimeSpan.FromSeconds(60);

            this.Name          = name ?? "null";
            this._aggregator   = new MeterAggregator(DateTimeOffset.UtcNow);
            this._sinks        = sinks ?? throw new ArgumentNullException(nameof(sinks));
            this._historyCount = historyKeep;
            this._properties   = properties;

            Task.Run(this.AggregatorLoopAsync);
        }
Example #3
0
 public MeterDataPoint(MeterAggregator agg, TimeSpan period)
 {
     this.Count          = agg.Count;
     this.StartTimestamp = agg.StartTimestamp;
     this.Period         = period;
 }
Example #4
0
 void IDisposable.Dispose()
 {
     _isDisposed = true;
     _aggregator = null;
 }
Example #5
0
        private async Task AggregatorLoopAsync()
        {
            while (_isDisposed == false)
            {
                try
                {
                    // Wait for end end of the aggregation period:
                    await Task.Delay(_aggregationPeriod).ConfigureAwait(continueOnCapturedContext: false);

                    if (_isDisposed)
                    {
                        break;
                    }

                    // Atomically snap the current aggregation:
                    MeterAggregator nextAggregator = new MeterAggregator(DateTimeOffset.UtcNow);

                    if (_aggregator == null)
                    {
                        _aggregator = nextAggregator;
                        continue;
                    }

                    MeterAggregator prevAggregator = Interlocked.Exchange(ref _aggregator, nextAggregator);


                    // Only send anything if at least one value was measured:
                    if (prevAggregator != null)
                    {
                        // Compute the actual aggregation period length:
                        TimeSpan aggPeriod = nextAggregator.StartTimestamp - prevAggregator.StartTimestamp;

                        History.AddFirst(new MeterDataPoint(prevAggregator, aggPeriod));
                        if (History.Count > _historyCount)
                        {
                            for (var i = 0; i < History.Count - _historyCount; i++)
                            {
                                History.RemoveLast();
                            }
                        }

                        //don't report empty meters
                        if (aggPeriod.TotalMilliseconds > 0 && prevAggregator.Count > 0)
                        {
                            foreach (var sink in _sinks)
                            {
                                try
                                {
                                    sink.TrackMeter(
                                        Name,
                                        prevAggregator.Count,
                                        aggPeriod,
                                        prevAggregator.StartTimestamp,
                                        _properties
                                        );
                                }catch (Exception ex)
                                {
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }