-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (58 loc) · 2.3 KB
/
Program.cs
File metadata and controls
72 lines (58 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
namespace SampleApi;
using System.Security.Cryptography;
using HealthChecks.ApplicationStatus.DependencyInjection;
using HealthChecks.OpenTelemetry.Instrumentation;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using OpenTelemetry.Metrics;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
.AddApplicationStatus()
.AddProcessAllocatedMemoryHealthCheck(5)
.AddDiskStorageHealthCheck(options => options.WithCheckAllDrives())
.AddAsyncCheck("random", async () =>
{
var metadata = new Dictionary<string, object>
{
{ "tenant", "mytenant1" }
};
await Task.Delay(RandomNumberGenerator.GetInt32(100, 501));
var rng = RandomNumberGenerator.GetInt32(100) % 3;
switch (rng)
{
case 0:
return HealthCheckResult.Unhealthy(data: metadata);
case 1:
return HealthCheckResult.Degraded(data: metadata);
default:
return HealthCheckResult.Healthy(data: metadata);
}
});
builder.Services.Configure<HealthChecksInstrumentationOptions>(options =>
{
options.StatusGaugeName = "myapp.health";
options.DurationGaugeName = "myapp.health.duration";
options.IncludeHealthCheckMetadata = true;
});
builder.Services.AddOpenTelemetry()
.WithMetrics(metricsBuilder => metricsBuilder
.AddHealthChecksInstrumentation()
.AddAspNetCoreInstrumentation()
.AddPrometheusExporter()
.AddConsoleExporter());
var app = builder.Build();
app.MapGet("/", () => Results.Redirect("/metrics"));
app.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.UseOpenTelemetryPrometheusScrapingEndpoint();
app.Run();
}
}