コード例 #1
0
        public void ApplicationMetricsHandle(EnumRequestResponse rr)
        {
            switch (rr)
            {
            case EnumRequestResponse.Request:
                CounterMetrics.IncrementActiveRequests();

                break;

            case EnumRequestResponse.Response:
                CounterMetrics.DecrementActiveRequests();

                if (_context.Response.StatusCode.ToString().StartsWith("4"))
                {
                    CounterMetrics.IncrementError4xx();
                }
                else if (_context.Response.StatusCode.ToString().StartsWith("5"))
                {
                    CounterMetrics.IncrementError5xx();
                }
                break;

            default:
                break;
            }
        }
コード例 #2
0
        public ActionResult <CounterMetrics> GetDiagnostics()
        {
            var now = DateTime.UtcNow;

            _process.Refresh();

            var cpuElapsedTime = now.Subtract(_lastMonitorTime).TotalMilliseconds;

            if (cpuElapsedTime > RefreshRate)
            {
                var newCPUTime = _process.TotalProcessorTime;
                var elapsedCPU = (newCPUTime - _oldCPUTime).TotalMilliseconds;
                _cpu = elapsedCPU * 100 / Environment.ProcessorCount / cpuElapsedTime;

                _lastMonitorTime = now;
                _oldCPUTime      = newCPUTime;
            }

            var rpsElapsedTime = now.Subtract(_lastRpsTime).TotalMilliseconds;

            if (rpsElapsedTime > RefreshRate)
            {
                _rps = Requests * 1000 / rpsElapsedTime;
                Interlocked.Exchange(ref Requests, 0);
                _lastRpsTime = now;
            }

            var diagnostics = new CounterMetrics
            {
                PID = _process.Id,

                // The memory occupied by objects.
                Allocated      = GC.GetTotalMemory(false),
                TotalAllocated = GC.GetTotalAllocatedBytes(false),

                // The working set includes both shared and private data. The shared data includes the pages that contain all the
                // instructions that the process executes, including instructions in the process modules and the system libraries.
                WorkingSet = _process.WorkingSet64,

                // The value returned by this property represents the current size of memory used by the process, in bytes, that
                // cannot be shared with other processes.
                PrivateBytes = _process.PrivateMemorySize64,

                // The number of generation 0 collections
                Gen0 = GC.CollectionCount(0),

                // The number of generation 1 collections
                Gen1 = GC.CollectionCount(1),

                // The number of generation 2 collections
                Gen2 = GC.CollectionCount(2),

                CPU = _cpu,

                RPS = _rps
            };

            return(diagnostics);
        }
        private static double ComputeRejectionProbability(CounterMetrics metrics, AdaptiveThrottlingOptions options)
        {
            var probability = 1.0 * (metrics.Requests - options.CriticalRatio * metrics.Accepts) / (metrics.Requests + 1);

            probability = Math.Max(probability, 0.0);
            probability = Math.Min(probability, options.MaximumRejectProbability);

            return(probability);
        }
            public CounterMetrics GetMetrics()
            {
                var metrics = new CounterMetrics();
                var minute  = GetCurrentMinute();

                foreach (var bucket in buckets)
                {
                    if (bucket.Minute <= minute - buckets.Length)
                    {
                        continue;
                    }

                    metrics.Requests += bucket.Requests;
                    metrics.Accepts  += bucket.Accepts;
                }

                return(metrics);
            }
 private static double ComputeRatio(CounterMetrics metrics)
 {
     return(1.0 * metrics.Requests / Math.Max(1.0, metrics.Accepts));
 }
コード例 #6
0
 private static double ComputeRatio(CounterMetrics metrics) =>
 1.0 * metrics.Replicas / Math.Max(1.0, metrics.Requests);