Capturing application metrics is only half the battle; without a visualization layer, raw telemetry streams are difficult to analyze. To effectively monitor Entity Framework Core (EF Core) performance in real time—tracking query volumes, active database contexts, and compile cache efficiency—you need a centralized monitoring dashboard.

By pairing Prometheus (for scraping and storing time-series data) with Grafana (for building visual charts), you can create a production-grade observability stack for your .NET applications.

Step 1: Configure Prometheus to Scrape Your Application

Prometheus pulls metrics from your .NET application at regular intervals by scraping an HTTP endpoint. Create a prometheus.yml configuration file in your project or monitoring directory:

YAML

global:
  scrape_interval: 15s # Collect metrics every 15 seconds

scrape_configs:
  - job_name: 'dotnet-efcore-app'
    static_configs:
      # Use 'host.docker.internal' if running your .NET app locally outside Docker, 
      # or your container service name if running within the same Docker network.
      - targets: ['host.docker.internal:5000'] 

Step 2: Spin Up Prometheus and Grafana via Docker Compose

Instead of installing monitoring tools manually, you can orchestrate both Prometheus and Grafana locally using a single docker-compose.yml file:

YAML

version: '3.8'

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
    depends_on:
      - prometheus

Run docker compose up -d in your terminal to start the containers.

Step 3: Connect Grafana to Prometheus

  1. Open your browser and navigate to http://localhost:3000.

  2. Log in using the default administrator credentials (admin / admin).

  3. Navigate to Connections -> Data sources -> Add data source.

  4. Select Prometheus.

  5. Set the connection URL to http://prometheus:9090 and click Save & test.

Step 4: Build Dashboards Using PromQL Queries

When OpenTelemetry exports .NET diagnostics metrics to Prometheus, dots (.) are automatically converted into underscores (_). You can build rich dashboard panels using these standard PromQL queries:

Panel Title

PromQL Query

What It Shows

Query Execution Rate

rate(microsoft_entityframeworkcore_queries_total[1m])

Database queries executed per second.

Active DbContext Instances

microsoft_entityframeworkcore_active_dbcontexts

Real-time tracking of active database contexts to detect memory leaks.

SaveChanges Operations

rate(microsoft_entityframeworkcore_savechanges_total[1m])

Frequency of database write transactions over time.

Compiled Query Cache Hit Rate

rate(microsoft_entityframeworkcore_compiled_query_cache_hits_total[1m])

Efficiency of compiled query caching.

Conclusion

Integrating EF Core metrics with Prometheus and Grafana gives your development and operations teams clear, proactive visibility into database health. By tracking execution rates and connection counts in real time, you can spot performance regressions before they impact end users.