<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: George Iheanyi</title>
    <description>The latest articles on DEV Community by George Iheanyi (@georgemeka).</description>
    <link>https://dev.to/georgemeka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2028035%2F8536fd24-47d2-45ed-ac31-7e76861962e9.jpg</url>
      <title>DEV Community: George Iheanyi</title>
      <link>https://dev.to/georgemeka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/georgemeka"/>
    <language>en</language>
    <item>
      <title>How to Monitor Metrics in Laravel Using Prometheus and Grafana</title>
      <dc:creator>George Iheanyi</dc:creator>
      <pubDate>Mon, 24 Mar 2025 07:37:26 +0000</pubDate>
      <link>https://dev.to/georgemeka/how-to-monitor-metrics-in-laravel-using-prometheus-and-grafana-1do9</link>
      <guid>https://dev.to/georgemeka/how-to-monitor-metrics-in-laravel-using-prometheus-and-grafana-1do9</guid>
      <description>&lt;p&gt;Monitoring and logging are crucial aspects of any sound or robust infrastructure or system.  Monitoring is the process of continuously collecting, analyzing and visualizing data from a system, application, or infrastructure to ensure that it is functioning correctly and efficiently.  This helps to mitigate any issues before they impact users or business operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Importance of Monitoring in Software Applications&lt;/strong&gt;&lt;br&gt;
Monitoring helps to maintain the health, performance, and security of an application.  Some of the reasons why monitoring is essential for any application include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures your application is always available and reliable.  It prevents downtime by detecting failures early.&lt;/li&gt;
&lt;li&gt;Identifies slow database queries, API calls, or resource bottlenecks.&lt;/li&gt;
&lt;li&gt;Enhances security and compliance by monitoring logs for potential security threats.&lt;/li&gt;
&lt;li&gt;Helps in debugging and troubleshooting.&lt;/li&gt;
&lt;li&gt;Enables scalability and capacity planning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is Prometheus&lt;/strong&gt;&lt;br&gt;
Prometheus is an open-source systems monitoring and alerting toolkit that collects and stores its metrics as time series data i.e. it stores metrics information with the timestamp at which it was recorded, alongside optional key-value pairs called labels.&lt;/p&gt;

&lt;p&gt;Metrics are numerical measurements, and time series refers to the recording of changes over time.  Metrics help you to understand why your application works the way it does.  For example, if your application is running slow, you will need some information.  If your application has a high number of requests, it can make your system slow.  And the only way to know is to have a request count metric.  With this, you can determine the cause and proactively increase the number of servers to handle the load.&lt;/p&gt;

&lt;p&gt;Prometheus provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time metrics collection&lt;/li&gt;
&lt;li&gt;Custom alerts for anomalies&lt;/li&gt;
&lt;li&gt;Detailed dashboards with Grafana&lt;/li&gt;
&lt;li&gt;Easy integration with laravel and other systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How Prometheus Works&lt;/strong&gt;&lt;br&gt;
Prometheus operates on a pull-based model where:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your laravel application exposes metrics.  This is done by creating an endpoint (e.g. /metrics) in your laravel application to expose application-specific metrics.&lt;/li&gt;
&lt;li&gt;Prometheus periodically fetches data from the /metrics endpoint.&lt;/li&gt;
&lt;li&gt;This data is stored in prometheus and can be queried using PromQL.&lt;/li&gt;
&lt;li&gt;Finally, you can choose to use Grafana to visualize your data or just view in prometheus.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Prometheus in Your Laravel Application&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Step 1: *&lt;/em&gt; Install Prometheus Exporter&lt;br&gt;
To expose your application's metrics, you need a Prometheus client library for PHP.  And we will be using promphp/prometheus_client_php in this lesson.&lt;/p&gt;

&lt;p&gt;Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require promphp/prometheus_client_php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;  Configure Storage for Prometheus&lt;br&gt;
We will be using Redis because it is highly recommended for production.&lt;/p&gt;

&lt;p&gt;Install Redis on your system&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
sudo apt-get install redis

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On your command prompt, start redis:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;redis-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify Redis is running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;redis-cli ping
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it returns PONG, redis is active.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install Redis and Predis in Laravel&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require predis/predis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: ** Define Metrics in Laravel&lt;br&gt;
Create a new **MetricsController&lt;/strong&gt; to expose Prometheus metrics&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Prometheus\CollectorRegistry;
use Prometheus\RenderTextFormat;
use Prometheus\Storage\Redis;
use Illuminate\Routing\Controller;

class MetricsController extends Controller
{
    public function metrics()
    {
        // Configure Redis storage
        $adapter = new Redis([
            'host' =&amp;gt; '127.0.0.1',
            'port' =&amp;gt; 6379
        ]);
        $registry = new CollectorRegistry($adapter);

        // Define a counter metric for HTTP requests
        $counter = $registry-&amp;gt;getOrRegisterCounter(
            'app', 'http_requests_total', 'Total number of HTTP requests', ['method', 'route']
        );
        $counter-&amp;gt;incBy(1, [request()-&amp;gt;method(), request()-&amp;gt;path()]);

        // Render Prometheus metrics in the required format
        $renderer = new RenderTextFormat();
        return response($renderer-&amp;gt;render($registry-&amp;gt;getMetricFamilySamples()))
            -&amp;gt;header('Content-Type', RenderTextFormat::MIME_TYPE);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Step 4: *&lt;/em&gt; Create a Route to Expose Metrics&lt;br&gt;
Add the following route in routes/api.php&lt;br&gt;
&lt;code&gt;Route::get('/metrics', [MetricsController::class, 'metrics']);&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt;  Install and Configure Prometheus&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Download Prometheus
For Linux:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wget https://github.com/prometheus/prometheus/releases/download/v2.50.0/prometheus-2.50.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Windows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit &lt;a href="https://prometheus.io/download/" rel="noopener noreferrer"&gt;https://prometheus.io/download/&lt;/a&gt; to download the latest version of prometheus for windows&lt;/li&gt;
&lt;li&gt; Extract the downloaded file into your C drive.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt; Configure Prometheus to scrape Laravel metrics. Edit the &lt;code&gt;prometheus.yml&lt;/code&gt; file inside your prometheus folder:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;global:
  scrape_interval: 5s  # How often Prometheus scrapes data

scrape_configs:
  - job_name: 'laravel_app'
    metrics_path: '/metrics'
       static_configs:
      - targets: ['localhost:8000'] # Change to your Laravel app’s hostname and port

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt; Run Promtheus&lt;/li&gt;
&lt;li&gt;cd into your Prometheus folder you installed above.  Then run:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./prometheus --config.file=prometheus.yml

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt;  Verify Prometheus is Scraping Laravel&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit Promtheus UI: &lt;a href="http://localhost:9090" rel="noopener noreferrer"&gt;http://localhost:9090&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Query Metrics: Enter app_http_requests_total to see the recorded requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Step 7: *&lt;/em&gt; Visualize Metrics in Grafana (Optional)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Grafana:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wget https://dl.grafana.com/oss/release/grafana-10.0.1.linux-amd64.tar.gz
tar -zxvf grafana-10.0.1.linux-amd64.tar.gz
cd grafana-10.0.1
./bin/grafana-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Open Grafana UI (&lt;a href="Http://localhost:3000"&gt;Http://localhost:3000&lt;/a&gt;) and add Prometheus as a data source (&lt;a href="http://localhost:9090" rel="noopener noreferrer"&gt;http://localhost:9090&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Create dashboards to visualize your Laravel application metrics.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
By following these steps, you now have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Laravel exposing metrics via /metrics&lt;/li&gt;
&lt;li&gt;Redis storing Prometheus data&lt;/li&gt;
&lt;li&gt;(Optional) Grafana visualizing application performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for reading.  In my next article, I will dive deep into how to add more custom metrics like histograms, counters, gauge etc.  If you love this article, don't forget to follow and also save for future use.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>prometheus</category>
    </item>
    <item>
      <title>Top 10 Lessons from 40 Years of Writing Code: What Every Software Engineer Should Know</title>
      <dc:creator>George Iheanyi</dc:creator>
      <pubDate>Mon, 18 Nov 2024 14:32:53 +0000</pubDate>
      <link>https://dev.to/georgemeka/top-10-lessons-from-40-years-of-writing-code-what-every-software-engineer-should-know-1o40</link>
      <guid>https://dev.to/georgemeka/top-10-lessons-from-40-years-of-writing-code-what-every-software-engineer-should-know-1o40</guid>
      <description>&lt;p&gt;I read an essay written by Lars Wirzenius in celebration of his 40 years of programming. Lars started his programming journey when he was 14 years old and has been building software till date. &lt;/p&gt;

&lt;p&gt;In his early years of programming, Lars was told programming was meant for only young people (the cool kids) and that by 25 years, he'd be too old to write codes. Well... Lars still writes code till date, after 40 years and says he is not stopping any time soon. There are some lessons I picked from his 40 years experience in his essay:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The fundamental, core skills to have as a programmer is communication and collaboration. Team work can be a force multiplier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To do good work, you have to take care of yourself. If you are tired or are too stressed, you will make mistakes and inevitably, bad decisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For every collaboration or team project, governance must be explicit. There must be a clear-cut definition of roles. Uncertainty about power results in confusion and quarrel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Software development is a political and ethical act. Make sure the software you build resonates with your values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To build quality software, enable diversity of thought among the people contributing to it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mistakes are inevitable. Make room for people's mistakes, and expect that from others too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write things down. Writing enables clarity of thoughts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learn to have good meetings. Prepare for meetings so you don't waste people's time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simplify things as much as you can. Simple, obvious code is easier to write, easier to get to work, and easier to change without breaking it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ask for help when you need it or get stuck. Accept help when offered.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope this helped.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>softwareengineering</category>
      <category>100daysofcode</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>90/10 Barbell Strategy in Tech</title>
      <dc:creator>George Iheanyi</dc:creator>
      <pubDate>Tue, 17 Sep 2024 21:30:25 +0000</pubDate>
      <link>https://dev.to/georgemeka/9010-barbell-strategy-in-tech-1a29</link>
      <guid>https://dev.to/georgemeka/9010-barbell-strategy-in-tech-1a29</guid>
      <description>&lt;p&gt;The 90/10 barbell strategy, as posited by Nassim Taleb, is a powerful framework that can be adapted to programming, startups, and tech as a whole. The idea, which balances extreme caution with calculated risk, suggests allocating 85-90% of your assets to safe investments and the remaining 10-15% to high-risk, high-reward ventures. This dual approach ensures that you preserve the bulk of your resources while still exposing yourself to the possibility of significant upside.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Applying the Barbell Strategy to Programming and Tech&lt;/strong&gt;&lt;br&gt;
In tech, innovation is constant. A new framework or tool seems to gain traction every few months, leading to a "fear of missing out" on the next big thing. But as with investing, an all-in approach is reckless. The barbell strategy suggests that you should continue to focus on stable, proven technologies for 85-90% of your work. These are the tools that have stood the test of time—well-documented, widely adopted, and battle-tested in production.&lt;/p&gt;

&lt;p&gt;However, it's equally important to dedicate 10-15% of your time or energy to exploring emerging technologies. This way, you're not caught off guard by industry shifts, and if a new technology turns out to be a game changer, you've already built up some familiarity with it. But the key is proportionality—dip your toes in, don't dive headfirst.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Breaking into Tech with the Barbell Approach&lt;/strong&gt;&lt;br&gt;
For those transitioning into tech from other industries, the barbell strategy also applies. Keep your current job while learning new tech skills, dedicating 10-15% of your time to mastering those skills and applying to new roles. This way, you hedge against the uncertainty of landing a tech role quickly, while maintaining financial stability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Startup Ventures and the Barbell Strategy&lt;/strong&gt;&lt;br&gt;
The same logic holds for entrepreneurship. Don't go all-in on a startup venture from the outset. Instead, invest a small portion—10-15% of your time or resources—while keeping a stable income source. This reduces the risk of failure affecting your entire livelihood. If the startup gains traction, you can gradually scale your involvement. If it fails, you’ve limited your losses and can move on to the next idea without a catastrophic impact.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The barbell strategy is fundamentally about managing risk while positioning yourself to seize high-reward opportunities. Applied to tech, it allows you to stay grounded in proven methods and technologies while still keeping an eye on the horizon for innovation. It's a powerful framework to navigate both personal development and professional ventures in the fast-evolving tech landscape.&lt;/p&gt;

</description>
      <category>developer</category>
      <category>100daysofcode</category>
      <category>learning</category>
      <category>career</category>
    </item>
    <item>
      <title>How to Solve Hydration Errors in Next.js</title>
      <dc:creator>George Iheanyi</dc:creator>
      <pubDate>Fri, 06 Sep 2024 11:56:52 +0000</pubDate>
      <link>https://dev.to/georgemeka/hydration-error-4n0k</link>
      <guid>https://dev.to/georgemeka/hydration-error-4n0k</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;"Hydration failed because the server-rendered HTML did not match the client...."&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you've been using Next.js to build applications, you must have gotten the above error or something similar.  It is called Hydration Error.  &lt;/p&gt;

&lt;p&gt;I used to get this error when I first started out with Next.js but didn't know what to do and never cared to look it up because it wasn't really affecting my code at the time.  Not until I was asked by an interviewer, what I'd do to solve an hydration error in Next.js.  I was dumbfounded because now it was not a case of the interviewer trying to bring me down, but a case of nonchalance and sheer ignorance.  I don't want you to be like me in your next interview.  So let's discuss on Hydration.&lt;/p&gt;

&lt;p&gt;Hydration is the process where static HTML becomes interactive by adding Javascript to it.  So normally when a webpage is rendered on the server, it loses its interactivity and event handlers before getting to the client.  React is responsible for building the component tree on the client.  This is called hydration, because it adds the interactivity and event handlers that were lost when the HTML was server-side rendered.  React compares it with the actual server-side rendered DOM.  They must be the same so React can adopt it.  &lt;/p&gt;

&lt;p&gt;If there is a mismatch between the page we have and what the client-side thinks we should have, we get a "Hydration Error."  Some common hydration error causes include: Incorrect HTML element nesting, different data used for rendering, using browser-only APIs, side effects, etc.&lt;/p&gt;

&lt;p&gt;Whatever the cause, you'd have to figure it out by reading the error message that you get.  Possible solutions include;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Using useEffect to run on the client only.&lt;/strong&gt;&lt;br&gt;
To prevent a hydration mismatch, make sure the component renders the same content on the server-side as it does on the initial client-side render.  You can use the useEffect hook to intentionally render content on the client.  See example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from 'react'

export default function App() {
  const [isClient, setIsClient] = useState(false)

  useEffect(() =&amp;gt; {
    setIsClient(true)
  }, [])

  return &amp;lt;h1&amp;gt;{isClient ? 'This is never prerendered' : 'Prerendered'}&amp;lt;/h1&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Disabling server-side rendering on specific components.&lt;/strong&gt;&lt;br&gt;
You can use the &lt;a href="https://nextjs.org/docs/app/building-your-application/optimizing/lazy-loading#skipping-ssr" rel="noopener noreferrer"&gt;disable prerendering&lt;/a&gt; feature on Next.js on specific components.  This can prevent hydration mismatches.  See example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import dynamic from 'next/dynamic'

const NoSSR = dynamic(() =&amp;gt; import('../components/no-ssr'), { ssr: false })

export default function Page() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;NoSSR /&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.  Using Suppress Hydration Warning&lt;/strong&gt;&lt;br&gt;
There are times when content will inevitably be different on the server and client, instances like timestamp.  What you can do is to silence the hydration mismatch warning by adding suppressHydrationWarning={true} to the element.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;time datetime="2016-10-25" suppressHydrationWarning /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So with these three methods, you should be able to resolve that hydration error the next time it pops up when building on Next.js.&lt;/p&gt;

&lt;p&gt;Don't forget to subscribe to my page to get more eye-opening content on Programming.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>nextjs</category>
      <category>react</category>
    </item>
  </channel>
</rss>
