<?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: Zarmin</title>
    <description>The latest articles on DEV Community by Zarmin (@zarmin).</description>
    <link>https://dev.to/zarmin</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%2F1248912%2F4b036f58-a226-4238-af59-9f2b5bfc8fc6.png</url>
      <title>DEV Community: Zarmin</title>
      <link>https://dev.to/zarmin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zarmin"/>
    <language>en</language>
    <item>
      <title>Datadog PHP APM filtering</title>
      <dc:creator>Zarmin</dc:creator>
      <pubDate>Wed, 21 Feb 2024 11:59:54 +0000</pubDate>
      <link>https://dev.to/zarmin/datadog-php-apm-filtering-5266</link>
      <guid>https://dev.to/zarmin/datadog-php-apm-filtering-5266</guid>
      <description>&lt;p&gt;A client needed a solution to filter PHP APM traces before sending them to the Datadog server to reduce ingestion fees.&lt;/p&gt;

&lt;p&gt;The filter rules were to exclude everything except requests exceeding 1 second and errors (HTTP code &amp;gt;= 400, exceptions, PHP errors).&lt;/p&gt;

&lt;p&gt;They still wanted to utilize automatic instrumentation for several libraries (e.g., Laravel, Curl, PDO).&lt;/p&gt;

&lt;p&gt;(The client is still using PHP 7.4, which is end-of-life; I know - they will upgrade to 8.x later.)&lt;/p&gt;

&lt;p&gt;This auto-prepend script will filter out the specified request traces (returning from the function will retain the trace).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="c1"&gt;// dd-filter.php&lt;/span&gt;

&lt;span class="nb"&gt;register_shutdown_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// config&lt;/span&gt;
  &lt;span class="nv"&gt;$TIME_LIMIT_SEC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// exit if Datadog (DDTrace) is NOT loaded&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;class_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\DDTrace\GlobalTracer'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;class_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\DDTrace\Tag'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// get HTTP status code&lt;/span&gt;
  &lt;span class="nv"&gt;$httpResponseCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;http_response_code&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$httpResponseCode&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// keep DD trace&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// is the script exited with an error?&lt;/span&gt;
  &lt;span class="nv"&gt;$lastError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;error_get_last&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$lastError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// keep DD trace&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// get current request time&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_SERVER&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'REQUEST_TIME_FLOAT'&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// keep DD trace &lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nv"&gt;$startTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_SERVER&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'REQUEST_TIME_FLOAT'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nv"&gt;$currentTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;microtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nv"&gt;$elapsedTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$currentTime&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;$startTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$elapsedTime&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$TIME_LIMIT_SEC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// keep DD trace&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// drop DD trace&lt;/span&gt;
  &lt;span class="nv"&gt;$tracer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;\DDTrace\GlobalTracer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nv"&gt;$span&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$tracer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getActiveSpan&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nv"&gt;$span&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$span&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\DDTrace\Tag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;MANUAL_DROP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Yes, I know it would be better if the configuration, for example, &lt;code&gt;TIME_LIMIT_SEC&lt;/code&gt;, were kept in a separate file.)&lt;/p&gt;

&lt;p&gt;This can be set up in the following way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Place this script to &lt;code&gt;/etc/php/7.4/datadog-filter/dd-filter.php&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set the script for auto-prepending in &lt;code&gt;/etc/php/7.4/fpm/php.ini&lt;/code&gt;:
&lt;code&gt;auto_prepend_file = /etc/php/7.4/datadog-filter/dd-filter.php&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Restart PHP-FPM: &lt;code&gt;systemctl restart php7.4-fpm&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(The client is using their app on a Debian 12 Linux VM.)&lt;/p&gt;

</description>
      <category>datadog</category>
      <category>php</category>
      <category>apm</category>
    </item>
    <item>
      <title>Electronic load repair</title>
      <dc:creator>Zarmin</dc:creator>
      <pubDate>Tue, 16 Jan 2024 14:48:06 +0000</pubDate>
      <link>https://dev.to/zarmin/electronic-load-repair-j2o</link>
      <guid>https://dev.to/zarmin/electronic-load-repair-j2o</guid>
      <description>&lt;p&gt;I wanted my first post to be interesting, but it was a facepalm instead.&lt;/p&gt;

&lt;p&gt;While using my &lt;del&gt;aliexpress&lt;/del&gt; cheap electronic load to test a 12V lead-acid battery, I smelled some &lt;del&gt;burnt smell&lt;/del&gt; magic smoke and it showed 0A of current regardless of the position of the pot meters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf1fe8nn6eqfth3fejry.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf1fe8nn6eqfth3fejry.jpeg" alt="Original Aliexpress photo about the electronic load"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Original Aliexpress photo about the electronic load&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I removed the big heatsink and before measuring / inspecting anything, I quickly replaced the IRFP260N power mosfet, because I had one in the drawer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwb3jp0a18b2iz56mqfor.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwb3jp0a18b2iz56mqfor.jpeg" alt="Electronic load without heatsink"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Electronic load without heatsink (and with the replaced MOSFET)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After that it showed the same symptoms, 0A of current.&lt;/p&gt;

&lt;p&gt;I went through some systematic analysis steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;measuring the old and new power MOSFET (old was bad, new was good), so the replacement was neccessary, but not sufficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;inspecting the whole board under a microscope checking for burnt parts: found nothing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;measuring a few components for damage, such as shorted caps and shorted/open resistors, but found nothing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;taking photos of both sides of the PCB, merging them as semi-transparent images and try to follow the traces to reconstruct the schematics&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fngcacsw7l75mbi56njxs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fngcacsw7l75mbi56njxs.jpg" alt="Reconstructing the traces"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reconstructing the traces&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;During the reconstruction it looked like a standard electronic load design using a power MOSFET and a comparator op-amp, but I had issues drawing the schematics, because the MOSFET pins were weird...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo80yr1yl8nyb2zabhkbg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo80yr1yl8nyb2zabhkbg.jpg" alt="Part of the schematics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8995w8j3g79rs8eag7vm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8995w8j3g79rs8eag7vm.png" alt="Simple Electronic load reference circuit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;My wrong schematics reconstruction vs a good simple electronic load reference circuit... Check for the MOSFET pins&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It took me half a minute to understand I could fix the whole issue under 2 minutes, if I was aware of the orientation of the power MOSFET...&lt;/p&gt;

&lt;p&gt;After flipping it, everything was fine...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhzevro1nriicr964m0ia.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhzevro1nriicr964m0ia.jpg" alt="Facepalm"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>First test post</title>
      <dc:creator>Zarmin</dc:creator>
      <pubDate>Thu, 04 Jan 2024 17:14:02 +0000</pubDate>
      <link>https://dev.to/zarmin/first-test-post-27p1</link>
      <guid>https://dev.to/zarmin/first-test-post-27p1</guid>
      <description>&lt;p&gt;&lt;em&gt;markdown&lt;/em&gt; &lt;em&gt;test&lt;/em&gt; ~post~&lt;/p&gt;

</description>
      <category>welcome</category>
    </item>
  </channel>
</rss>
