Isaac.

Implementing API Monitoring

Monitor API health and performance in production.

By EMEPublished: February 20, 2025
api monitoringmetricsalertingobservability

A Simple Analogy

API monitoring is like having instruments on a dashboard. You can see metrics in real-time to spot issues.


Why Monitor?

  • Issue detection: Know when something's wrong
  • Performance tracking: Identify slow endpoints
  • Debugging: Understand what happened
  • Alerting: Notify team of problems
  • Capacity planning: Plan for growth

Key Metrics

Response Time (Latency)
- Min, max, average, P95, P99
- Alert if > 500ms

Error Rate
- 4xx vs 5xx percentage
- Alert if > 1%

Throughput
- Requests per second
- Track peak capacity

Availability
- Uptime percentage
- Target: 99.9% or better

Application Insights

builder.Services.AddApplicationInsightsTelemetry();

var client = new TelemetryClient();

// Track custom events
client.TrackEvent("UserRegistered", new Dictionary<string, string>
{
    { "Source", "Mobile" },
    { "Country", "US" }
});

// Track custom metrics
client.GetMetric("ProcessingTime").TrackValue(1250);  // milliseconds

// Track exceptions
try
{
    await ProcessOrderAsync();
}
catch (Exception ex)
{
    client.TrackException(ex, new Dictionary<string, string>
    {
        { "OrderId", "123" }
    });
}

Custom Middleware

public class MonitoringMiddleware
{
    private readonly RequestDelegate _next;
    private readonly TelemetryClient _telemetry;
    
    public async Task InvokeAsync(HttpContext context, TelemetryClient telemetry)
    {
        var stopwatch = System.Diagnostics.Stopwatch.StartNew();
        
        try
        {
            await _next(context);
        }
        finally
        {
            stopwatch.Stop();
            
            _telemetry.TrackEvent("ApiRequest", new Dictionary<string, string>
            {
                { "Method", context.Request.Method },
                { "Path", context.Request.Path },
                { "StatusCode", context.Response.StatusCode.ToString() }
            }, new Dictionary<string, double>
            {
                { "Duration", stopwatch.ElapsedMilliseconds }
            });
        }
    }
}

Prometheus Metrics

using Prometheus;

var httpRequestDuration = Metrics
    .CreateHistogram("http_request_duration_seconds",
        "HTTP request latencies in seconds");

var httpRequestTotal = Metrics
    .CreateCounter("http_requests_total",
        "Total HTTP requests");

app.Use(async (context, next) =>
{
    var method = context.Request.Method;
    var path = context.Request.Path;
    
    using (httpRequestDuration
        .WithLabels(method, path)
        .NewTimer())
    {
        await next();
    }
    
    httpRequestTotal.WithLabels(method, path).Inc();
});

// Expose metrics at /metrics
app.UseHttpMetrics();

Alerting Rules

# Example Prometheus alert
groups:
- name: api_alerts
  rules:
  - alert: HighErrorRate
    expr: rate(http_requests_total{status=~"5.."}[5m]) > 0.05
    for: 5m
    annotations:
      summary: "High error rate detected"
  
  - alert: HighLatency
    expr: histogram_quantile(0.95, http_request_duration_seconds) > 0.5
    for: 5m
    annotations:
      summary: "P95 latency exceeds 500ms"

Best Practices

  1. Start simple: Monitor key metrics only
  2. Set thresholds: Define alert conditions
  3. Aggregation: Track by endpoint/service
  4. Retention: Keep history for analysis
  5. Dashboards: Visualize important metrics

Related Concepts

  • Distributed tracing
  • Log aggregation
  • Time-series databases
  • Observability

Summary

Monitor API health through response times, error rates, and throughput. Use Application Insights, Prometheus, or custom telemetry.