Health & Metrics Integration in ASP.NET Core
π‘ Concept Name
Health Checks & Metrics Monitoring
π Quick Intro
Health checks let you monitor the health status of your ASP.NET Core app, while metrics (via Prometheus or App Insights) provide visibility into traffic, performance, and failures. Together, they form the foundation of app observability.
π§ Analogy / Short Story
Think of a web app like a car. Health checks are the dashboard lights (engine check, oil), while metrics are the trip computer β showing fuel efficiency, distance, and speed trends. Both are essential to keep it running smoothly and avoid breakdowns.
π§ Technical Explanation
Health Checks: Provided by Microsoft.Extensions.Diagnostics.HealthChecks
. Built-in checks include DB, disk, memory, custom endpoints, etc.
Metrics:
- Prometheus: Uses
prometheus-net.AspNetCore
. Exposes metrics at/metrics
endpoint. - Azure App Insights: Use
Microsoft.ApplicationInsights.AspNetCore
. Tracks telemetry (requests, dependencies, exceptions, performance).
π― Purpose & Use Case
- β Monitor application uptime & failures
- β Integrate with Kubernetes liveness/readiness
- β Feed dashboards (Grafana, Azure Monitor)
- β Trigger alerts when failures occur
- β Observe request load and performance bottlenecks
π» Real Code Example
// Add in Program.cs
builder.Services.AddHealthChecks()
.AddSqlServer("YourConnectionString");
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddPrometheusCounters();
var app = builder.Build();
// Prometheus endpoint
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health");
endpoints.MapMetrics(); // exposes /metrics
});
app.Run();
Prometheus NuGet: prometheus-net.AspNetCore
App Insights NuGet: Microsoft.ApplicationInsights.AspNetCore
β Interview Q&A
Q1: What are health checks in ASP.NET Core?
A: Built-in endpoints that expose application/service health.
Q2: Default health check endpoint?
A: You define it manually, e.g., /health
.
Q3: Which NuGet package is used for Application Insights?
A: Microsoft.ApplicationInsights.AspNetCore
Q4: How do you expose Prometheus metrics?
A: Use app.MapMetrics()
from Prometheus.NET.
Q5: What is the benefit of Prometheus integration?
A: Real-time monitoring of metrics like request count, duration, etc.
Q6: Can health checks be used in Kubernetes?
A: Yes, for liveness/readiness probes.
Q7: Can I write custom health checks?
A: Yes, by implementing IHealthCheck
.
Q8: Which endpoint does Application Insights collect data from?
A: All registered requests, dependencies, etc.
Q9: How do you view metrics in Azure App Insights?
A: Through Azure Portal or Log Analytics queries.
Q10: What does Prometheus scrape?
A: It periodically scrapes the /metrics
endpoint exposed by your app.
π MCQs
Q1: Which of these exposes metrics to Prometheus?
- A. MapHealthMetrics()
- B. MapMetrics()
- C. EnablePrometheus()
- D. AddMonitoring()
Q2: Health checks are configured in:
- A. builder.Services
- B. Startup.cs only
- C. launchSettings.json
- D. IIS Manager
Q3: App Insights tracks which of the following?
- A. Only request count
- B. Telemetry, logs, exceptions
- C. Memory snapshots
- D. Azure DevOps logs
Q4: Which Prometheus package is used?
- A. Microsoft.Metrics
- B. Prometheus.Telemetry
- C. prometheus-net.AspNetCore
- D. System.Metrics
Q5: Whatβs the output format of Prometheus metrics?
- A. XML
- B. HTML
- C. Plain text key-value pairs
- D. JSON only
π‘ Bonus Insight
Health and telemetry aren't just for production. Use them in staging/dev to catch regressions early. Combine metrics with alerting rules to auto-detect outages before users complain.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!