工作技術筆記系列:Grafana

工作開發心得 - Grafana

keywords: Grafana

What is Grafana?

Image

Grafana allows you to query, visualize, alert on and understand your metrics no matter where they are stored. Create, explore, and share dashboards with your team and foster a data-driven culture

https://github.com/grafana/grafana

Grafana 提供超漂亮的 Dashboard,可以賞心悅目的監看服務 Log

Grafana 也支援許多 Storage, Database,例如 Prometheus, Azure Data Explorer

Image

Deploy on local

docker-compose.yaml

打開 3000 port,設定 depends_on prometheus,帳密先設定:admin/pass

WebUI

打開 http://localhost:3000/ 就可以進到 Grafana

Image

How to create dashboard & panel

New Dashboard

Image

選 Prometheus

Image

  1. 輸入 PromQL
  2. 選一個適合的 visualizations 呈現圖表
  3. Run query 執行 PromQL
  4. 設定好後 Apply

Image

  1. 設定好的 visualization 表格會出現在 Dashboard 上
  2. 記得要存檔

Image

Connect to Log Analytics workspaces

如果是用 Azure 全家桶,Azure 也有提供類似 Prometheus/Grafana 的 Logs 監控平台,在 Azure Monitor → Azure Log Analytics 中

Image

Image

Grafana 支援來自 Azure Log Analytics 的資料,可以在上面寫 KQL,用起來跟在 Azure 上一模一樣

How to connect to Log Analytics workspaces

Add data source 中入 azure 找到 Azure Monitor

Image

點開後要填入 3 個欄位

Image

之後就可以在 create new panel 的地方,選 Azure Analytics Datasource,就可以在下面寫 KQL

Image

About KQL

Kusto Query Language (KQL),主要應用在 Azure Data Explorer (ADX), Azure Monitor Log Analytics

KQL 主要拿來處理資料庫,是 SQL 的替換方案,還有一篇文章在教你如何一一對應

SQL to Kusto query translation - Kusto

  • KQL 第一行指定 table,而後面每一行以 | 區隔
  • take 取出前 5 筆資料
1
2
StormEvents 
| take 5
  • project 指定欄位 (column)
1
2
3
StormEvents
| take 5
| project State, EventType, DamageProperty
  • where 指定條件
1
2
3
StormEvents
| where State == 'TEXAS' and EventType == 'Flood'
| project StartTime, EndTime, State, EventType, DamageProperty
  • sort 排序,top 結合 sort take
1
2
3
4
5
6
7
8
9
StormEvents
| where State == 'TEXAS' and EventType == 'Flood'
| sort by DamageProperty
| project StartTime, EndTime, State, EventType, DamageProperty

StormEvents
| where State == 'TEXAS' and EventType == 'Flood'
| top 5 by DamageProperty
| project StartTime, EndTime, State, EventType, DamageProperty
  • extend 新增欄位 (column)
1
2
3
4
StormEvents
| where State == 'TEXAS' and EventType == 'Flood'
| top 5 by DamageProperty desc
| extend Duration = EndTime - StartTime
  • summarize 後面接 aggregation function
  • 可以用 = 存成變數
  • count() 計算資料數量
1
2
StormEvents
| summarize TotalStorms = count() by State
  • bin() 指定區間,每…怎樣
1
2
3
4
StormEvents
| where StartTime between (datetime(2007-01-01) .. datetime(2007-12-31))
and DamageCrops > 0
| summarize EventCount = count() by bin(StartTime, 7d)

其它參考:Tutorial: Learn common Kusto Query Language operators - Kusto