<?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: amirreza valizade</title>
    <description>The latest articles on DEV Community by amirreza valizade (@vamirreza).</description>
    <link>https://dev.to/vamirreza</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%2F605048%2F56ae170c-ada8-40b3-9896-92e1e95270df.png</url>
      <title>DEV Community: amirreza valizade</title>
      <link>https://dev.to/vamirreza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vamirreza"/>
    <language>en</language>
    <item>
      <title>Manage Prometheus TSDB in the better way!</title>
      <dc:creator>amirreza valizade</dc:creator>
      <pubDate>Tue, 28 Mar 2023 20:38:58 +0000</pubDate>
      <link>https://dev.to/vamirreza/manage-prometheus-tsdb-in-the-better-way-236a</link>
      <guid>https://dev.to/vamirreza/manage-prometheus-tsdb-in-the-better-way-236a</guid>
      <description>&lt;p&gt;Prometheus is a powerful monitoring system that provides a simple solution to the retention of old data with &lt;code&gt;--storage.tsdb.retention.size&lt;/code&gt; and &lt;code&gt;--storage.tsdb.retention.time&lt;/code&gt; configurations. These configurations allow users to define the maximum size and age of data that should be retained in the Prometheus database. However, in some cases, users may need to store certain metrics for long-term purposes and delete unnecessaries. In this article, we will discuss how to label Prometheus targets and delete old data using the Admin API to meet such requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Labeling Prometheus Targets
&lt;/h2&gt;

&lt;p&gt;To retain specific metrics for a longer period, you need to label the targets from which Prometheus scrapes data. You can add a new label, such as &lt;code&gt;retention_time&lt;/code&gt;, to the job configuration file for each target. The value of this label should represent the duration for which you want to retain the data. For example, you can set the label to &lt;code&gt;"one-month"&lt;/code&gt;, &lt;code&gt;"three-month"&lt;/code&gt;, &lt;code&gt;"twelve-month"&lt;/code&gt;, or any other value that suits your needs.&lt;/p&gt;

&lt;p&gt;Here is an example job configuration file that adds the  &lt;code&gt;retention_time&lt;/code&gt;  label:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  - job_name: 'node_exporter'
    file_sd_configs:
    - files:
      - node_exporter.yml
    relabel_configs:
      - target_label: retention_time
        replacement: "one-month"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deleting labeled Data Using the Admin API
&lt;/h2&gt;

&lt;p&gt;Once you have labeled the targets, you can use the Prometheus Admin API to delete old data that is no longer required. The &lt;code&gt;DeleteSeries&lt;/code&gt; endpoint deletes data for a selection of series in a time range. The data still exists on disk and is cleaned up in future compactions, or you can explicitly clean it up using the &lt;code&gt;CleanTombstones&lt;/code&gt; endpoint. Enable admin api by &lt;code&gt;--web.enable-admin-api&lt;/code&gt; on Prometheus.&lt;/p&gt;

&lt;p&gt;To delete data for a particular time range, you can use the &lt;code&gt;match[]&lt;/code&gt; URL query parameter to select the series to delete, along with the start and end timestamps. Here is an example of using &lt;code&gt;DeleteSeries&lt;/code&gt; to delete data for series with &lt;code&gt;retention_time="one-month"&lt;/code&gt; label that are older than one month:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl -X PUT \
  -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={retention_time="one-month"}&amp;amp;end='"$(date +%s -d '1 month ago')"''
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;URL query parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;‍&lt;code&gt;match[]=&amp;lt;series_selector&amp;gt;&lt;/code&gt;: Repeated label matcher argument that selects the series to delete. At least one &lt;code&gt;match[]&lt;/code&gt; argument must be provided.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;start=&amp;lt;rfc3339 | unix_timestamp&amp;gt;&lt;/code&gt;: Start timestamp. Optional and defaults to minimum possible time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;end=&amp;lt;rfc3339 | unix_timestamp&amp;gt;&lt;/code&gt;: End timestamp. Optional and defaults to maximum possible time.
Not mentioning both start and end times would clear all the data for the matched series in the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: that these endpoints mark the samples from the selected series as deleted, but they do not prevent the associated series metadata from still being returned in metadata queries for the affected time range. You can use the &lt;code&gt;CleanTombstones&lt;/code&gt; endpoint to remove the deleted data from disk and clean up the existing tombstones.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl -X POST http://localhost:9090/api/v1/admin/tsdb/clean_tombstones
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have labels that define how long we need the metrics and the request which can remove them based on labels. At the end you have a script like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/sh

#calculate the end timestamp and start timestamp will be the minimum possible time
curl -X PUT -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={retention_time="one-month"}&amp;amp;end='"$(date +%s -d '1 month ago')"''
curl -X PUT -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={retention_time="three-month"}&amp;amp;end='"$(date +%s -d '3 month ago')"''
curl -X PUT -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={retention_time="twelve-month"}&amp;amp;end='"$(date +%s -d '12 months ago')"''

#clean_storage
curl -X 'PUT' 'http://127.0.0.1:9090/api/v1/admin/tsdb/clean_tombstones'   -H 'accept: */*'%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget about the automating to ensure that old metrics are regularly deleted without any manual intervention.&lt;/p&gt;

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