# Day 76 Build a Grafana dashboard

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567900689/c3630eac-d258-4dbc-9e7c-53c8ad966f0f.png align="center")

Table of contents

Concept

Pre-requisites

Let's Start with our Aim

Configure Loki and Promtail

Data Source in Grafana

Checking logs in Loki

Creating Dashboard

## **Concept**

Loki and Promtail are agents connected to Grafana to achieve the dashboard creation.

**Promtail is an agent which will ship the contents of local logs to a private Grafana Loki instance or Grafana Cloud**. It is usually deployed to every machine that has applications needed to be monitored.

## **Pre-requisites**

* Install and configure Grafana in an EC2 instance.
    
* Follow the below link to my blog which contains detailed steps explaining the installation process.
    
    ## **Let's Start with our Aim**
    
    ### **Configure Loki and Promtail**
    
    1. Create a folder and download the yaml file that will contain the required configuration for running loki.
        
        ***wget***[***raw.githubusercontent.com/grafana/loki/v2.8..***](https://raw.githubusercontent.com/grafana/loki/v2.8.0/cmd/loki/loki-local-config.yaml)***\-O loki-config.yaml***
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567996949/da1e9364-e9ad-4694-abb9-daa478411c8e.png align="center")
        
    2. Download the yaml file that will contain the required configuration for running promtail.
        
        ***wget***[***raw.githubusercontent.com/grafana/loki/v2.8..***](https://raw.githubusercontent.com/grafana/loki/v2.8.0/clients/cmd/promtail/promtail-docker-config.yaml)***\-O promtail-config.yaml***
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707568115391/ab263dcb-bdc2-4165-808d-86926311d84e.png align="center")
        
    3. Run Loki Docker container using the below command.
        
        ***docker run -d --name loki -v $(pwd):/mnt/config -p 3100:3100 grafana/loki:2.8.0 --config.file=/mnt/config/loki-config.yaml***
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707568159225/03892a70-8ebe-44b4-ae14-00d5605704d3.png align="center")
        
    4. In the above step, 3100 port is mapped to the loki container. Therefore let's open the port in the security group to make the loki container running.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707566785039/f42dd24b-88f1-4434-bbfe-88cbccf32c6a.png align="center")
        
    5. Navigate to the loki URL and wait for sometime to make the loki live.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707566832431/e2cdc7a8-109b-4540-8d56-4231d707ea97.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707566852829/bfc356d4-0b04-41fa-90dc-f6407dde3415.png align="center")
        
    6. You can also see the metrics which means the logs which is the sole purpose of loki to collect.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707566875000/a718e301-fb27-4e28-9497-d1a78067b19f.png align="center")
        
    7. Run the Promtail Docker container using the below command and check with docker ps to see the container status.
        
        ***docker run -d --name promtail -v $(pwd):/mnt/config -v /var/log:/var/log --link loki grafana/promtail:2.8.0 --config.file=/mnt/config/promtail-config.yaml***
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707566960790/1daaf238-4ea5-45f0-bd26-cfc77d0961c0.png align="center")
        
        ### **Data Source in Grafana**
        
    8. Now, navigate to the Grafana webapp and on the homepage choose the add data source option.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567041029/19aee6df-fc8a-45b8-802b-f9cb03e690be.png align="center")
        
    9. Provide the HTTP URL as below to connect the loki data source to Grafana so that loki will send the logs to grafana.
        
        [***localhost:3100***](http://localhost:3100/)
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567069665/9a058278-404a-4fba-b70d-193a4da4840e.png align="center")
        
    10. Click on save & test to connect the data source.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567092271/3828c5f3-1c72-44f1-a27b-e1ef7965ceab.png align="center")
        
    
    ### **Checking logs in Loki**
    
    1. Click on explore in the above screenshot after adding the data source. In the label filters, we can choose job and varlogs which is generally the path /var/log/\*log in the backend to show all the system logs.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567162904/6543a37d-bdff-467d-aed5-4b8a38d9d50f.png align="center")
        
    2. Click on the run query in the above screenshot to execute and show all the system logs as below.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567202282/6eab7f89-2f25-40b0-98be-ebc74911b20e.png align="center")
        
    
    ### **Creating Dashboard**
    
    1. Let's add the log to the dashboard by choosing the option from the above screenshot location.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567247271/82127077-8d97-4727-9b19-9bacba749cae.png align="center")
        
    2. Now the system logs are added to the grafana dashboard. Let's add some more by clicking on **visualization** to add some graphs as shown below screenshot.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685310121056/fe7bc394-fa49-4df7-be34-8feed075cc16.png?auto=compress,format&format=webp align="left")
        
    3. In **Label filters** choose job and varlogs and line contains to error to show all tthe lines with error and select the duration to show all the lines with error in the logs.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685310214294/f4eafc86-faac-4979-afe9-1a429e6f3301.png?auto=compress,format&format=webp align="left")
        
    4. To achieve the aim of displaying the grafana log, we need to provide the grafana log path to the promtail config yaml file in the target section as shown below.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567748772/f2127fc8-edd6-430d-8f47-e67dc3650c73.png align="center")
        
    5. We can now choose the label filters to set the job and grafanalogs with the line contains and visualization option to view in a graphical manner. We can add this to our dashboard.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685310867293/07e8275f-78ae-4b47-aac8-83b58794d901.png?auto=compress,format&format=webp align="left")
        
    6. Now, install nginx in the EC2 instance.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567830399/a80a184e-6841-489b-addb-adcf31090aea.png align="center")
        
    7. Use the proper label filters to show an aggregate sum of words repeating nginx while installing. This can be achieved by setting the varlogs as label filters.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685310782283/c03cce12-d4ab-46bf-b5a3-abd372a0ebae.png?auto=compress,format&format=webp align="left")
        
    8. We can see now the complete grafana dashboard.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707567900689/c3630eac-d258-4dbc-9e7c-53c8ad966f0f.png align="center")
        
    
    Thanks for reading my article. Have a nice day.
    
    Happy learning ✨✨
