Get prometheus snapshot for specific timeperiod.

218 views
Skip to first unread message

abhishek ellendula

unread,
Mar 17, 2024, 10:52:49 AM3/17/24
to Prometheus Users
Hi All

Below is the way how we generally create snapshot.

Snapshot

Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand> under the TSDB's data directory and returns the directory as response. It will optionally skip snapshotting data that is only present in the head block, and which has not yet been compacted to disk.

POST /api/v1/admin/tsdb/snapshot PUT /api/v1/admin/tsdb/snapshot

URL query parameters:

  • skip_head=<bool>: Skip data present in the head block. Optional.
$ curl -XPOST http://localhost:9090/api/v1/admin/tsdb/snapshot { "status": "success", "data": { "name": "20171210T211224Z-2be650b6d019eb54" } }

The snapshot now exists at <data-dir>/snapshots/20171210T211224Z-2be650b6d019eb54

But is there a any way or method to preserve prometheus snapshot for specific time-period.

Thanks 
Abhishek


Brian Candler

unread,
Mar 17, 2024, 2:07:51 PM3/17/24
to Prometheus Users
I'm not sure what you mean by "preserve prometheus snapshot" - AFAIK the snapshot remains forever until you delete it.

If you mean you want to delete snapshots when they reach a particular age, then you can do that yourself from a cronjob. e.g. for 90 days retention:
find <data-dir>/snapshots -mtime +90 -type f -delete

abhishek ellendula

unread,
Mar 17, 2024, 2:22:42 PM3/17/24
to Prometheus Users
Let's say that my system has been running for the past 45 days, starting on February 1st. I simply need the data for the 4th and 5th days of March.
How can i create snapshot which captures only 2 days data or any no of days.
To capture data for a specific duration, can you provide the URL query that takes time parameters, such as start and finish times. 


Brian Candler

unread,
Mar 17, 2024, 4:05:50 PM3/17/24
to Prometheus Users
> To capture data for a specific duration, can you provide the URL query that takes time parameters, such as start and finish times. 

No I can't, because there is no feature for that - as the API documentation makes clear.

Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand> under the TSDB's data directory

The snapshot for today includes all the earlier data, of course. So I presume you could take a snapshot then just delete all the blocks outside the time period of interest - but I've never tested it. You should be able to find the time ranges from the meta.json files:

root@prometheus:/var/lib/prometheus/data# cat 01HS1YQS341WWGFYZSN0N8GPR1/meta.json
{
"ulid": "01HS1YQS341WWGFYZSN0N8GPR1",
"minTime": 1709942400000,
"maxTime": 1710525600000,
...

Because these blocks are rolled up, they may cover a fairly wide time interval. For example, the above block covers 583200000 milliseconds, which is 6.75 days.

# date --date @1709942400
Sat Mar  9 00:00:00 GMT 2024
# date --date @1710525600
Fri Mar 15 18:00:00 GMT 2024

However, you should note that you won't save much disk space by trimming snapshots in this way (if that's what you're trying to do). That's because the snapshots are hardlinks to shared, immutable files. The snapshots will only take extra space once the main tsdb has started to expire blocks, and the snapshot contains blocks older than that.

Reply all
Reply to author
Forward
0 new messages