Using Prometheus and Grafana to visualize EF Core Performance

Analyzing raw telemetry streams is challenging without a graphical layer, thus capturing application metrics is just half the fight. You require a centralized monitoring dashboard to watch query volumes, active database contexts, and compilation cache efficiency in order to efficiently monitor Entity Framework Core (EF Core) performance in real time.

You may develop a production-grade observability stack for your.NET apps by combining Prometheus (for scraping and storing time-series data) with Grafana (for creating visual charts).

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

Your development and operations teams can see database health clearly and proactively by integrating EF Core metrics with Prometheus and Grafana. You can identify performance regressions before they affect end users by monitoring execution rates and connection counts in real time.

Best Entity Framework Core 5.0 Hosting

Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your Entity Framework Core 5.0 web ranking. HostForLIFE.eu is highly recommended. In Europe, HostForLIFE.eu is the most popular option for first-time web hosts searching for an affordable plan. Their standard price begins at only €3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFE.eu guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.

By providing reseller hosting accounts, HostForLIFE.eu also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.