private async Task HandleContextAsync(HttpListenerContext listenerContext)
        {
            //request handling
            using (_processingTimer.NewContext())
            {
                var time = ValueReader.GetCurrentValue(_processingTimer)
                           .Scale(TimeUnit.Milliseconds, TimeUnit.Milliseconds)
                           .Histogram.Percentile95;

                if (time < _acceptableTimeOfService)
                {
                    string paramString = listenerContext.Request.RawUrl;
                    if (listenerContext.Request.HttpMethod == "POST")
                    {
                        if (paramString.StartsWith("/process/"))
                        {
                            var result = ProcessImage(listenerContext);
                            using (var writer = new BinaryWriter(result.Item1.Response.OutputStream))
                                writer.Write(result.Item2);
                            return;
                        }
                    }

                    listenerContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    using (var writer = new StreamWriter(listenerContext.Response.OutputStream))
                        await writer.WriteLineAsync();
                }
                else
                {
                    listenerContext.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
                    using (var writer = new StreamWriter(listenerContext.Response.OutputStream))
                        await writer.WriteLineAsync();
                }
            }
        }
Example #2
0
        public SampleMetrics()
        {
            // define a simple gauge that will provide the instant value of this.someValue when requested
            Metric.Gauge("SampleMetrics.DataValue", () => this.someValue, Unit.Custom("$"));

            Metric.Gauge("Custom Ratio", () => ValueReader.GetCurrentValue(totalRequestsCounter).Count / ValueReader.GetCurrentValue(meter).FiveMinuteRate, Unit.Percent);

            Metric.Advanced.Gauge("Ratio", () => new HitRatioGauge(meter, timer, m => m.OneMinuteRate), Unit.Percent);
        }
Example #3
0
        public SampleMetrics(IMetrics metrics)
        {
            _metrics = metrics;
            _concurrentRequestsCounter = _metrics.Advanced.Counter(SampleMetricsRegistry.Counters.ConcurrentRequestsCounter);
            _histogramOfData           = _metrics.Advanced.Histogram(SampleMetricsRegistry.Histograms.ResultsExample);
            _meter                = _metrics.Advanced.Meter(SampleMetricsRegistry.Meters.Requests);
            _setCounter           = _metrics.Advanced.Counter(SampleMetricsRegistry.Counters.SetCounter);
            _setMeter             = _metrics.Advanced.Meter(SampleMetricsRegistry.Meters.SetMeter);
            _timer                = _metrics.Advanced.Timer(SampleMetricsRegistry.Timers.Requests);
            _totalRequestsCounter = _metrics.Advanced.Counter(SampleMetricsRegistry.Counters.Requests);

            // define a simple gauge that will provide the instant value of someValue when requested
            _metrics.Gauge(SampleMetricsRegistry.Gauges.DataValue, () => _someValue);

            _metrics.Gauge(SampleMetricsRegistry.Gauges.CustomRatioGauge,
                           () => ValueReader.GetCurrentValue(_totalRequestsCounter).Count / ValueReader.GetCurrentValue(_meter).FiveMinuteRate);

            _metrics.Advanced.Gauge(SampleMetricsRegistry.Gauges.Ratio, () => new HitRatioGauge(_meter, _timer, m => m.OneMinuteRate));
        }
Example #4
0
 /// <summary>
 /// Creates a new HitRatioGauge with externally tracked Meters, and uses the provided meter rate function to extract the value for the ratio.
 /// </summary>
 /// <param name="hitMeter">The numerator meter to use for the ratio.</param>
 /// <param name="totalMeter">The denominator meter to use for the ratio.</param>
 /// <param name="meterRateFunc">The function to extract a value from the MeterValue. Will be applied to both the numerator and denominator meters.</param>
 public HitRatioGauge(Meter hitMeter, Meter totalMeter, Func <MeterValue, double> meterRateFunc)
     : base(() => meterRateFunc(ValueReader.GetCurrentValue(hitMeter)), () => meterRateFunc(ValueReader.GetCurrentValue(totalMeter)))
 {
 }
Example #5
0
 /// <summary>
 ///     Creates a new HitPercentageGauge with externally tracked Meter and Timer, and uses the provided meter rate function
 ///     to extract the value for the percentage.
 /// </summary>
 /// <param name="hitMeter">The numerator meter to use.</param>
 /// <param name="totalTimer">The denominator timer to use.</param>
 /// <param name="meterRateFunc">
 ///     The function to extract a value from the MeterValue. Will be applied to both the numerator
 ///     and denominator meters.
 /// </param>
 public HitPercentageGauge(IMeter hitMeter, ITimer totalTimer, Func <MeterValue, double> meterRateFunc)
     : base(() => meterRateFunc(ValueReader.GetCurrentValue(hitMeter)), () => meterRateFunc(ValueReader.GetCurrentValue(totalTimer).Rate))
 {
 }