英文:
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:
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:
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 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.
"Policy": "HealthResponsePolicy"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论