作為當(dāng)前最流行的圖形化運(yùn)維監(jiān)控解決方案之一,Grafana 提供了一個(gè)靈活易用的界面,可以連接多種不同的數(shù)據(jù)源,包括時(shí)序數(shù)據(jù)庫(Time Series Database)、云服務(wù)、監(jiān)控系統(tǒng)等,然后從這些數(shù)據(jù)源中提取數(shù)據(jù)并實(shí)時(shí)地將其可視化。為了給用戶打造更豐富的可視化方案,TDengine 在開源不久就提供了對(duì) Grafana 的支持,此后也在不斷升級(jí)改造 TDengine Grafana 插件,還推出了基于 Grafana 的零依賴監(jiān)控解決方案 TDinsight。本篇文章將以 tdengine-datasource 為例介紹 Grafana 插件開發(fā)。
實(shí)際上,tdengine-datasource 是 TDengine 為 Grafana 打造的 datasource 插件,它能夠從 TDengine 讀取數(shù)據(jù),并展示到 Grafana。

開發(fā)環(huán)境準(zhǔn)備
- Grafana
- go
- Mage:https://github.com/magefile/mage
- nodejs
- yarn
Grafana 配置
首先下載 Grafana 并解壓,修改 conf/default.ini 配置。
- 修改插件加載路徑。Grafana 插件會(huì)默認(rèn)加載在 plugins = data/plugins,這里需要修改為自己插件的地址。這里通常改為插件項(xiàng)目下的 dist 目錄。
[paths]
plugins = /path/to/Grafana-plugins
- 修改 allow_loading_unsigned_plugins 配置,配置項(xiàng)的值為插件的名字,允許 Grafana 加載 unsigned 的插件。
allow_loading_unsigned_plugins = my_plugin_name
新建插件
使用 grafna-create-plugin 創(chuàng)建插件,然后根據(jù)提示新建插件。
$ npx @Grafana/create-plugin
? What is going to be the name of your plugin? my-plugin # 輸入插件名字
? What is the organization name of your plugin? taosdata # 輸入公司名字
? How would you describe your plugin? my testing plugin # 輸入插件描述
? What type of plugin would you like? datasource # 選擇插件類型 app or panel or datasource
? Do you want a backend part of your plugin? Yes # datasource 類型插件是否包含后端
? Do you want to add Github CI and Release workflows? Yes # 是否添加 github ci 和 release 工作流
? Do you want to add a Github workflow for automatically checking "Grafana API compatibility" on PRs? Yes # 是否添加 pr 工作流
之后 grafna-create-plugin 會(huì)在目錄創(chuàng)建名為 taosdata-myplugin-datasource 的插件工程。進(jìn)到該目錄之后,執(zhí)行 yarn install && yarn build && go mod tidy && mage -v build 即可構(gòu)建插件。啟動(dòng) Grafana,在 datasource 配置頁面就可以搜到該插件,插件的前端代碼在 src 目錄,后端代碼在 pkg 目錄。
后端代碼
后端代碼需要實(shí)現(xiàn) QueryDataHandler 接口和 CheckHealthHandler 接口,分別用于查詢數(shù)據(jù)和 checkHealth。
頁面查詢數(shù)據(jù)時(shí)會(huì)調(diào)用 QueryData 方法。不同的數(shù)據(jù)源,QueryData 方法實(shí)現(xiàn)會(huì)有差異,歸根結(jié)底,該方法的作用是從數(shù)據(jù)源查詢數(shù)據(jù),并將數(shù)據(jù)封裝成 Data Frame。
type QueryDataHandler interface {
QueryData(ctx context.Context, req *QueryDataRequest) (*QueryDataResponse, error)
}
Grafana 會(huì)定時(shí)調(diào)用 CheckHealth 方法,以檢測(cè)插件的健康狀態(tài)。數(shù)據(jù)源配置頁面的 Save & Test 按鈕也會(huì)調(diào)用 CheckHealth 方法來檢測(cè)配置是否正確。
type CheckHealthHandler interface {
CheckHealth(ctx context.Context, req *CheckHealthRequest) (*CheckHealthResult, error)
}
前端代碼
grafana datasource plugin 需要有一定的前端代碼開發(fā)量,包含 datasource、數(shù)據(jù)源配置頁面和數(shù)據(jù)查詢頁面。
datasource-with-backend 插件,datasource 的前端代碼需要實(shí)現(xiàn)(extends)DataSourceWithBackend 接口。由于通過后端接口進(jìn)行數(shù)據(jù)查詢,所以前端頁面代碼比較簡(jiǎn)單。
datasource-backend 插件,datasource 的前端代碼需要實(shí)現(xiàn)(extends)DataSourceApi 接口,并至少實(shí)現(xiàn) query、testDatasource 等方法。其中 query 方法用于數(shù)據(jù)查詢,testDatasource 方法用于測(cè)試數(shù)據(jù)源配置。
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse>
async testDatasource()
數(shù)據(jù)源插件需要配置數(shù)據(jù)源地址、授權(quán)等信息,通常還需要配置數(shù)據(jù)查詢方式,因此需要數(shù)據(jù)源配置頁面和數(shù)據(jù)查詢頁面。
plugin.json 和 module.ts
如果是通過 grafna-create-plugin 創(chuàng)建的插件項(xiàng)目,grafna-create-plugin 會(huì)在 src 目錄下生成 plugin.json 和 module.ts 文件。其中 plugin.json 包含了插件的信息,包括插件類型、插件名字、ID 等。而 module.ts 是 grafana 插件的入口。
Grafana 和 backend plugin 通訊協(xié)議
Grafana 和 backend-plugin 是兩個(gè)進(jìn)程,plugin 是 Grafana 的子進(jìn)程,二者通過 gRpc 通信,plugin 作為 server 端,Grafana 作為 client 端。
其 gRpc 通信協(xié)議可參考 https://github.com/Grafana/Grafana-plugin-sdk-go/blob/master/proto/backend.proto,從協(xié)議上看,backend-plugin 提供了如下 service:
//---------------------------------------------------------
// Resource service enables HTTP-style requests over gRPC.
//---------------------------------------------------------
service Resource {
rpc CallResource(CallResourceRequest) returns (stream CallResourceResponse);
}
//-----------------------------------------------
// Data
//-----------------------------------------------
service Data {
rpc QueryData(QueryDataRequest) returns (QueryDataResponse);
}
//-----------------------------------------------
// Diagnostics
//-----------------------------------------------
service Diagnostics {
rpc CheckHealth(CheckHealthRequest) returns (CheckHealthResponse);
rpc CollectMetrics(CollectMetricsRequest) returns (CollectMetricsResponse);
}
//-----------------------------------------------------------------
// Stream -- EXPERIMENTAL and is subject to change until 8.0
//-----------------------------------------------------------------
service Stream {
// SubscribeStream called when a user tries to subscribe to a plugin/datasource
// managed channel path – thus plugin can check subscribe permissions and communicate
// options with Grafana Core. When the first subscriber joins a channel, RunStream
// will be called.
rpc SubscribeStream(SubscribeStreamRequest) returns (SubscribeStreamResponse);
// RunStream will be initiated by Grafana to consume a stream. RunStream will be
// called once for the first client successfully subscribed to a channel path.
// When Grafana detects that there are no longer any subscribers inside a channel,
// the call will be terminated until next active subscriber appears. Call termination
// can happen with a delay.
rpc RunStream(RunStreamRequest) returns (stream StreamPacket);
// PublishStream called when a user tries to publish to a plugin/datasource
// managed channel path. Here plugin can check publish permissions and
// modify publication data if required.
rpc PublishStream(PublishStreamRequest) returns (PublishStreamResponse);
}
grafana backend-plugin 實(shí)現(xiàn)依賴 github.com/hashicorp/go-plugin 庫,go-plugin 是一個(gè)基于 gRpc 的golang 插件系統(tǒng),由 HashiCorp 開發(fā),并在業(yè)界廣泛使用,grafana backend plugin 就是基于 go-plugin 實(shí)現(xiàn)的。
tdengine-datasource
tdengine-datasource 是 TDengine 的 grafana 數(shù)據(jù)源插件。目前,tdengine-datasource 不完全是一個(gè) backend-plugin,TDinsight 或 dashboard 的數(shù)據(jù)仍然是通過前端頁面直接查詢,只有后端 alerting 的查詢請(qǐng)求會(huì)經(jīng)過后端。

ConfigEditor
在數(shù)據(jù)源配置頁面可以配置數(shù)據(jù)源的信息。添加數(shù)據(jù)源時(shí),需要配置數(shù)據(jù)源的名字(默認(rèn)為 TDengine Datasource)、TDengine 的地址、認(rèn)證信息,通過 Save&test 按鈕,可以校驗(yàn) TDengine 的認(rèn)證信息。

QueryEditor
在查詢配置頁面可以自定義數(shù)據(jù)查詢,包含查詢 sql、時(shí)間戳偏移配置、group by 配置等。

結(jié)語
現(xiàn)在你可以操作體驗(yàn)了,希望本篇文章能帶給你一些幫助。更多示例可參考:
- tdengine-datasource 代碼 https://github.com/taosdata/Grafanaplugin/
- 更多 Grafana-plugin 示例 https://github.com/Grafana/Grafana-plugin-examples
如果在使用 TDengine Grafana 插件的過程中遇到任何問題,或者有新的功能建議,也歡迎在 GitHub(https://github.com/taosdata/grafanaplugin) 上和我們交流?;蛘咛砑?strong>小T vx:tdengine,和 TDengine 的技術(shù)研發(fā)人員進(jìn)行直接溝通。



互聯(lián)網(wǎng).png)



-1.png)




.png)


證.png)


伙伴.png)
伙伴.png)
伙伴.png)



