public async Task Invoke(IDictionary <string, object> environment)
        {
            var requestPath = environment["owin.RequestPath"] as string;

            if (Options.HealthEndpointEnabled &&
                Options.HealthEndpoint.IsPresent() &&
                Options.HealthEndpoint == requestPath)
            {
                Logger.MiddlewareExecuting(GetType());

                //TODO: AH - confirm cancellation token is correct
                var cancellationToken = (CancellationToken)environment["owin.CallCancelled"];
                var healthStatus      = await Metrics.Health.ReadStatusAsync(cancellationToken);

                string warning = null;

                var responseStatusCode = HttpStatusCode.OK;

                if (healthStatus.Status.IsUnhealthy())
                {
                    responseStatusCode = HttpStatusCode.InternalServerError;
                }

                if (healthStatus.Status.IsDegraded())
                {
                    responseStatusCode = HttpStatusCode.OK;
                    warning            = "Degraded";
                }

                var json = _serializer.Serialize(healthStatus);

                await WriteResponseAsync(environment, json, "application/json", responseStatusCode, warning);

                Logger.MiddlewareExecuted(GetType());

                return;
            }

            await Next(environment);
        }
Esempio n. 2
0
        // ReSharper disable UnusedMember.Global
        public async Task Invoke(HttpContext context)
        // ReSharper restore UnusedMember.Global
        {
            if (Options.HealthEndpointEnabled &&
                Options.HealthEndpoint.IsPresent() &&
                Options.HealthEndpoint == context.Request.Path)
            {
                Logger.MiddlewareExecuting(GetType());

                var healthStatus = await Metrics.Health.ReadStatusAsync(context.RequestAborted);

                string warning = null;

                var responseStatusCode = HttpStatusCode.OK;

                if (healthStatus.Status.IsUnhealthy())
                {
                    responseStatusCode = HttpStatusCode.InternalServerError;
                }

                if (healthStatus.Status.IsDegraded())
                {
                    responseStatusCode = HttpStatusCode.OK;
                    warning            = Constants.Health.DegradedStatusDisplay;
                }

                var json = _serializer.Serialize(healthStatus);

                await WriteResponseAsync(context, json, "application/json", responseStatusCode, warning);

                Logger.MiddlewareExecuted(GetType());

                return;
            }

            await Next(context);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public Task WriteAsync(HttpContext context, HealthStatus healthStatus, CancellationToken token = default(CancellationToken))
        {
            var json = _serializer.Serialize(healthStatus);

            return(context.Response.WriteAsync(json, token));
        }