Prometheus architecture and use cases :
Tags: prometheus, devops, monitoring, opensource
Published: 2020-11-29 15:10:17

# __What is Prometheus :__
Prometheus is an open source automated monitoring and Alerting tool used for analyze the logs and send the alerts in case of any failure .

# __Why use Prometheus :__

As we all know __monitoring__ our application as well as the infrastructure is really an essence and monitoring distributed applications is a real challenge .

* __Prometheus__ helps us in that area where it scrapes the metrics data from the endpoint using query language(PromQL).
* Can be configured with Grafana which is a data visualization tool used for analyze the metrics data .
* Trigger the alters in case of any misconfiguration in our application .
* And the best part that it is free so community support is good . 

# __Prometheus Components :__

* __Prometheus server__ :
Is the core component of __Prometheus__ consists of three components .

* __Client Libraries :__
Provides language support for monitoring different applications . 

* __Push gateway :__
Push all the collected data to the database but only for short-lived jobs .

* __Exporters :__
It is a script needed for configuration like exposing the /metrics endpoint . 

* __Alert Manager :__
Send alerts in case of misconfiguration to different channels like slack, email, notification .
 
# __Prometheus Architecture :__

![Prometheus architecture](https://prometheus.io/assets/architecture.png)

# __Prometheus server :__
1. Data retrieval worker 
2. Time series database
3. HTTP server

* __Data retriever__ helps us to retrieve the data from the application endpoint .
* __Time series database__ stores the metrics data so later they can be retrieved and analyzed .
* __HTTP server__ accepts PromQL query language push the collected data to the dashboard for visualization purpose .

# __push gateway :__
* In case of pull mechanism __data retrieval worker__ pull all the metrics data from the endpoint .
* As a result multiple Prometheus instances can pull metrics data .
* But for short lived jobs like batch jobs which can be pushed to the Prometheus database . In that can we can use the push gateway .

# __Alert Manager :__
Responsible for sending the alters in case of any failure in the system .

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5wog1c7gxq1ycxm2zxee.png)

# __Prometheus web UI :__
Prometheus have it's own UI to visualize the logs but can be configured with Grafana .

![Grafana](https://grafana.com/static/img/grafana/showcase_visualize-954.jpg)

# __What does Prometheus monitor :__

* Linux/windows server
* Web server
* Stateless application
* Statefull application 

# __which units are monitored of those targets ?__

* It totally depends if we are analyzing log of an application or an operating system .

- CPU states 
- Memory and disk usages
- request count
- request Duration
- Exception count 

These all are known as metrics which got saved into time series database which then can be visualized .

# __How does Prometheus collects the data :__
The third component in our __Prometheus server__ which is a HTTP server which uses __PromQL__ query language and then scrapes the  metric data form worker nodes through a __HTTP endpoint - hostaddress/metrics__   

# __Target endpoint and exporters :__

* Some services are exposing to __/metrics__ endpoint by default but many services have not .
* exporter is a script and that fetches metric from the target so that Prometheus can understand that .

__Example of a Linux server exporter :__ 
- download node expoter
- untar and execute 
- converts metrics of the server
- exposes /metrics endpoint
- configure Prometheus to scrape this endpoint  

# __Client libraries :__
* Client libraries have different language support for monitoring your application .

# __Data storage :__
* Prometheus stores its on-disk time series data under the directory specified by the flag __storage.local.path__. 
* But can be configured to store the data on a remote location .

![remote data storage](https://prometheus.io/docs/prometheus/latest/images/remote_integrations.png) 

# __Configuring Prometheus__
*  In __Prometheus.yaml__ file you can define your desired configuration .

* __global config__ defines how often Prometheus will scrape it's targets . 
* __rules__ for aggregating metric values or creating alerts when condition met .
* __Scrape config__ for what resources Prometheus monitor .