0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Grafana ダッシュボードのクエリを抜き出すツールの検討

Last updated at Posted at 2024-10-25

Grafana ダッシュボードでパネルに設定したクエリが多数に膨れた場合に、いちいちパネルを見るのが辛くなってきます。
JSON を見るのも辛くて嫌になってしまいます。
そこで、エクスポートした JSON からクエリを抜き出すツールについて検討してみました。

役立ち情報

  • 手軽に Grafana 参照
    play.grafana.orgを参照すると大まかな挙動把握ができて重宝しています。

  • Grafana ダッシュボードの情報をエクスポート
    下記手順で Grafana ダッシュボードの情報をエクスポートでき、他の環境にも移行できます。

    1. Grafana ダッシュボードへ遷移
    2. Grafana ダッシュボードの上部にある Share ボタンか Share マークをクリック
    3. Export, Save to file をクリック

目的

Grafana ダッシュボードの情報をエクスポートすると、クエリ情報も出力されますが一行なので辛いです。

【前略】
          "metricColumn": "none",
          "rawQuery": false,
          "rawSql": "SELECT\n  $__timeGroupAlias(payment_date,1h,NULL),\n  sum(amount) AS \"amount\"\nFROM payment\nWHERE\n  $__timeFilter(payment_date)\nGROUP BY 1\nORDER BY $__timeGroup(payment_date,1h,NULL)",
【後略】

下記がメインの目的なので、あると助かるが、正直「どうでもいいツール」に分類されるとは思います。

  • クエリの問題が発覚したときに、類似する問題点を持ったパネルを一発 grep で把握したい
  • 関連修正がもれなく行えたか一発 grep で把握したい
  • 出力結果は git にでも突っ込んで構成管理に役立てたい(Grafana でもバージョン管理できるが、json ベースで見にくい)

やったこと

下記でやりたいことは満たせています。
ただ、昨今こうしたものをつくるのにも様々選択肢があると思います。
生成 AI 絡めるとどうだったか、など次に触れようと思います。
Grafana ダッシュボードのクエリを抜き出すツールを生成 AI に作成してもらった

因みに jq を使わず無理矢理感のある状態は意図的とはなります。
どうでもいいツールなので、git bash だけあれば動くものがよく、jq 利用パターンを生成 AI 利用で検討する予定です。

#!/bin/bash

# JSONファイルのパス
json_file="./xxx.json"

# 一時ファイル
temp_file="filtered_json.tmp"

# 出力ディレクトリの作成
output_dir="sql_outputs"
mkdir -p "$output_dir"

# 必要な行だけ抽出("title"と"rawSql"を含む行)
grep -E '"title":|"rawSql":' "$json_file" > "$temp_file"

# 変数
panel_title=""
raw_sql_content=""

# 抽出済みの一時ファイルから1行ずつ処理
while IFS= read -r line
do

    # パネルタイトルの取得
    if echo "$line" | grep -q '"title":'; then
        panel_title=$(echo "$line" | sed 's/.*"title": "\(.*\)",/\1/')

        # ファイル名として扱うため、スペースをアンダースコアに変換
        panel_title=$(echo "$panel_title" | sed 's/[ \t:]/_/g')

        # tmp.sqlがあればリネーム
        if [[ -f "${output_dir}/tmp.sql" ]]; then
            mv "${output_dir}/tmp.sql" "${output_dir}/${panel_title}.sql"
        fi
    fi

	# rawSqlの内容を処理中
    if echo "$line" | grep -q '"rawSql":'; then
        raw_sql_content=$(echo "$line" | sed 's/.*"rawSql": "\(.*\)",/\1/')

        # クエリの改行を反映
        raw_sql_content=$(echo "$raw_sql_content" | sed 's/\\n/\n/g')

        # 出力ファイル名の作成 (パネルタイトル)
        file_name="${output_dir}/tmp.sql"

        # クエリ内容を書き出し
        echo -e "$raw_sql_content" > "$file_name"
        echo "SQL query written to $file_name"

        # 変数をリセット
        raw_sql_content=""
    fi

done < "$temp_file"

# 一時ファイルを削除
rm "$temp_file"

echo "All SQL queries have been extracted."
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?