<?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 Tom Fawcett on Medium]]></title>
        <description><![CDATA[Stories by Tom Fawcett on Medium]]></description>
        <link>https://medium.com/@tom.fawcett?source=rss-72c19dffc1b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8BnjlVWmmrtBU3YLleB6Ag.jpeg</url>
            <title>Stories by Tom Fawcett on Medium</title>
            <link>https://medium.com/@tom.fawcett?source=rss-72c19dffc1b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 05:54:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tom.fawcett/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[Time of day based notifications with Prometheus and Alertmanager]]></title>
            <link>https://medium.com/@tom.fawcett/time-of-day-based-notifications-with-prometheus-and-alertmanager-1bf7a23b7695?source=rss-72c19dffc1b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1bf7a23b7695</guid>
            <category><![CDATA[prometheus]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[metrics]]></category>
            <dc:creator><![CDATA[Tom Fawcett]]></dc:creator>
            <pubDate>Sun, 18 Nov 2018 18:43:42 GMT</pubDate>
            <atom:updated>2022-02-27T10:23:20.130Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://prometheus.io/">Prometheus</a> + <a href="https://prometheus.io/docs/alerting/alertmanager/">Alertmanager</a> is a powerful combination for monitoring. One feature it lacks, however, is proper support for time of day based notifications.</p><p>Whilst this feature is still outstanding (<a href="https://github.com/prometheus/alertmanager/issues/876">GitHub issue</a>) I have been working around it with the following solution.</p><blockquote><strong>Update</strong>: new readers may instead wish to investigate <a href="https://prometheus.io/docs/alerting/latest/configuration/#mute_time_interval">mute_time_interval</a></blockquote><h3>The problem</h3><p>I want to prevent low priority alerts being sent at nighttime.</p><p>Prometheus has a time() function. However, it will only ever return the time in UTC. Being in the UK (specifically timezone Europe/London) this works fine some of the year. But once British Summer Time (BST) takes effect, any solution relying solely on this function would need manual adjustment.</p><p>Fortunately, Prometheus’ Query Language (PromQL) is sufficiently powerful to remove the need for manual adjustment.</p><h3>The solution</h3><p>First a reminder of the definition of European Summer Time, which BST follows:</p><blockquote>European Summer Time is the variation of standard clock time that is applied in most European countries … in the period between spring and autumn, during which clocks are advanced by one hour from the time observed in the rest of the year…</blockquote><blockquote>European Summer Time begins at 01:00 UTC … on the last Sunday in March and ends at 01:00 UTC … on the last Sunday in October each year</blockquote><p><a href="https://en.wikipedia.org/wiki/Summer_Time_in_Europe"><em>source</em></a></p><p>We can follow this definition in PromQL like so:</p><pre>- record: is_european_summer_time<br>  expr: |<br>      (vector(1) and (month() &gt; 3 and month() &lt; 10))<br>      or<br>      (vector(1) and (month() == 3 and (day_of_month() - day_of_week()) &gt;= 25) and absent((day_of_month() &gt;= 25) and (day_of_week() == 0)))<br>      or<br>      (vector(1) and (month() == 10 and (day_of_month() - day_of_week()) &lt; 25) and absent((day_of_month() &gt;= 25) and (day_of_week() == 0)))<br>      or<br>      (vector(1) and ((month() == 10 and hour() &lt; 1) or (month() == 3 and hour() &gt; 0)) and ((day_of_month() &gt;= 25) and (day_of_week() == 0)))<br>      or<br>      vector(0)</pre><p>This expression works through each of the four tests, returning 1 if the test is true. If it reaches the bottom it returns 0.</p><p>A rough verbal description of the expression:</p><ol><li>Is the month after March but before October?</li><li>Is the month March, and the day after the last Sunday?</li><li>Is the month October, and the day before the last Sunday?</li><li>Is the month October, the day the last Sunday, and the time before 01:00? <strong>Or</strong> is the month March, the day the last Sunday, and the time after or equal to 01:00?</li></ol><p>If none of the above are true then it is not European Summer Time.</p><p><em>I’ll note at this point that though this expression has worked well for me it could probably be refactored.</em></p><p>With the difficult bit out of the way, we can then use our new is_european_summer_time metric to convert the UTC function time() into a Europe/London version:</p><pre>- record: europe_london_time<br>  expr: time() + 3600 * is_european_summer_time</pre><p>We then use this to create a Europe/London version of hour() :</p><pre>- record: europe_london_hour<br>  expr: hour(europe_london_time)</pre><p>And finally (for the Prometheus configuration) we create an alert that fires during our desired night period:</p><pre>- alert: QuietHours<br>  expr: europe_london_hour &gt;= 23 or europe_london_hour &lt;= 6<br>  for: 1m<br>  labels:<br>    notification: page<br>    severity: critical<br>  annotations:<br>    description: &#39;This alert fires during quiet hours. It should be blackholed by Alertmanager.&#39;</pre><p>The final pieces of the solution are on the Alertmanager side.</p><p>We prevent the QuietHours alert from ever reaching a real receiver:</p><pre>routes:<br>  - match:<br>      alertname: QuietHours<br>    receiver: blackhole</pre><pre>receivers:<br>- name: blackhole</pre><p>but still use it to inhibit, and therefore prevent the sending of, warning level alerts:</p><pre>inhibit_rules:<br>- source_match:<br>    alertname: QuietHours<br>  target_match:<br>    severity: warning</pre><h3>Conclusion</h3><p>Though Prometheus + Alertmanager may currently lack proper support for time of day based notifications, PromQL is powerful enough to compensate.</p><p>This example showed a simple inhibition based solution designed to work for timezone Europe/London. However, the principles used should transfer to other solutions and timezones.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1bf7a23b7695" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Extracting useful duration metrics from HAProxy — Prometheus & Fluentd]]></title>
            <link>https://medium.com/@tom.fawcett/extracting-useful-duration-metrics-from-haproxy-prometheus-fluentd-2be9832ff702?source=rss-72c19dffc1b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2be9832ff702</guid>
            <category><![CDATA[fluentd]]></category>
            <category><![CDATA[haproxy]]></category>
            <category><![CDATA[metrics]]></category>
            <category><![CDATA[prometheus]]></category>
            <category><![CDATA[monitoring]]></category>
            <dc:creator><![CDATA[Tom Fawcett]]></dc:creator>
            <pubDate>Mon, 05 Mar 2018 20:25:51 GMT</pubDate>
            <atom:updated>2018-04-17T17:12:39.252Z</atom:updated>
            <content:encoded><![CDATA[<h3>Background</h3><p>One of HAProxy’s killer features is its rich metric output.</p><p>HAProxy outputs over <a href="https://cbonte.github.io/haproxy-dconv/1.8/management.html#9.1">80 different metrics in a CSV format</a>, which can be parsed by numerous tools. My personal favourite being <a href="https://github.com/prometheus/haproxy_exporter">Prometheus HAProxy Exporter</a>. Once the metrics are in <a href="https://prometheus.io/">Prometheus</a> it is easy to create monitoring for Rate and Error, but the third letter of <a href="https://www.weave.works/blog/the-red-method-key-metrics-for-microservices-architecture/">RED</a> — Duration — is somewhat absent.</p><p>HAProxy exposes the following duration metrics:</p><blockquote>58. qtime [..BS]: the average queue time in ms over the 1024 last requests<br>59. ctime [..BS]: the average connect time in ms over the 1024 last requests<br>60. rtime [..BS]: the average response time in ms over the 1024 last requests (0 for TCP)<br>61. ttime [..BS]: the average total session time in ms over the 1024 last requests</blockquote><p>On the surface these sound potentially useful, but in practice I have found them to be an incredibly vague representation of the true duration performance of the system. This shouldn’t be surprising considering they are all averages over the last 1024 requests.</p><p>Fortunately HAProxy has another killer feature — rich logging.</p><h3>Metrics from logs</h3><p>HAProxy logs via syslog. This provides a great deal of flexibility and potential for log shipping and processing. The HAProxy docs have plenty of <a href="https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#8.2.3">detail about the field format and configuration</a>, but the key field for this challenge is:</p><blockquote>Ta: total active time for the HTTP request, between the moment the proxy received the first byte of the request header and the emission of the last byte of the response body.</blockquote><p>Now that sounds useful.</p><p>A Prometheus <a href="https://prometheus.io/docs/practices/histograms/">histogram</a> of <strong>Ta</strong> would complement the Rate and Error metrics extracted via HAProxy Exporter nicely. As I already happen to be processing these logs via <a href="https://www.fluentd.org/">Fluentd</a>, I can use the <a href="https://github.com/fluent/fluent-plugin-prometheus">Fluentd Prometheus plugin</a> to create such a histogram.</p><h3>Config samples</h3><p>Now for some config samples showing how to create this histogram.</p><h4>HAProxy config</h4><p>(Showing only log config)</p><pre>global<br>    log   localhost:5140  format rfc5424  local0<br>...</pre><pre>defaults<br>    mode http<br>    log global<br>    option httplog<br>...</pre><h4>Fluentd config</h4><p>With three plugins installed: <a href="https://github.com/fluent/fluent-plugin-rewrite-tag-filter">fluent-plugin-rewrite-tag-filter</a>, <a href="https://github.com/fluent/fluent-plugin-prometheus">fluent-plugin-prometheus</a>, and <a href="https://github.com/repeatedly/fluent-plugin-record-modifier">fluent-plugin-record-modifier</a>.</p><pre># Input and parse HAProxy syslog.<br>&lt;source&gt;<br>  @type syslog<br>  tag haproxy<br>  port 5140<br>  bind 127.0.0.1<br>  &lt;parse&gt;<br>    @type regexp<br>  # Matches HAProxy 1.8 rfc5424 HTTP logs, with a catch-all for other log types.<br>    expression /^\d{1,2} (?&lt;syslog_time&gt;[^ ]+) (?&lt;syslog_host&gt;[^ ]+) (?&lt;ps&gt;\w+) (?&lt;pid&gt;\d+) ([^ ]+) ((\[(.*)\]|[^ ])) ((?&lt;client_ip&gt;[\w\.]+):(?&lt;client_port&gt;\d+) \[(?&lt;req_date&gt;.+)\] (?&lt;frontend&gt;[\w\~-]+) (?&lt;backend&gt;[\w-]+)\/(?&lt;backend_server&gt;[^ ]+) (?&lt;tr_receive_time&gt;[^\/]+)\/(?&lt;tw_queue_time&gt;[^\/]+)\/(?&lt;tc_connect_time&gt;[^\/]+)\/(?&lt;tr_resp_time&gt;[^\/]+)\/(?&lt;ta_active_time&gt;[^\/]+) (?&lt;status_code&gt;\d+) (?&lt;bytes_read&gt;\d+) (?&lt;req_cookie&gt;[^ ]+) (?&lt;res_cookie&gt;[^ ]+) (?&lt;term_state&gt;[\w-]+) (?&lt;actconn&gt;\d+)\/(?&lt;feconn&gt;\d+)\/(?&lt;beconn&gt;\d+)\/(?&lt;srv_conn&gt;\d+)\/(?&lt;retries&gt;\d+) (?&lt;srv_queue&gt;\d+)\/(?&lt;backend_queue&gt;\d+) \{?(?&lt;req_headers&gt;[^}]*)\}? ?\{?(?&lt;res_headers&gt;[^}]*)\}? ?&quot;(?&lt;request&gt;[^&quot;]*)&quot;|(?&lt;message&gt;.+))/<br>    types actconn:integer,backend:string,backend_queue:integer,beconn:integer,backend_server:string,bytes_read:integer,client_ip:string,client_port:integer,feconn:integer,frontend:string,message:string,pid:integer,ps:string,req_date:string,req_headers:string,req_cookie:string,request:string,res_headers:string,retries:string,res_cookie:string,srv_conn:integer,srv_queue:integer,status_code:integer,syslog_time:string,term_state:string,tc_connect_time:integer,tr_resp_time:integer,tr_receive_time:integer,ta_active_time:integer,tw_queue_time:integer<br>  &lt;/parse&gt;<br>&lt;/source&gt;<br><br># Tag our log types.<br>&lt;match haproxy&gt;<br>  @type rewrite_tag_filter<br>  &lt;rule&gt;<br>    key     frontend<br>    pattern .<br>    tag haproxy.http<br>  &lt;/rule&gt;<br>  &lt;rule&gt;<br>    key     message<br>    pattern .<br>    tag haproxy.general<br>  &lt;/rule&gt;<br>&lt;/match&gt;<br><br># Setup the Prometheus plugin.<br>&lt;source&gt;<br>  @type prometheus<br>  bind 0.0.0.0<br>  port 24231<br>  metrics_path /metrics<br>&lt;/source&gt;<br><br># Following Prometheus best practice by converting to base unit.<br>&lt;filter haproxy.http&gt;<br>  @type record_modifier<br>  &lt;record&gt;<br>    ta_active_time_seconds ${record[&quot;ta_active_time&quot;].to_f / 1000}<br>  &lt;/record&gt;<br>&lt;/filter&gt;<br><br># Create the histogram.<br>&lt;filter haproxy.http&gt;<br>  @type prometheus<br>  &lt;metric&gt;<br>    name http_request_duration_seconds<br>    type histogram<br>    desc A histogram of the request duration. <br>    key ta_active_time_seconds<br># Buckets chosen in mind of relevant SLO.<br>    buckets 0.1, 0.5, 1, 5, 10, 60<br>    &lt;labels&gt;<br>      tag ${tag}<br># Could also label by backend server if desired.<br>      backend ${backend}<br>    &lt;/labels&gt;<br>  &lt;/metric&gt;<br>&lt;/filter&gt;</pre><p>Scraping Fluentd on port 24231 then shows:</p><pre># TYPE http_request_duration_seconds histogram<br># HELP http_request_duration_seconds A histogram of the request duration.<br>http_request_duration_seconds_bucket{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;,le=&quot;0.1&quot;} 362.0<br>http_request_duration_seconds_bucket{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;,le=&quot;0.5&quot;} 362.0<br>http_request_duration_seconds_bucket{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;,le=&quot;1&quot;} 812.0<br>http_request_duration_seconds_bucket{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;,le=&quot;5&quot;} 950.0<br>http_request_duration_seconds_bucket{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;,le=&quot;10&quot;} 986.0<br>http_request_duration_seconds_bucket{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;,le=&quot;60&quot;} 986.0<br>http_request_duration_seconds_bucket{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;,le=&quot;+Inf&quot;} 986.0<br>http_request_duration_seconds_sum{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;} 1074.0<br>http_request_duration_seconds_count{tag=&quot;haproxy.http&quot;,backend=&quot;foobar&quot;} 986.0</pre><p>Perfect!</p><h3>Using the metrics</h3><p>There are a number of good examples in the <a href="https://prometheus.io/docs/practices/histograms/">Prometheus histogram docs</a> of histogram based queries, including an approximation of Apdex score. With that in mind I will include only one example:</p><pre>histogram_quantile(0.50,</pre><pre>sum(rate(http_request_duration_seconds_bucket{job=&quot;fluentd&quot;,backend=&quot;foobar&quot;}[5m])) by (le)</pre><pre>)</pre><p>That returns the 50th percentile (median) of request durations over the last 5 minutes for backend foobar.</p><h3>Conclusion</h3><p>So to summarise — Fluentd combined with its Prometheus plugin allows you to create Prometheus duration histograms out of HAProxy logs. From these you can easily calculate percentiles and means, allowing effective monitoring of duration performance.</p><p>I was only interested in a single HAProxy HTTP log field — <strong>Ta</strong>, but the theory should be applicable to TCP logs and other duration fields. Similarly you could configure additional labels to gain further insights — e.g. labelling by <strong>status_code </strong>would allow a more accurate Apdex score.</p><p>If Fluentd doesn’t take your fancy, you could have a go with <a href="https://github.com/google/mtail">mtail</a>; though that is file orientated and lacks first class support for histograms.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2be9832ff702" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>