Exemple #1
0
        public MetricsSnapshot GetSnapshot()
        {
            var lastSnapshotTime = _lastSnapshotTimestamp;
            var currentTime      = _clock.GetMillisecondTimestamp();

            if (_lastSnapshot == null || currentTime - lastSnapshotTime > _config.GetSnapshotTtlMillis(_key))
            {
                // Try to update the _lastSnapshotTimestamp. If we update it, this thread will take on the authority of updating
                // the snapshot. CompareExchange returns the original result, so if it's different from currentTime, we successfully exchanged.
                if (Interlocked.CompareExchange(ref _lastSnapshotTimestamp, currentTime, _lastSnapshotTimestamp) != currentTime)
                {
                    // TODO rob.hruska 11/8/2013 - May be inaccurate if counts are incremented as we're querying these.
                    var success = _resettingNumbersBucket.GetCount(CounterMetric.CommandSuccess);
                    var failure = _resettingNumbersBucket.GetCount(CounterMetric.CommandFailure);
                    var total   = success + failure;

                    int errorPercentage;
                    if (total == 0)
                    {
                        errorPercentage = 0;
                    }
                    else
                    {
                        errorPercentage = (int)(success == 0 ? 100 : (failure / (double)total) * 100);
                    }

                    _lastSnapshot = new MetricsSnapshot(total, errorPercentage);
                }
            }

            return(_lastSnapshot);
        }
        public MetricsSnapshot GetSnapshot()
        {
            var stopwatch = Stopwatch.StartNew();
            try
            {
                var lastSnapshotTime = _lastSnapshotTimestamp;
                var currentTime = _clock.GetMillisecondTimestamp();

                if (_lastSnapshot == null || currentTime - lastSnapshotTime > _snapshotTtlMillis.Value)
                {
                    // Try to update the _lastSnapshotTimestamp. If we update it, this thread will take on the authority of updating
                    // the snapshot. CompareExchange returns the original result, so if it's different from currentTime, we successfully exchanged.
                    if (Interlocked.CompareExchange(ref _lastSnapshotTimestamp, currentTime, _lastSnapshotTimestamp) != currentTime)
                    {
                        var createwatch = Stopwatch.StartNew();
                        try
                        {
                            // TODO rob.hruska 11/8/2013 - May be inaccurate if counts are incremented as we're querying these.
                            var success = _resettingNumbersBucket.GetCount(CounterMetric.CommandSuccess);
                            var failure = _resettingNumbersBucket.GetCount(CounterMetric.CommandFailure);
                            var total = success + failure;

                            int errorPercentage;
                            if (total == 0)
                            {
                                errorPercentage = 0;
                            }
                            else
                            {
                                errorPercentage = (int) (success == 0 ? 100 : (failure / (double) total) * 100);
                            }

                            _lastSnapshot = new MetricsSnapshot(total, errorPercentage);
                        }
                        finally
                        {
                            createwatch.Stop();
                            _stats.Elapsed(StatsPrefix + " CreateSnapshot", null, createwatch.Elapsed);
                        }
                    }
                }

                return _lastSnapshot;
            }
            finally
            {
                _stats.Elapsed(StatsPrefix + " GetSnapshot", null, stopwatch.Elapsed);
            }
        }