<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Archives des monitoring - ji-ka</title>
	<atom:link href="https://www.ji-ka.tn/tag/monitoring/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.ji-ka.tn/tag/monitoring/</link>
	<description></description>
	<lastBuildDate>Mon, 02 Oct 2023 19:48:10 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://www.ji-ka.tn/wp-content/uploads/2023/10/cropped-icon-32x32.png</url>
	<title>Archives des monitoring - ji-ka</title>
	<link>https://www.ji-ka.tn/tag/monitoring/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Monitoring with Prometheus and Grafana</title>
		<link>https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/</link>
					<comments>https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/#respond</comments>
		
		<dc:creator><![CDATA[Jihed Kaouech]]></dc:creator>
		<pubDate>Sat, 30 Sep 2023 11:00:44 +0000</pubDate>
				<category><![CDATA[spring boot]]></category>
		<category><![CDATA[grafana]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[prometheus]]></category>
		<guid isPermaLink="false">http://jikatnr.cluster028.hosting.ovh.net/?p=65</guid>

					<description><![CDATA[<p>In this article, we will be looking into how we can monitor our Spring Boot application using Grafana. We would be looking into the whole setup and create a simple dashboard to view some metrics. 1 Application Spring Boot is a very popular microservice framework that significantly simplifies web application development by providing Java developers [&#8230;]</p>
<p>L’article <a href="https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/">Monitoring with Prometheus and Grafana</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will be looking into how we can monitor our Spring Boot application using Grafana. We would be looking into the whole setup and create a simple dashboard to view some metrics.</p>



<span id="more-65"></span>



<h3 class="wp-block-heading"><strong>1 Application</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Spring Boot is a very popular microservice framework that significantly simplifies web application development by providing Java developers with a platform to get started with an auto-configurable, production-grade Spring application.</p>



<h4 class="wp-block-heading"><strong>1.1 Actuator</strong></h4>



<p>Actuator endpoints let you monitor and interact with your Spring Boot application.<br>Read this article for more details : <a href="https://github.com/JKaouech/spring-boot/wiki/Spring-boot-actuator">spring boot actuator</a></p>



<h4 class="wp-block-heading"><strong>1.2 Setup</strong></h4>



<ul class="wp-block-list">
<li><strong>Prometheus dependency</strong></li>
</ul>



<p>To expose prometeus enndpoint we nedd to add this dependency</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;io.micrometer&lt;/groupId&gt;
            &lt;artifactId&gt;micrometer-registry-prometheus&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
        &lt;/dependency&gt;" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-web</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-actuator</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">io.micrometer</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">micrometer-registry-prometheus</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">scope</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">runtime</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">scope</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<ul class="wp-block-list">
<li><strong>Spring Boot properties</strong></li>
</ul>



<p>Next, we need to expose an actuator endpoint through which Prometheus will collect metrics data in the format that Prometheus understands. For this, we need to add the following properties.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="# -- actuator global configuration -- #
management: 
  endpoints: 
    web: 
      exposure: 
        include: &quot;health,info,prometheus&quot;
  endpoint:
    health:
      show-details: always
    metrics:
      enabled: true
    prometheus:
      enabled: true 

management.metrics:
  tags:
    application: ${spring.application.name}
    env: ${spring.profiles.active}
  distribution:
    percentiles-histogram:
      http:
        server:
          requests: 'true'" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #6A9955"># -- actuator global configuration -- #</span></span>
<span class="line"><span style="color: #569CD6">management</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">endpoints</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">web</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">exposure</span><span style="color: #E6E6E6">: </span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #569CD6">include</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;health,info,prometheus&quot;</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">endpoint</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">health</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">show-details</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">always</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">metrics</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">enabled</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">prometheus</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">enabled</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span><span style="color: #E6E6E6"> </span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">management.metrics</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">tags</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">application</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">${spring.application.name}</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">env</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">${spring.profiles.active}</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">distribution</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">percentiles-histogram</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #569CD6">http</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #569CD6">server</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">          </span><span style="color: #569CD6">requests</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;true&#39;</span></span></code></pre></div>



<p>You can reach the info actuator on your local machine : <a href="http://localhost:8080/actuator/prometheus">http://localhost:8080/actuator/prometheus</a> once you start your Spring Boot application.</p>



<p>The prometheus endpoint shows various metrics, such as JVM threads state, information about HTTP server requests, etc.</p>



<ul class="wp-block-list">
<li><strong>Metics data</strong></li>
</ul>



<p>After opening the above endpoint, you will find some metrics data in the following format</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Metaspace&quot;,} 4.417008E7
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Survivor Space&quot;,} 8388608.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap 'non-nmethods'&quot;,} 1294336.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Old Gen&quot;,} 9224992.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap 'non-profiled nmethods'&quot;,} 1.0788608E7
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Compressed Class Space&quot;,} 5484736.0
jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Eden Space&quot;,} 2.5165824E7" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Metaspace&quot;,} 4.417008E7</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Survivor Space&quot;,} 8388608.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap &#39;non-nmethods&#39;&quot;,} 1294336.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Old Gen&quot;,} 9224992.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;CodeHeap &#39;non-profiled nmethods&#39;&quot;,} 1.0788608E7</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;nonheap&quot;,env=&quot;local&quot;,id=&quot;Compressed Class Space&quot;,} 5484736.0</span></span>
<span class="line"><span style="color: #E6E6E6">jvm_memory_used_bytes{application=&quot;spring-prometheus-client&quot;,area=&quot;heap&quot;,env=&quot;local&quot;,id=&quot;G1 Eden Space&quot;,} 2.5165824E7</span></span></code></pre></div>



<p>The first part (<code>jvm_memory_used_bytes</code>) is called the label, while the fields inside the curly braces are called attributes. Each of these labels represents a particular metric and the attribute provides you with a way to query so that you can get the values.</p>



<h3 class="wp-block-heading"><strong>2 Prometheus</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Prometheus gathers metrics at intervals and needs to know how often to scrape them.<br>We will be using a Prometheus docker image and provide it with some configuration</p>



<h4 class="wp-block-heading"><strong>2.1 Configuration file</strong></h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="# my global config
global:
  scrape_interval:     120s  # By default, scrape targets every 15 seconds.
  evaluation_interval: 120s  # By default, scrape targets every 15 seconds.

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:  
  - job_name: 'Spring Boot Application input'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 2s
    static_configs:
      - targets: ['host.docker.internal:8080']" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #6A9955"># my global config</span></span>
<span class="line"><span style="color: #569CD6">global</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">scrape_interval</span><span style="color: #E6E6E6">:     </span><span style="color: #CE9178">120s</span><span style="color: #E6E6E6">  </span><span style="color: #6A9955"># By default, scrape targets every 15 seconds.</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">evaluation_interval</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">120s</span><span style="color: #E6E6E6">  </span><span style="color: #6A9955"># By default, scrape targets every 15 seconds.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># A scrape configuration containing exactly one endpoint to scrape:</span></span>
<span class="line"><span style="color: #6A9955"># Here it&#39;s Prometheus itself.</span></span>
<span class="line"><span style="color: #569CD6">scrape_configs</span><span style="color: #E6E6E6">:  </span></span>
<span class="line"><span style="color: #E6E6E6">  - </span><span style="color: #569CD6">job_name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;Spring Boot Application input&#39;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">metrics_path</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&#39;/actuator/prometheus&#39;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">scrape_interval</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">2s</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">static_configs</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #569CD6">targets</span><span style="color: #E6E6E6">: [</span><span style="color: #CE9178">&#39;host.docker.internal:8080&#39;</span><span style="color: #E6E6E6">]</span></span></code></pre></div>



<p><strong>Important notes :</strong></p>



<ul class="wp-block-list">
<li><code>scrape_interval</code> : Scrape the application and collect information every 2 seconds</li>



<li><code>targets</code> : The target is the host and port of our application.</li>



<li><code>metrics_path</code> : The path we want to scrape.</li>
</ul>



<h4 class="wp-block-heading"><strong>2.2 Start service</strong></h4>



<p>Create a docker-compose file that will bring the Prometheus docker image up and running.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    ports:
      - 9090:9090
    volumes:
      - ./prometheus/:/etc/prometheus/" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">services</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">prometheus</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">image</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prom/prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">container_name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">command</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">&#39;--config.file=/etc/prometheus/prometheus.yml&#39;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">ports</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">9090:9090</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">volumes</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./prometheus/:/etc/prometheus/</span></span></code></pre></div>



<p>the config file is mounted at the location <code>/etc/prometheus</code> and we use the location of the config file as an argument to the command.</p>



<h4 class="wp-block-heading"><strong>2.3 Test</strong></h4>



<p>1- Start spring application<br>2- Start the docker image with <code><em>docker compose up -d</em></code><br>3- Open the URL <a href="http://localhost:9090">http://localhost:9090</a> on browser.<br>4- Search for the label <code><em>http_server_requests_seconds_count</em></code></p>



<figure class="wp-block-image size-large has-custom-border is-style-default"><img fetchpriority="high" decoding="async" width="1024" height="254" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/prometheus-01-1024x254.png" alt="" class="wp-image-69" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-1024x254.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-300x74.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-768x190.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01-1536x380.png 1536w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-01.png 1910w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>In case you don&rsquo;t find the label, You can check if the job is running by navigating to <strong>« Status &gt; Targets »</strong>. You should see the state as “UP ».</p>



<figure class="wp-block-image size-large has-custom-border"><img decoding="async" width="1024" height="257" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/prometheus-02-1024x257.png" alt="" class="wp-image-70" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-1024x257.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-300x75.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-768x193.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02-1536x386.png 1536w, https://www.ji-ka.tn/wp-content/uploads/2023/09/prometheus-02.png 1591w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Now, the data is getting ingested into Prometheus every 2 seconds.<br>Although Prometheus has a decent UI, Grafana&rsquo;s Dashboard is more powerful.</p>



<h3 class="wp-block-heading"><strong>3- Grafana</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>Before starting Grafana we need to do some configuration to prepare the data source and dashboard. with that, we no longer need to create them manually.</p>



<h4 class="wp-block-heading"><strong>3.1 Datasource</strong></h4>



<p>First we need to add and configure the Prometheus data source.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="apiVersion: 1

# list of datasources that should be deleted from the database
deleteDatasources:
  - name: Prometheus
    orgId: 1

datasources:
- uid: prometheus
  orgId: 1
  name: Prometheus
  type: prometheus
  url: http://prometheus:9090 
  isDefault: true
  access: proxy
  readOnly: false
  editable: true" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">apiVersion</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># list of datasources that should be deleted from the database</span></span>
<span class="line"><span style="color: #569CD6">deleteDatasources</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  - </span><span style="color: #569CD6">name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">Prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">orgId</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">datasources</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">- </span><span style="color: #569CD6">uid</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">orgId</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">Prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">type</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">url</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">http://prometheus:9090</span><span style="color: #E6E6E6"> </span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">isDefault</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">access</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">proxy</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">readOnly</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">false</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">editable</span><span style="color: #E6E6E6">: </span><span style="color: #569CD6">true</span></span></code></pre></div>



<h4 class="wp-block-heading"><strong>3.2 Dashboard</strong></h4>



<p>To prepare the Dashboard we need two things :</p>



<ul class="wp-block-list">
<li>Dashboard config file : dashboards.yaml</li>



<li>Dashboard source file : data-observability.json</li>
</ul>



<h4 class="wp-block-heading"><strong>3.3 Start service</strong></h4>



<p>Create a docker-compose file that will bring the Grafana docker image up and running.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="  grafana:
    image: grafana/grafana
    container_name: grafana
    depends_on:
      - prometheus
    ports:
      - 3000:3000
    user: root
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - ./grafana/datasources:/etc/grafana/provisioning/datasources # data sources
      - ./grafana/dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml # dashboard setting
      - ./grafana/dashboards:/etc/grafana/dashboards # dashboard json files directory" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">grafana</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">image</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">grafana/grafana</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">container_name</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">grafana</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">depends_on</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">prometheus</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">ports</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">3000:3000</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">user</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">root</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">environment</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">GF_SECURITY_ADMIN_USER=admin</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">GF_SECURITY_ADMIN_PASSWORD=admin</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">volumes</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./grafana/datasources:/etc/grafana/provisioning/datasources</span><span style="color: #E6E6E6"> </span><span style="color: #6A9955"># data sources</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./grafana/dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml</span><span style="color: #E6E6E6"> </span><span style="color: #6A9955"># dashboard setting</span></span>
<span class="line"><span style="color: #E6E6E6">      - </span><span style="color: #CE9178">./grafana/dashboards:/etc/grafana/dashboards</span><span style="color: #E6E6E6"> </span><span style="color: #6A9955"># dashboard json files directory</span></span></code></pre></div>



<h4 class="wp-block-heading"><strong>3.4 Test</strong></h4>



<p>2- Start the docker image with <code>docker compose up -d</code><br>3- Open the URL http://localhost:3000 on browser.</p>



<figure class="wp-block-image size-large has-custom-border"><img decoding="async" width="1024" height="453" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/grafana-01-1024x453.png" alt="" class="wp-image-67" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01-1024x453.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01-300x133.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01-768x340.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-01.png 1236w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>We can see our Dashboard that we have already configured</p>



<figure class="wp-block-image size-large has-custom-border"><img loading="lazy" decoding="async" width="1024" height="497" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/09/grafana-02-1024x497.png" alt="" class="wp-image-68" style="border-width:1px" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-1024x497.png 1024w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-300x146.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-768x373.png 768w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02-1536x745.png 1536w, https://www.ji-ka.tn/wp-content/uploads/2023/09/grafana-02.png 1843w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3 class="wp-block-heading"><strong>4. Source code</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p>The complete source code of this article can be found <a href="https://github.com/JKaouech/spring-boot/tree/master/spring-prometheus">over on GitHub.</a></p>



<h3 class="wp-block-heading"><strong>Reference</strong></h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<p><a href="https://refactorfirst.com/spring-boot-prometheus-grafana">https://refactorfirst.com/spring-boot-prometheus-grafana</a></p>



<p><a href="https://dev.to/luafanti/spring-boot-monitoring-with-prometheus-operator-40g1">https://dev.to/luafanti/spring-boot-monitoring-with-prometheus-operator-40g1</a></p>



<p><a href="https://grafana.com/blog/2022/04/26/set-up-and-observe-a-spring-boot-application-with-grafana-cloud-prometheus-and-opentelemetry/">https://grafana.com/blog/2022/04/26/set-up-and-observe-a-spring-boot-application-with-grafana-cloud-prometheus-and-opentelemetry/</a></p>



<p><a href="https://betterprogramming.pub/how-to-monitor-a-spring-boot-app-with-prometheus-and-grafana-22e2338f97fc">https://betterprogramming.pub/how-to-monitor-a-spring-boot-app-with-prometheus-and-grafana-22e2338f97fc</a></p>
<p>L’article <a href="https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/">Monitoring with Prometheus and Grafana</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ji-ka.tn/monitoring-with-prometheus-and-grafana/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
