Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Oct 22 04:26
    mlapaglia commented #485
  • Oct 22 01:10
    ghd258 opened #489
  • Oct 21 19:19
    delepster edited #488
  • Oct 21 18:53
    delepster opened #488
  • Oct 21 15:33
    ugumba commented #189
  • Oct 21 15:33
    ugumba commented #189
  • Oct 21 09:00
    damianh commented #189
  • Oct 21 09:00
    damianh commented #189
  • Oct 17 17:01
    pieter-venter commented #486
  • Oct 17 01:58
    nullpainter opened #487
  • Oct 17 00:45
    WolfspiritM commented #486
  • Oct 17 00:42
    WolfspiritM commented #486
  • Oct 17 00:25
    pieter-venter commented #486
  • Oct 16 12:25
    ghd258 commented #476
  • Oct 16 12:25
    ghd258 commented #476
  • Oct 14 15:15
    WolfspiritM opened #486
  • Oct 14 10:50
    srsrsrsrsrsrsr opened #485
  • Oct 10 21:47
    Rast1234 edited #484
  • Oct 10 21:40
    Rast1234 opened #484
  • Oct 10 18:34
    impactmrj opened #483
Richard Tasker
@ritasker
@alhardy Thanks for helping me out last week. I didn't realise I had to add a webserver to my console app to get the healthchecks. But I have them now :+1:
Allan Hardy
@alhardy
@ritasker no worries. Health checks can be used without having to register the health check middleware but then you would have to report the health by some other means other than exposing an endpoint.
Silas Reinagel
@SilasReinagel
I seem to be having troubles with the Health Endpoint hosting
Not sure if I am doing something wrong.
        public static void AddHealthChecks(this IServiceCollection services)
        {
            var serviceProvider = services.BuildServiceProvider();
            var health = AppMetricsHealth.CreateDefaultBuilder()
                .HealthChecks.AddCheck(new ServiceHealthCheck<OltpDb>(serviceProvider.GetService<OltpDb>(), "SQL Connection"))
                .Build();

            services.AddHealth(health);
            services.AddHealthEndpoints();
        }

        public static void UseHealthChecks(this IApplicationBuilder applicationBuilder)
        {
            applicationBuilder.UseHealthAllEndpoints();
        }
I have NuGet Packages instead for App.Metrics.AspNetCore, App.Metrics.AspNetCore.Endpoints, App.Metrics.AspNetCore.Health, App.Metrics.AspNetCore.Health.Endpoints, App.Metrics.AspNetCore.Mvc and App.Metrics.AspNetCore.Health.Hosting
I can't find the hosting endpoint when running.
Akash John
@johnakash
Hi Guys
I am looking for a solution to monitor health check of some applications we developed. All the applications are hosted internally and we will be monitoring the same through Health. Could you please let us know if this application help me in that?
MikhaelSorkin
@MikhaelSorkin
Hi all.
Can someone tell me, how to configure reporting from healthchecks to metrics, to evaluate healthchecks only when metrics requested?
We have an installation of Prometheus and we want to control healthchecks evaluation frequency only at Prometheus.
So we need this pipeline:
1) prometheus asks http://some-service/metrics
2) appmetrics evaluates healthchecks and converts result to 0 or 1
3) appmetrics returns this value in prometheus text format
Allan Hardy
@alhardy
Try this:
public static IWebHostBuilder ConfigureDefaultHealth(this IWebHostBuilder webHostBuilder, IMetrics metrics)
        {
            webHostBuilder.ConfigureHealthWithDefaults((context, builder) =>
            {
                // Add additional checks
                builder.Report.ToMetrics(metrics);
            });

            return webHostBuilder;
        }
public sealed class Program
    {
        public static async Task<int> Main(string[] args)
        {
            var webHost = CreateWebHostBuilder(args).Build();

            try
            {
                webHost.Run();

                return 0;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly, check the application's WebHost configuration.");
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var metrics = AppMetrics.CreateDefaultBuilder().BuildDefaultMetrics();

            var webHost = WebHost.CreateDefaultBuilder(args)
                   .UseKestrel(options => options.AddServerHeader = false)
                   .UseContentRoot(Directory.GetCurrentDirectory())
                   .ConfigureDefaultMetrics(metrics)
                   .ConfigureDefaultHealth(metrics)
                   .UseDefaultMetrics()
                   .UseDefaultHealth()
                   .UseStartup<Startup>();

            return webHost;
        }
    }
Program.Metrics.cs
public static class ProgramMetrics
    {
        public static IMetricsRoot BuildDefaultMetrics(this IMetricsBuilder builder)
        {
            var configuration = new ConfigurationBuilder()
                                .AddDefaultSources()
                                .Build();

            // Samples with weight of less than 10% of average should be discarded when rescaling
            const double minimumSampleWeight = 0.001;

            builder.Configuration.ReadFrom(configuration);

            builder.SampleWith.ForwardDecaying(
                AppMetricsReservoirSamplingConstants.DefaultSampleSize,
                AppMetricsReservoirSamplingConstants.DefaultExponentialDecayFactor,
                minimumSampleWeight: minimumSampleWeight);

            if (!Constants.Environments.IsDevelopment())
            {
                var grafanaCloudHostedMetricsOptions = new MetricsReportingHostedMetricsOptions();
                configuration.GetSection(nameof(MetricsReportingHostedMetricsOptions)).Bind(grafanaCloudHostedMetricsOptions);

                builder.Report.ToHostedMetrics(grafanaCloudHostedMetricsOptions);
            }

            return builder.Build();
        }

        public static IWebHostBuilder ConfigureDefaultMetrics(this IWebHostBuilder webHostBuilder, IMetricsRoot metrics)
        {
            webHostBuilder.ConfigureMetrics(metrics);

            return webHostBuilder;
        }

        public static IWebHostBuilder UseDefaultMetrics(this IWebHostBuilder webHostBuilder)
        {
            // Protect endpoints with key
            webHostBuilder.ConfigureServices(
                (context, services) => { services.AddSingleton<IStartupFilter>(new RequireEndpointKeyStartupFilter(context.Configuration)); });

            webHostBuilder.UseMetrics();

            return webHostBuilder;
        }
    }
MikhaelSorkin
@MikhaelSorkin

Hello Allan.
Thank you for your response.
As i understand, this line builder.Report.ToMetrics(metrics) adds reporter which would execute healthchecks at its own frequency - it configures by ReportInterval.
I want to disable this reporting and want to evaluate health score only when prometheus asks for it.
I found another solution to evaluate healthchecks on demand:

var metrics = new MetricsBuilder().Build();
var health = new HealthBuilder().Build();

metrics.Measure.Gauge.SetValue(new GaugeOptions(), () => new FunctionGauge(() =>
{
    var checkResult = health.HealthCheckRunner.ReadAsync().GetAwaiter().GetResult();
    if(checkResult.Status == HealthCheckStatus.Healthy)
    {
        return 1;
    }

    return 0;
}));

Is it ok?