Monitoring the performance of a .NET application is critical for ensuring its reliability and efficiency. In this guide, we’ll walk through the process of setting up Docker, configuring Prometheus, and visualizing metrics with Grafana. If you haven’t reviewed the prerequisites, explore these detailed guides to build a solid foundation:

Step 1: Review Previous Blogs

Before proceeding, it is recommended to go through the previous blog posts for better understanding and continuity:

Step 2: Install Docker on Ubuntu

Docker simplifies application deployment by providing containerization. Install Docker on your Ubuntu server using:

sudo apt install -y docker.io
  • sudo apt install -y docker.io: Installs Docker, a platform for running applications in containers. The -y flag automatically confirms prompts.

Start and enable the Docker service to ensure it runs automatically after a reboot:

sudo systemctl start docker
sudo systemctl enable docker
  • sudo systemctl start docker: Starts the Docker service immediately.
  • sudo systemctl enable docker: Configures Docker to start automatically when the system boots.

Additional Steps After Installing Docker

  1. Grant necessary permissions to the Docker socket:
sudo chmod 666 /var/run/docker.sock
  • chmod 666 /var/run/docker.sock: Changes permissions of the Docker socket to allow all users to access Docker without needing root privileges.
  1. Add your user to the Docker group:
sudo usermod -aG docker $USER
  • usermod -aG docker $USER: Adds the current user ($USER) to the Docker group, granting permission to execute Docker commands without sudo.

Log out and log back in for the changes to take effect.

Step 3: Configure Prometheus Using Docker

Prometheus collects metrics and stores them for analysis. To get started:

  1. Pull the Prometheus Docker image:
sudo docker pull prom/prometheus
  • docker pull: Downloads the specified Docker image from the Docker registry.
  1. Edit the prometheus.yml file to configure your application’s metrics endpoint:
sudo nano prometheus.yml

Here’s a sample configuration:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'RollDiceApp'
    scrape_interval: 5s
    metrics_path: '/metrics'
    static_configs:
      - targets: ['<EC2-instance-IP>:8080']

Replace <EC2-instance-IP> with your server’s public IP address.

Step 4: Build and Run Your .NET Application

  1. Compile the application:
sudo dotnet build
  • dotnet build: Compiles the .NET application, ensuring all dependencies and code are in place for execution.
  1. Run the application:
sudo dotnet run
  • dotnet run: Builds and runs the .NET application. This exposes the /metrics endpoint for Prometheus to scrape.

Test the application metrics at:

http://<EC2-Instance-IP>:8080/metrics

Test the RollDice simulation at:

http://<EC2-Instance-IP>:8080/rolldice

Navigate to your project directory and start the Prometheus container:

cd RollDiceApp
sudo docker run -p 9090:9090 -v ~/RollDiceApp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
  • docker run: Runs a container from the specified Docker image.
  • -p 9090:9090: Maps port 9090 of the host machine to port 9090 of the container.
  • -v: Mounts the local prometheus.yml file into the container.

Access Prometheus at:

http://<EC2-Instance-IP>:9090

Verify that Prometheus is scraping metrics by visiting the Targets page in the Prometheus UI.

Step 5: Install and Configure Grafana Using Docker

  1. Pull the Grafana Docker image:
sudo docker pull grafana/grafana
  1. Start the Grafana container:
sudo docker run -d -p 3000:3000 --name=grafana grafana/grafana
  • -d: Runs the container in detached mode.
  • -p 3000:3000: Maps port 3000 of the host machine to port 3000 of the container.

Access Grafana at:

http://<EC2-Instance-IP>:3000

Default login credentials:

  • Username: admin
  • Password: admin

Change the password on first login or skip as per your preference.

Step 6: Connect Prometheus to Grafana

After logging into Grafana:

  1. Navigate to Connections > Data sources.
  2. Search for Prometheus and select it.
  3. Add a new data source with the URL:
http://<EC2-instance-IP>:9090

Save and test the configuration to ensure connectivity.

Step 7: Create a Grafana Dashboard

  1. Click the Plus icon in the sidebar and select Dashboard.
  2. Add a visualization and configure a query using Prometheus data. For example:
http_requests_received_total
  1. Choose a visualization type such as Time series, Graph, or Gauge. For this example, we’ll use Gauge.
  2. Save the dashboard with an appropriate title.

Conclusion

In this guide, we set up a robust monitoring solution for a .NET application using OpenTelemetry, Prometheus, and Grafana. This setup provides valuable insights into your application’s health and performance, empowering you to address issues proactively.

If you have any questions, feel free to reach out at devopsbyrushi@gmail.com.

Related Articles

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *