public void Request_timestamp_is_set_on_request_start()
        {
            var observer = new AspNetMetricsObserver(new AspNetMetricsOptions(), _metrics);

            observer.OnHttpRequestStarted(_httpContext);
            _httpContext.GetRequestDuration().ShouldNotBe(TimeSpan.Zero);
        }
        public void Sli_exceptions_set_sli_error()
        {
            var observer = new AspNetMetricsObserver(new AspNetMetricsOptions(), _metrics);

            observer.OnUnhandledException(_httpContext, new SliException(ErrorType.InternalDependency, "dep"));

            _httpContext.HasError(out var error).ShouldBeTrue();
            error.ShouldNotBeNull();
            error.Value.Item1.ShouldBe(ErrorType.InternalDependency);
            error.Value.Item2.ShouldBe("dep");
        }
        public void Can_configure_request_duration_histogram()
        {
            bool invoked = false;
            var  options = new AspNetMetricsOptions
            {
                ConfigureRequestDurationHistogram = config => invoked = true
            };

            _ = new AspNetMetricsObserver(options, _metrics);
            invoked.ShouldBeTrue();
        }
        public void Can_configure_requests_in_progress_gauge()
        {
            bool invoked = false;
            var  options = new AspNetMetricsOptions
            {
                ConfigureRequestsInProgressGauge = config => invoked = true
            };

            _ = new AspNetMetricsObserver(options, _metrics);
            invoked.ShouldBeTrue();
        }
        public void Can_configure_error_total_counter()
        {
            bool invoked = false;
            var  options = new AspNetMetricsOptions
            {
                ConfigureErrorTotalCounter = config => invoked = true
            };

            _ = new AspNetMetricsObserver(options, _metrics);
            invoked.ShouldBeTrue();
        }
        public void With_prometheus_metrics()
        {
            _httpContext.SetOperation("op");

            var observer = new AspNetMetricsObserver(new AspNetMetricsOptions(), new PrometheusMetrics());

            observer.OnHttpRequestStarted(_httpContext);
            observer.OnEndpointMatched(_httpContext);
            _httpContext.Response.StatusCode = 200;
            observer.OnHttpRequestCompleted(_httpContext);
        }
        public void Can_configure_request_duration_summary()
        {
            bool invoked = false;
            var  options = new AspNetMetricsOptions
            {
                ConfigureRequestDurationSummary = config => invoked = true,
                RequestDurationMetricType       = ObserverMetricType.Summary
            };

            _ = new AspNetMetricsObserver(options, _metrics);
            invoked.ShouldBeTrue();
        }
        public void Sli_errors_increment_counter()
        {
            _httpContext.SetSliError(ErrorType.InternalDependency, "client-service");
            _httpContext.SetOperation("error-op");

            var observer = new AspNetMetricsObserver(new AspNetMetricsOptions(), _metrics);

            observer.OnHttpRequestStarted(_httpContext);
            observer.OnHttpRequestCompleted(_httpContext);

            var metric = _metrics.GetMetric <ICounter>("http_server_errors_total");

            metric.ShouldNotBeNull();
            metric.Collector.Verify(x => x.WithLabels("error-op", "internal_dependency", "client-service"), Times.Once);
            metric.Child.Verify(x => x.Inc(1), Times.Once);
        }
        public void Server_errors_increment_error_counter()
        {
            _httpContext.SetOperation("op");

            var observer = new AspNetMetricsObserver(new AspNetMetricsOptions(), _metrics);

            observer.OnHttpRequestStarted(_httpContext);
            observer.OnEndpointMatched(_httpContext);
            _httpContext.Response.StatusCode = 504;
            observer.OnHttpRequestCompleted(_httpContext);

            var metric = _metrics.GetMetric <ICounter>("http_server_errors_total");

            metric.ShouldNotBeNull();
            metric.Collector.Verify(x => x.WithLabels("op", "internal", String.Empty), Times.Once);
            metric.Child.Verify(x => x.Inc(1), Times.Once);
        }
        public void Can_track_requests()
        {
            _httpContext.SetOperation("op");

            var observer = new AspNetMetricsObserver(new AspNetMetricsOptions(), _metrics);

            observer.OnHttpRequestStarted(_httpContext);

            observer.OnEndpointMatched(_httpContext);

            var metric = _metrics.GetMetric <IGauge>("http_server_requests_in_progress");

            metric.ShouldNotBeNull();
            metric.Collector.Verify(x => x.WithLabels("op"), Times.Once);
            metric.Child.Verify(x => x.Inc(1), Times.Once);

            _httpContext.Response.StatusCode = 200;
            observer.OnHttpRequestCompleted(_httpContext);
        }