获取健康状态并记录 – YARP

huangapple go评论52阅读模式
英文:

get health status and log it - YARP

问题

I am working with YARP for reverse proxy to communicate with end points. I need to check the health status (like working properly, downtime, transient issues etc). I have followed the configurations mentioned in the following YARP documentation:

https://microsoft.github.io/reverse-proxy/articles/dests-health-checks.html#available-destination-collection

The configuration for health check in appsettings.json is:

"Clusters": {
      "abc": {
        "HealthCheck": {
          "Active": {
            "Enabled": "true",
            "Interval": "00:00:10",
            "Timeout": "00:00:10",
            "Policy": "ConsecutiveFailures",
            "Path": "/test"
          }
        },
        "Metadata": {
          "ConsecutiveFailuresHealthPolicy.Threshold": "3"
        },
        "Destinations": {
          "abc": {
            "Address": "http://localhost:5500/abc",
            "Health": "http://localhost:5500/"
          }
        }
      },
      "api": {
        "Destinations": {
          "api": {
            "Address": "http://localhost:5600/"
          }
        }
      }
    }

I am able to hit the action on 'abc' end point. However since I want to log the status, I had written the following class, following the sample in the above given link.

using Yarp.ReverseProxy.Health;
using Yarp.ReverseProxy.Model;

namespace YarpReverseProxyServer
{
    public class HealthResponsePolicy : IActiveHealthCheckPolicy
    {
        private readonly IDestinationHealthUpdater _healthUpdater;

        public HealthResponsePolicy(IDestinationHealthUpdater healthUpdater)
        {
            _healthUpdater = healthUpdater;
        }

        public string Name => "HealthResponsePolicy";

        public void ProbingCompleted(ClusterState cluster, IReadOnlyList<DestinationProbingResult> probingResults)
        {
            if (probingResults.Count == 0)
            {
                return;
            }

            var newHealthStates = new NewActiveDestinationHealth[probingResults.Count];
            for (var i = 0; i < probingResults.Count; i++)
            {
                var response = probingResults[i].Response;
                var newHealth = response is not null && response.IsSuccessStatusCode ? DestinationHealth.Healthy : DestinationHealth.Unhealthy;
                newHealthStates[i] = new NewActiveDestinationHealth(probingResults[i].Destination, newHealth);
            }

            _healthUpdater.SetActive(cluster, newHealthStates);
        }
    }
}

and then add the following line in Startup.cs, although it was not mentioned in the above link:

services.AddSingleton<IActiveHealthCheckPolicy, HealthResponsePolicy>();

However this does not work, because the code never hits the ProbingCompleted method of the HealthResponsePolicy class. I can't understand what I am missing.

英文:

I am working with YARP for reverse proxy to communicate with end points. I need to check the health status (like working properly, downtime, transient issues etc). I have followed the configurations mentioned in the following YARP documentation:

https://microsoft.github.io/reverse-proxy/articles/dests-health-checks.html#available-destination-collection

The configuration for health check in appsettings.json is:

&quot;Clusters&quot;: {
      &quot;abc&quot;: {
        &quot;HealthCheck&quot;: {
          &quot;Active&quot;: {
            &quot;Enabled&quot;: &quot;true&quot;,
            &quot;Interval&quot;: &quot;00:00:10&quot;,
            &quot;Timeout&quot;: &quot;00:00:10&quot;,
            &quot;Policy&quot;: &quot;ConsecutiveFailures&quot;,
            &quot;Path&quot;: &quot;/test&quot;
          }
        },
        &quot;Metadata&quot;: {
          &quot;ConsecutiveFailuresHealthPolicy.Threshold&quot;: &quot;3&quot;
        },
        &quot;Destinations&quot;: {
          &quot;abc&quot;: {
            &quot;Address&quot;: &quot;http://localhost:5500/abc&quot;,
            &quot;Health&quot;: &quot;http://localhost:5500/&quot;
          }
        }
      },
      &quot;api&quot;: {
        &quot;Destinations&quot;: {
          &quot;api&quot;: {
            &quot;Address&quot;: &quot;http://localhost:5600/&quot;
          }
        }
      }
    }

I am able to hit the action on 'abc' end point. However since I want to log the status, I had written the following class, following the sample in the above given link.

using Yarp.ReverseProxy.Health;
using Yarp.ReverseProxy.Model;

namespace YarpReverseProxyServer
{
    public class HealthResponsePolicy : IActiveHealthCheckPolicy
    {
        private readonly IDestinationHealthUpdater _healthUpdater;

        public HealthResponsePolicy(IDestinationHealthUpdater healthUpdater)
        {
            _healthUpdater = healthUpdater;
        }

        public string Name =&gt; &quot;HealthResponsePolicy&quot;;

        public void ProbingCompleted(ClusterState cluster, IReadOnlyList&lt;DestinationProbingResult&gt; probingResults)
        {
            if (probingResults.Count == 0)
            {
                return;
            }

            var newHealthStates = new NewActiveDestinationHealth[probingResults.Count];
            for (var i = 0; i &lt; probingResults.Count; i++)
            {
                var response = probingResults[i].Response;
                var newHealth = response is not null &amp;&amp; response.IsSuccessStatusCode ? DestinationHealth.Healthy : DestinationHealth.Unhealthy;
                newHealthStates[i] = new NewActiveDestinationHealth(probingResults[i].Destination, newHealth);
            }

            _healthUpdater.SetActive(cluster, newHealthStates);
        }
    }
}

and then add the following line in Startup.cs, although it was not mentioned in the above link:

services.AddSingleton&lt;IActiveHealthCheckPolicy, HealthResponsePolicy&gt;();

However this does not work, because code never hits to ProbingCompleted method of HealthResponsePolicy class. I cant understand what I am missing.

答案1

得分: 0

我找到了问题所在。我必须在 appsettings.json 文件的 Clusters 属性下设置以下策略,用于自定义类 HealthResponsePolicy。

"Policy": "HealthResponsePolicy"
英文:

I have found what was the problem. I had to set the following policy to the custom class HealthResponsePolicy in the appsettings.json under Clusters attribute.

&quot;Policy&quot;: &quot;HealthResponsePolicy&quot;

huangapple
  • 本文由 发表于 2023年4月4日 16:36:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定