LoginSignup
0
0

More than 3 years have passed since last update.

GrafanaのVariables Queryに$timeFilterを使う方法

Last updated at Posted at 2020-08-18

Template variablesとは

公式ドキュメントより。

A variable is a placeholder for a value. You can use variables in metric queries and in panel titles. So when you change the value, using the drop-down list at the top of the dashboard, your panel’s metric queries will change to reflect the new value.
Variables allow you to create more interactive and dynamic dashboards. Instead of hard-coding things like server, application, and sensor names in your metric queries, you can use variables in their place. Variables are displayed as drop-down lists at the top of the dashboard. These drop-downs make it easy to change the data being displayed in your dashboard.

ダッシュボード読み込み時にvariableを動的生成する場合、Query variableを使えばよい。
InfluxDBの場合は通常Query欄に以下のようにクエリを指定する。

SHOW TAG VALUES FROM "<measurement>" WITH KEY = "<tag key>"

Template variableに時間による絞り込みを加える

上記のSHOW TAG VALUESクエリの場合、過去すべての<tag key>の値が表示される。過去の値すべてが不要なケースにおいて、いつまでもTemplate variablesに値が残ってしまうのは都合が悪い。

SELECTクエリと同様にSHOW TAG VALUESクエリが以下のようなWHERE句を解釈できればよいか、あいにくサポートされていない。

SHOW TAG VALUES FROM "<measurement>" WITH KEY = "<tag key>" WHERE $timeFilter
  or
SHOW TAG VALUES FROM "<measurement>" WITH KEY = "<tag key>" WHERE time > now() - 1h

ワークアラウンドとして、SELECTクエリを使っう以下の方法ならうまく機能するようだ。

SELECT "<tag value>" FROM ( SELECT "<field>", "<tag value>" FROM "<measurment>" WHERE $timeFilter )
  or
SELECT "<tag value>" FROM ( SELECT "<field>", "<tag value>" FROM "<measurment>" WHERE time > now() -1h )
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0