Monitoring your Entity Framework Core (EF Core) database performance metrics in Prometheus gives you real-time visibility into application behavior. However, relying solely on passive dashboards means issues like memory leaks or traffic spikes can go unnoticed until they impact users.

Configuring Prometheus alerting rules allows your observability system to automatically detect anomalies, such as high query rates, unreleased DbContext instances, or sluggish API latencies, and flag them before they escalate into outages.

Step 1: Create the Alerting Rules File

Create a file named alert_rules.yml inside your Prometheus configuration directory. This file defines rule groups and expressions that evaluate your time-series metrics against specified thresholds.

YAML

groups:
  - name: efcore_database_alerts
    rules:
      # Alert 1: High Database Query Traffic Spike
      - alert: HighDatabaseQueryRate
        expr: rate(microsoft_entityframeworkcore_queries_total[1m]) > 50
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "High database query volume detected"
          description: "The application is executing more than 50 queries per second for over 2 minutes."

      # Alert 2: Potential DbContext Memory Leak
      - alert: HighActiveDbContextCount
        expr: microsoft_entityframeworkcore_active_dbcontexts > 100
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Potential DbContext memory leak"
          description: "Active DbContext instances exceed 100 for more than 5 minutes, indicating unreleased contexts."

      # Alert 3: Slow HTTP Requests Tied to Heavy Database Workloads
      - alert: SlowHttpDatabaseLatency
        expr: histogram_quantile(0.95, rate(http_server_duration_seconds_bucket[5m])) > 1.5
        for: 3m
        labels:
          severity: warning
        annotations:
          summary: "Slow API response times detected"
          description: "P95 HTTP latency is exceeding 1.5 seconds, often caused by inefficient or unoptimized database queries."

Step 2: Register Rules in prometheus.yml

To ensure Prometheus loads your custom rules, reference the file via the rule_files directive inside your main prometheus.yml configuration:

YAML

global:
  scrape_interval: 15s

# Link your alerting rules file here
rule_files:
  - 'alert_rules.yml'

scrape_configs:
  - job_name: 'dotnet-efcore-app'
    static_configs:
      - targets: ['host.docker.internal:5000']

Restart your Prometheus container to apply the updated configuration:

Bash

docker compose restart prometheus

Step 3: Verify Your Alerts in Prometheus

  1. Open your browser and navigate to the Prometheus dashboard at http://localhost:9090.

  2. Click on the Alerts tab in the top navigation bar.

  3. Review your configured alerts (HighDatabaseQueryRate, HighActiveDbContextCount, etc.). Their states will transition from Inactive (green) to Firing (red) whenever the defined metric thresholds are breached.

Conclusion

By coupling OpenTelemetry EF Core metrics with Prometheus alerting rules, you transition from reactive debugging to proactive infrastructure monitoring. Catching unreleased DbContext counts or traffic spikes early ensures high availability and resilient application performance.