<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Andi Wibowo on Medium]]></title>
        <description><![CDATA[Stories by Andi Wibowo on Medium]]></description>
        <link>https://medium.com/@mrandiiw?source=rss-dee373d6a96d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zeie50hQZmGtS5DT5qFrrA.jpeg</url>
            <title>Stories by Andi Wibowo on Medium</title>
            <link>https://medium.com/@mrandiiw?source=rss-dee373d6a96d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:21:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mrandiiw/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Set Up Prometheus & Grafana for Golang App Monitoring]]></title>
            <link>https://medium.com/@mrandiiw/how-to-set-up-prometheus-grafana-for-golang-app-monitoring-2bd5e5c3d23e?source=rss-dee373d6a96d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bd5e5c3d23e</guid>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[prometheus]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[grafana]]></category>
            <dc:creator><![CDATA[Andi Wibowo]]></dc:creator>
            <pubDate>Fri, 06 Sep 2024 05:42:12 GMT</pubDate>
            <atom:updated>2024-09-06T05:42:12.849Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Yjq1zTbCO6AI0qtQLe-WA.png" /></figure><h3><strong>Overview</strong></h3><p>In this post, we’ll delve into the process of setting up Prometheus and Grafana to effectively monitor your Golang applications. Prometheus, a powerful open-source monitoring system, is ideal for time series data collection, while Grafana provides a visually appealing dashboard for analyzing and visualizing metrics. By combining these tools, you’ll gain valuable insights into your application’s performance and identify potential bottlenecks.</p><h3><strong>What is Prometheus &amp; Grafana?</strong></h3><p>Imagine Prometheus, a vigilant guardian of your Golang application, tirelessly collecting data points like a diligent scribe. Prometheus, a time series database, is meticulously organized, storing each metric as a timestamped record. This structured approach ensures that you can easily trace the history of your application’s performance.</p><p>Enter Grafana, the visual storyteller. This powerful tool transforms raw data into captivating narratives. With a stroke of creativity, you can craft dashboards that reveal the hidden patterns and trends within your application’s metrics. From sleek line graphs to vibrant pie charts, Grafana’s arsenal of visualizations allows you to paint a vivid picture of your application’s health.</p><h3><strong>Implementation</strong></h3><p>For this application, we’re utilizing Golang and the Gin framework. To streamline the setup process, we’ll install Prometheus and Grafana within Docker containers. This approach offers greater flexibility and ease of management. Finally, we’ll conduct thorough testing using the K6 load testing tool to ensure optimal performance and identify potential bottlenecks.</p><h3><strong>Installation</strong></h3><p>Begin by creating a docker-compose.yml file in the root directory of your project. This file will define the services and configurations for installing Prometheus and Grafana.</p><pre>version: &#39;3.8&#39;<br><br>services:<br>  prometheus:<br>    image: prom/prometheus:latest<br>    container_name: prometheus<br>    volumes:<br>      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml<br>    ports:<br>      - &quot;9090:9090&quot;<br>    networks:<br>      - monitoring<br><br>  grafana:<br>    image: grafana/grafana-oss:latest<br>    container_name: grafana<br>    ports:<br>      - &quot;3000:3000&quot;<br>    environment:<br>      - GF_SECURITY_ADMIN_PASSWORD=admin<br>    networks:<br>      - monitoring<br>    depends_on:<br>      - prometheus<br><br>networks:<br>  monitoring:<br>    driver: bridge</pre><p>Next, within the prometheus folder, create a file named prometheus.yml. This file will store the configuration settings for the Prometheus instance.</p><pre>global:<br>  scrape_interval: 15s<br><br>scrape_configs:<br>  - job_name: &#39;prometheus-go&#39;<br>    static_configs:<br>      - targets: [&#39;host.docker.internal:8080&#39;]</pre><p>The resulting folder structure will resemble the following:</p><pre>.<br>├── <strong>k6</strong><br>│   └── api_test.js<br>│   └── docker-compose.yml<br>├── <strong>middleware</strong><br>│   └── prometheus.go<br>├── <strong>prometheus</strong><br>│   └── prometheus.yml<br>├── docker-compose.yml<br>└── main.go</pre><p>To install and run Prometheus and Grafana within Docker containers, execute the following command:</p><pre>docker-compose -f docker-compose.yml up -d</pre><p>At this point, your Prometheus and Grafana instances should be running within Docker containers. We’re now ready to proceed to the next section.</p><h3><strong>Create Golang Application</strong></h3><p>Following the installation of Prometheus and Grafana, we’ll proceed to create the Golang application for this course. Begin by executing the command go mod init to initialize a new Go module. Subsequently, create a directory named middleware and a file named prometheus.go within it. in this file we will setup prometheus initialitation, creating a CounterVec label for myapp_requests_total and myapp_requests_errors_total. and create TrackMetrics function to track the metricts by label.</p><p>In this file, we’ll configure Prometheus initialization, creating a CounterVec label for myapp_requests_total and myapp_requests_errors_total. Additionally, we&#39;ll develop the TrackMetrics function to track metrics based on these labels.</p><pre>go mod init</pre><pre>package middleware<br><br>import (<br> &quot;net/http&quot;<br><br> &quot;github.com/gin-gonic/gin&quot;<br> &quot;github.com/prometheus/client_golang/prometheus&quot;<br>)<br><br>var (<br> RequestCount = prometheus.NewCounterVec(<br>  prometheus.CounterOpts{<br>   Name: &quot;myapp_requests_total&quot;,<br>   Help: &quot;Total number of requests processed by the MyApp web server.&quot;,<br>  },<br>  []string{&quot;path&quot;, &quot;status&quot;},<br> )<br><br> ErrorCount = prometheus.NewCounterVec(<br>  prometheus.CounterOpts{<br>   Name: &quot;myapp_requests_errors_total&quot;,<br>   Help: &quot;Total number of error requests processed by the MyApp web server.&quot;,<br>  },<br>  []string{&quot;path&quot;, &quot;status&quot;},<br> )<br>)<br><br>func PrometheusInit() {<br> prometheus.MustRegister(RequestCount)<br> prometheus.MustRegister(ErrorCount)<br>}<br><br>func TrackMetrics() gin.HandlerFunc {<br> return func(c *gin.Context) {<br>  path := c.Request.URL.Path<br>  c.Next()<br>  status := c.Writer.Status()<br>  RequestCount.WithLabelValues(path, http.StatusText(status)).Inc()<br>  if status &gt;= 400 {<br>   ErrorCount.WithLabelValues(path, http.StatusText(status)).Inc()<br>  }<br> }<br>}</pre><p>Next, create a main.go file within the root directory. In this file, we&#39;ll establish an endpoint using the Gin web framework and integrate the Prometheus middleware.</p><pre>package main<br><br>import (<br> &quot;net/http&quot;<br> &quot;tutor/prometheus-go/middleware&quot;<br><br> &quot;github.com/gin-gonic/gin&quot;<br> &quot;github.com/prometheus/client_golang/prometheus/promhttp&quot;<br>)<br><br>var (<br> internalServerError = &quot;Internal Server Error&quot;<br> notFound            = &quot;Not Found&quot;<br>)<br><br>func main() {<br> r := gin.Default()<br><br> middleware.PrometheusInit()<br><br> // Prometheus metrics endpoint<br> r.GET(&quot;/metrics&quot;, gin.WrapH(promhttp.Handler()))<br><br> // Middleware to track request metrics<br> r.Use(middleware.TrackMetrics())<br><br> // A simple route that increments the request count<br> r.GET(&quot;/&quot;, func(c *gin.Context) {<br>  c.String(http.StatusOK, &quot;Hello, Prometheus!&quot;)<br> })<br><br> // Another example route<br> r.GET(&quot;/get-user&quot;, func(c *gin.Context) {<br>  param := c.DefaultQuery(&quot;param&quot;, &quot;&quot;) // Get the query parameter &quot;param&quot; with a default empty value<br><br>  if param == &quot;error&quot; {<br>   c.JSON(http.StatusInternalServerError, gin.H{<br>    &quot;message&quot;: internalServerError,<br>   })<br>   return<br>  }<br><br>  if param == &quot;not-found&quot; {<br>   c.JSON(http.StatusNotFound, gin.H{<br>    &quot;message&quot;: notFound,<br>   })<br>   return<br>  }<br><br>  c.String(http.StatusOK, &quot;Success Get Users&quot;)<br> })<br><br> r.GET(&quot;/get-role&quot;, func(c *gin.Context) {<br>  param := c.DefaultQuery(&quot;param&quot;, &quot;&quot;) // Get the query parameter &quot;param&quot; with a default empty value<br><br>  if param == &quot;error&quot; {<br>   c.JSON(http.StatusInternalServerError, gin.H{<br>    &quot;message&quot;: internalServerError,<br>   })<br>   return<br>  }<br><br>  if param == &quot;not-found&quot; {<br>   c.JSON(http.StatusNotFound, gin.H{<br>    &quot;message&quot;: notFound,<br>   })<br>   return<br>  }<br><br>  c.String(http.StatusOK, &quot;Success Get Roles&quot;)<br> })<br><br> r.GET(&quot;/get-level&quot;, func(c *gin.Context) {<br>  param := c.DefaultQuery(&quot;param&quot;, &quot;&quot;) // Get the query parameter &quot;param&quot; with a default empty value<br><br>  if param == &quot;error&quot; {<br>   c.JSON(http.StatusInternalServerError, gin.H{<br>    &quot;message&quot;: internalServerError,<br>   })<br>   return<br>  }<br><br>  if param == &quot;not-found&quot; {<br>   c.JSON(http.StatusNotFound, gin.H{<br>    &quot;message&quot;: notFound,<br>   })<br>   return<br>  }<br><br>  c.String(http.StatusOK, &quot;Success Get Levels&quot;)<br> })<br><br> // Start the Gin server<br> r.Run(&quot;127.0.0.1:8080&quot;)<br>}</pre><p>If you encounter any errors, be sure to run go mod tidy to resolve module dependencies. Afterward, you can execute the application using go run main.go. Your application is now up and running.</p><h3><strong>Setup Grafana &amp; Create Dashboard</strong></h3><h4>Step 1: Access Grafana</h4><ol><li>Open your browser and navigate tohttp://localhost:3000.</li><li>Log in with the default credentials (admin/admin).</li><li>You’ll be prompted to change the password; you can set a new one or skip.</li></ol><h4>Step 2: Add Prometheus as a Data Source</h4><ol><li>Once logged in, go to <strong>Configuration</strong>(gear icon) -&gt;<strong>Data Sources</strong>.</li><li>Click <strong>Add data source</strong>.</li><li>Select <strong>Prometheus</strong>.</li><li>In the URL field, enter <a href="http://prometheus:9090.">http://prometheus:9090.</a></li><li>Click <strong>Save &amp; Test </strong>to make sure the connection is successful.</li></ol><h4>Step 3: Create Dashboard &amp; Visualization</h4><ol><li>Click <strong>Dashboard</strong>.</li><li>Select <strong>Create Dashboard</strong>.</li><li>Select Create <strong>Visualization </strong>-&gt; Select <strong>Prometheus</strong>.</li><li>Input sum(myapp_requests_total) in the PromQL query field, run it, give the title &quot;Total All Requests&quot;, and click <strong>Apply</strong>.</li><li>Input sum by (path)(myapp_requests_total) in the PromQL query field, run it, give the title &quot;Total Request Endpoint&quot;, change the visualization to a bar gauge, and click <strong>Apply</strong>.</li><li>Input sum by (path,status)(myapp_requests_errors_total) in the PromQL query field, run it, give the title &quot;Total Request Error Endpoint&quot;, change the visualization to a bar gauge, and click <strong>Apply</strong>.</li><li>Click icon save then <strong>Save </strong>to make sure the dashboard is created.</li></ol><h3>Testing using K6</h3><p>To test the endpoint application and visualize the data in your Grafana dashboard, execute the following command:</p><pre>cd k6 &amp;&amp; docker-compose up -d</pre><p>Wait for approximately 3 minutes, then execute the following command inside k6 folder to stop the testing:</p><pre>docker-compose -f docker-compose.yml down</pre><p>Now, visit your Grafana dashboard. You should see a dashboard similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5KZD-SvwsYOhTm8Xmlkwjw.png" /></figure><p>Congratulations! You’ve successfully set up Prometheus and Grafana to monitor your application.</p><p>If you’d like to view a comprehensive tutorial, please visit my YouTube channel. I’ve created a step-by-step video that covers the entire process.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FuGGkCVsqQQg%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DuGGkCVsqQQg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FuGGkCVsqQQg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/447e9ebc612d0bb1afad6f90b33c2372/href">https://medium.com/media/447e9ebc612d0bb1afad6f90b33c2372/href</a></iframe><p>Alright, I believe that’s everything. Thank you, everyone. Until next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bd5e5c3d23e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>