Xabaril / AspNetCore.Diagnostics.HealthChecks

Enterprise HealthChecks for ASP.NET Core Diagnostics Package
Apache License 2.0
4.04k stars 789 forks source link

HealthChecks Timing Execution #548

Closed vicentt closed 4 years ago

vicentt commented 4 years ago

I am using the library in 2.2 version in a NO Kubernetes docker environment.  I have  HealthChecks of many types such like Self, Sql, KeyVault Identiy Server etc. The problem is that I can't find how to configure these HealthChecks to be executed every so often.

I configured the app like this:

  services.AddHealthChecksUI(setupSettings: setup => setup.SetEvaluationTimeInSeconds(300));  

and in my appsettings json i have

"EvaluationTimeinSeconds": 300,   

In the HealthChecks UI I see that the elapsed time between LAST EXECUTION is 5 minutes but the events in my HealthChecks Application Insight occur every 30 seconds and I see in the Aplication Insight of my application that the dependencies are executed too often and for each container what it does have a lot of mess.

In fact, sometimes I get things like: System.Net.Sockets.SocketException: Cannot assign requested address when trying to check keyvault in Azure and IdentiyServer.  

I think I end up wiping out all the available ports on the container or on the host.(I don't know much about docker)

Am I misusing the library? Am I missing some configuration??

vicentt commented 4 years ago

After a new deployment, The service is now using the value established in:

"EvaluationTimeinSeconds": 300,

But in the Application Insight configured for the HealthChecks Service, the custom events still occurs every 30 seconds. I dont understand why.

Sorry for the inconvenience.

Thanks.

CarlosLanderas commented 4 years ago

@vicentt , are you configuring it like here?

https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/520

It's weird, because Application Insights publisher is an implementation of a Microsoft Interface that is only triggered after a report collection.

Could you share your project configuration to take a look into it?

Thanks!

vicentt commented 4 years ago

Thanks Carlos for your Answer. I dont want to waste your time.

The only thing I don't understand is why the library writes a custom message in the Insight that I've put in for it, every 30 seconds. It doesn't really matter that much. It's just that it makes me angry not knowing why. It is almost certainly a mistake of mine, so don't pay much attention to me ..

The library is a great job. My congratulations.

This is a screenshot of my development enviroment insight that works with a single container

image

This is my code I have changed some parts of the code to not show sensitive information

appsettings.json

  "HealthChecks-UI": {
    "HealthChecks": [
      {
        "Name": "Health of Application",
        "Uri": "http://desa-myurl/myhealthdependency"
      },
      {
        "Name": "Health of Sql Server Dependencies",
        "Uri": "http://desa-myurl/sqlserverdependency"
      },
      {
        "Name": "Health of Dependencies",
        "Uri": "http://desa-myurl/mydependencies"
      }
    ],
    "Webhooks": [ ],
    "EvaluationTimeinSeconds": 600,
    "MinimumSecondsBetweenFailureNotifications": 900
   }

My Configure Services Methods

 public static IServiceCollection AddMyMethodSqlServerHealthCkecksConfiguration(this IServiceCollection services, IConfiguration Configuration)
        {
            services.AddHealthChecks()
                 .AddSqlServer(
                    connectionString: Configuration["SqlServerData:ConnectionString1"],
                    healthQuery: "SELECT 1",
                    name: "Database1",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] {  "SQL" })
                 .AddSqlServer(
                    connectionString: Configuration["SqlServerData:ConnectionString2"],
                    healthQuery: "SELECT 1",
                    name: "Database2",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "SQL" })
                 .AddSqlServer(
                    connectionString: Configuration["SqlServerData:ConnectionString3"],
                    healthQuery: "SELECT 1",
                    name: "Database3",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "SQL" })
                .AddSqlServer(
                    connectionString: Configuration["AzureDownloadsData:ConnectionString4"],
                    healthQuery: "SELECT 1",
                    name: "Database4",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "SQL" });

            return services;
        }
   public static IServiceCollection AddMyMethodOtherStuffHealthCkecksConfiguration(this IServiceCollection services, IConfiguration Configuration)
        {
            services.AddSingleton<MyCustomHealthCheck>();
            services.AddHealthChecks()
                .AddCheck<SolrHealthCheck>(
                    name: "Solr",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "dependencies" }
                )
                .AddRedis(
                    redisConnectionString: Configuration["RedisData:RedisConnection"],
                    name: "Redis",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "dependencies" })
                .AddIdentityServer(
                    idSvrUri: new Uri($"{Configuration["AuthData:MyIdentityServer"]}/openid-configuration"),
                    name: "IdentityServer",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "dependencies" }
                )
                .AddAzureKeyVault(setup =>
                    {
                        setup
                        .UseKeyVaultUrl(TheURL)
                        .UseClientSecrets(MyClientId,MyClientSecret)
                        .AddSecret("ForHealthChecks");
                    },
                    name: "AzureKeyVault",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "dependencies" })
                .AddAzureBlobStorage(connectionString: MyConnectionString,
                    name: "AzureBlobStorage",
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "dependencies" })
                .AddCheck<MyCustomHealthCheck>(
                    name: "My Custom `Service",`
                    failureStatus: HealthStatus.Unhealthy,
                    tags: new string[] { "dependencies" }
                )
                .AddCheck("self", () => HealthCheckResult.Healthy())
                .AddApplicationInsightsPublisher(Configuration["ApplicationInsights:InstrumentationKey-HealthChecks"]);

            services.AddHealthChecksUI();

            return services;
        }

Relevant part of my Configure Method

               .UseHealthChecks("/myhealthdependency", new HealthCheckOptions
                {
                    Predicate = registration => registration.Name.Equals("self"),
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                })
                .UseHealthChecks("/mydependencies", new HealthCheckOptions
                {
                    Predicate = registration => registration.Tags.Contains("dependencies"),
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                })
                .UseHealthChecks("/sqlserverdependencies", new HealthCheckOptions
                {
                    Predicate = registration => registration.Tags.Contains("SQL"),
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                })
                .UseHealthChecksUI(setup =>
                {
                     setup.UIPath = "/healthui"`;` 
                     setup.ApiPath = "/ui-api"; //
                     setup.AddCustomStylesheet($"{Env.WebRootPath}/HealthChecksUI/HealthChecksMyCSS.css");
                })

Thank you very much to you and to the entire Xabaril team!!

Mr-Pearce commented 4 years ago

Just found a solution for that 30 second publisher Problem maybe this helps #599