Grafana: Dynamic Retentions (InfluxDB)

Aus Wiki-WebPerfect
Version vom 24. Juni 2021, 08:40 Uhr von Admin (Diskussion | Beiträge)

(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

If you plan to store metric data in an InfluxDB for a long time, you have to downsample the data (reduce the granularity to increase the scalability). More about downsampling: InfluxDB: Downsampling
Downsampling in InfluxDB means you create a new "database/bucket" and write the downsampled data into it. This leads to problems with dashboards.

Problem explanation

We assume: You have three different retention policies/configurations.

  • telegraf: Retention = 30 Days, Granularity = raw
  • telegraf_90d: Retention = 90 Days, Granularity = 1h
  • telegraf_365d: Retention = 365 Days, Granularity = 12h

If we create a dashboard, for example in Grafana, we have to specify the bucket (InfluxDB 2.x) or the retention-policy (InfluxDB 1.x) in our query.
For example we write our query to select the bucket "telegraf". This bucket have only data 30 days back because of the retention configuration.
This means if we select an timespan in Grafana > 30 days, the data are not complete, respectively data older then 30 days are missing. This is because the data older then 30 days are not anymore stored in the bucket "telegraf" instead the data are stored downsampled in the bucket "telegraf_90d".


Solution

To prevent the mentioned problem above, we are using Grafana's "Template Variables" to create a dynamic retention selection. Following are to solution to create this, one for InfluxDB 2.x and one for 1.x.

Grafana template variables for dynamic retention policies on InfluxDB 2.x

In InfluxDB 2.x retention policies are no longer seperate objects like in InfluxDB 1.x. In InfluxDB 2.x retention is an bucket configuration. In this example we have the buckets mentioned in the "Problem explanation" and additionally an bucket "telegraf_inf" with no retention (infinity).

Grafana template variable configuration
02-dynamic retention policies.png


Grafana template variable query

//Bucketfilter to filter only buckets beginning with name telegraf..
bucketfilter = /telegraf.*/

buckets()
  |> filter(fn: (r) => r.name =~ bucketfilter)
  //convert retentionperiod from nanosecond to days
  |> map(fn: (r) => ({r with 
    retentionPeriodinDays: r.retentionPeriod / 86400000000000}) 
  )
  //replace retentionpolicy infinity with a high number in NS
  |> map(fn: (r) => ({r with
    retentionPeriod: if r.retentionPeriod == 0 then 999999999999999999
    else r.retentionPeriod})
  )
  //calculate the duration from "to" and "from" timespan and convert it to nanosecond
  |> map(fn: (r) => ({r with 
    DashboardDurationinNS: (${__to} - ${__from}) * 1000000})
  )
  |> filter(fn: (r) => 
    r.DashboardDurationinNS <= r.retentionPeriod and
    r.retentionPeriod >= uint(v: now()) - uint(v: ${__from} * 1000000)
  )
  |> sort(columns: ["retentionPeriod"], desc: false)
  |> limit(n: 1)
  |> keep(columns: ["name"]) //remove all fields except for "name"

If you want the same "dynamic retention" functionality but don't select the raw bucket "telegraf" -> Change the bucket filter to following:

bucketfilter = /telegraf.+/


Grafana Query

from(bucket: "${dynamicbucket}")
  ...your_query...


Grafana template variables for dynamic retention policies on InfluxDB 1.x

01-dynamic retention policies.png