Grafana 的插件开发_grafana 插件开发-程序员宅基地

技术标签: 前端环境  开发grafana的panel插件  

<1>推荐一个网址:http://blog.leanote.com/post/nixon/%E6%8F%92%E4%BB%B6%E5%BC%80%E5%8F%91

Grafana Panel Plugin 开发

Grafana 插件开发

上一次分享提到过Grafana的插件氛围三种类型(Panel, Datasource, App),这一次主要记录Panel类型的插件开发.

0. 起手式

根据官网的描述, 插件包一般放在/var/lib/grafana/plugins或者data/plugins(相对Grafana源码目录). 前者是生产环境, 后者是开发环境. 所以我们这一次选择后者, 毕竟一会儿要从源码修改Grafana的前端代码, 然后启动go语言编写的后端.

基础知识准备: 
angularjs, systemjs, typescript, jquery, echarts, grunt, nodejs and golang.

1. 目录结构

先把官网上说明需要的文件目录加上.

  1. johnnyb-awesome-datasource
  2. |-- dist # 存放编译打包后的插件代码
  3. |-- spec # 测试代码
  4. | |-- datasource_spec.js
  5. | |-- query_ctrl_spec.js
  6. | |-- test-main.js
  7. |-- src # 插件源码
  8. | |-- img # 需要的图片, 比如LOGO, 可选
  9. | | |-- logo.svg
  10. | |-- partials # 界面文件, 可选
  11. | | |-- annotations.editor.html
  12. | | |-- config.html
  13. | | |-- query.editor.html
  14. | |-- datasource.js
  15. | |-- module.js # 唯一入口文件
  16. | |-- plugin.json # 插件描述文件
  17. | |-- query_ctrl.js
  18. |-- Gruntfile.js # Grunt任务
  19. |-- LICENSE
  20. |-- package.json
  21. |-- README.md # 这也会显示在插件说明中

README.md: The only difference from how GitHub renders markdown is that html is not allowed.

实际上并不需要严格按照上述目录存放文件, 关键点在于: 插件目录下需要有src/dist/src/中需要有module.jsplugin.json. 其他开心就好.

所以实际上, 我的目录结构是这样:

  1. practice-panel
  2. |-- dist # 存放编译打包后的插件代码
  3. |-- src # 插件源码
  4. | |-- partials # 界面文件, 可选
  5. | | |-- module.html
  6. | | |-- module.editor.html
  7. | |-- module.js # 唯一入口文件
  8. | |-- plugin.json # 插件描述文件
  9. | |-- controller.js # 分离出来的Controller, 也可全部放在module.js里面
  10. |-- Gruntfile.js # Grunt任务描述
  11. |-- package.json
  12. |-- README.md

2. 插件描述文件(.json)

然后来认识一个文件, 这个文件是用来描述插件的. 包括插件的唯一标识, 名字, 作者, 版本等等. 全文是这样的:

  1. {
  2. "id": "",
  3. "type": "",
  4. "name": "",
  5. "info": {
  6. "description": "",
  7. "author": {
  8. "name": "",
  9. "url": ""
  10. },
  11. "keywords": [],
  12. "logos": [
  13. "small": "",
  14. "large": ""
  15. ],
  16. "version": ""
  17. },
  18. "dependencies": {
  19. "grafanaVersion": "",
  20. "plugins": []
  21. }
  22. }

具体的含义, 参考附录中的官方说明链接. 本次的内容参考源码文件.

3. 入口文件

这基本上就是约定, Grafana会从插件包的dist/第一个读取的文件就是module.js文件. 不管用什么语言编写, 最终都要保证dist/module.js可读.

4. 可用依赖资源

Grafana的前端模块化方案采用的Systemjs. 如果熟悉任何一种模块化方案都行, 这里的影响并不是很大, 除了需要添加依赖资源. 在插件开发过程中, 开发者是无法添加任何依赖资源的. 因为插件是作为Grafana前端整体的一部分在运行. 所有的资源配置都写在Grafana源码目录的public/app/system.conf.js文件中, 资源都放在public/vendor/目录中.

默认可用资源如下:

  1. virtual-scroll
  2. mousetrap
  3. remarkable
  4. tether
  5. eventemitter3
  6. tether-drop
  7. moment
  8. jquery
  9. lodash-src
  10. lodash
  11. angular
  12. bootstrap
  13. angular-route
  14. angular-sanitize
  15. angular-ui
  16. angular-strap
  17. angular-dragdrop
  18. angular-bindonce
  19. spectrum
  20. bootstrap-tagsinput
  21. jquery.flot
  22. jquery.flot.pie
  23. jquery.flot.selection
  24. jquery.flot.stack
  25. jquery.flot.stackpercent
  26. jquery.flot.time
  27. jquery.flot.crosshair
  28. jquery.flot.fillbelow
  29. jquery.flot.gauge
  30. d3
  31. jquery.flot.dashes

是的, 像D3, jquery, moment, lodash这些非常有用的图表基础库都有了, 甚至很贴心的连AngularJS全家桶都有. 美中不足的是AngularJS版本偏低: 1.6.1.

但这一次练习, 我们要用echarts来构建图表. 很遗憾, Grafana没有这项资源. 所以总共需要修改二个地方.

  • echarts.min.js放入public/vendor/目录
  • 修改public/app/system.conf.js文件, 在paths节点中添加代码: 'echarts': 'vendor/echarts.min.js', 上下文看起来像这样:

    1. 'tether-drop': 'vendor/npm/tether-drop/dist/js/drop.js',
    2. 'moment': 'vendor/moment.js',
    3. 'echarts': 'vendor/echarts.min.js',
    4. 'jquery': 'vendor/jquery/dist/jquery.js',

5. 开始编码

MetricsPanelCtrl类

准备工作基本完成了, 但是编码之前, 需要认识一个类: MetricsPanelCtrl. 这个类的源码文件位于Grafana源码目录下public/app/features/panel/metrics_panel_ctrl.tsMetricsPanelCtrl类继承自PanelCtrl类, 是我们完成本次Panel类型插件开发必须要用到的. 它主要是解决了以下三个问题:

  1. 在进入编辑状态后, 使metrics Tab成为默认面板.

    1. constructor($scope, $injector) {
    2. super($scope, $injector);
    3. // make metrics tab the default
    4. this.editorTabIndex = 1;
    5. // ... other code
    6. }
  2. 订阅事件:

    1. this.events.on('refresh', this.onMetricsPanelRefresh.bind(this));
    2. this.events.on('init-edit-mode', this.onInitMetricsPanelEditMode.bind(this));
    3. this.events.on('panel-teardown', this.onPanelTearDown.bind(this));
  3. 提供设置数据源后的响应:

    1. setDatasource(datasource) {
    2. // ... other code
    3.  
    4. this.panel.datasource = datasource.value;
    5. this.datasourceName = datasource.name;
    6. this.datasource = null;
    7. this.refresh();
    8. }

插件事件

因为文档中并无特别描述, 所以以下是已知的插件支持的事件:

  • refresh can be used to response when refresh button was clicked.
  • init-edit-mode can be used to add tabs when editing a panel
  • panel-teardown can be used for clean up
  • data-received is an event in that is triggered on data refresh and can be hooked into
  • data-snapshot-load is an event triggered to load data when in snapshot mode.
  • data-error is used to handle errors on dashboard refresh.

编写一个controller.js

Controller类必须实现link方法或者init方法. 这是Grafana的插件机制决定的. 与此同时, Controller的实例对象也将作为AngularJS组件的上下文对象存在.

  1. // app/core/directives/plugin_component.ts
  2. function getPluginComponentDirective(options) {
  3. // other code
  4. return function() {
  5. return {
  6. // other code
  7. link: (scope, elem, attrs, ctrl) => {
  8. if (ctrl.link) {
  9. ctrl.link(scope, elem, attrs, ctrl);
  10. }
  11. if (ctrl.init) {
  12. ctrl.init();
  13. }
  14. }
  15. }
  16. }
  17. }

而这个函数是作为AngularJS的指令处理函数存在的. 所以我们的controller.js看起来至少应该是这样的:

  1. import { MetricsPanelCtrl } from 'app/plugins/sdk';
  2.  
  3. export class Controller extends MetricsPanelCtrl {
  4. constructor() {
  5. // initialize
  6. // subscribe events
  7. }
  8.  
  9. link(scope, elem, attrs, ctrl) {
  10. // impelement linking
  11. }
  12. }

完整实现细节请参考源码文件.

是的, 此时还没有界面. 在编写界面之前, 我们先了解一下插件的二种状态.

6. 插件状态

插件有二种状态, 一种是只读状态, 一种是编辑状态. 只读状态就是打开一个dashboard时各个Panel表现的状态. 编辑状态是点击”Edit”之后, 进入的可以影响数据源的状态. 我们需要分别为这二种状态编写页面.

完整实现细节参考源码文件.

7. HTML + CSS

Grafana提供一些布局样式和外观样式. 非常不幸, 我没有在官方文档上找到对应的样式说明. 不过幸运的是, 插件目前只有三种, 而且三种都提供了Example代码. 分别是:

https://github.com/grafana/piechart-panel

https://github.com/grafana/simple-json-datasource

https://github.com/grafana/kubernetes-app

这一次练习则是copy的piechart-panel的编辑页面. 用起来有点类似bootstrap的感觉.

好了, 重点来了: 在编写完成界面后, 需要将界面和controller关联在一起.

只读状态的界面关联方法是在controller.js中声明完Controller类后, 添加静态属性字段:

  1. Controller.templateUrl = './partials/module.html';

编辑状态的界面则需要在init-edit-mode事件处理函数中注册:

  1. onInitEditMode() {
  2. this.addEditorTab('Options', 'public/plugins/practice-panel/partials/module.editor.html', 2);
  3. }

完整实现细节参考源码文件.

8. 编译打包

编译Grafana官方使用的是Grunt, 实际上只要按照目录结构来, 用什么打包工具并不重要.

grunt

一个练习插件就这么完成了.

9. 总结

success.png
一种数据的四种表现形式. 其中右下角红色的柱状图即本次练习插件.

图表数据Grafana只支持二种, 时序图和非时序图. 非时序图仅返回一个普通的数据结构:

type_choice.png
dev.png

附录

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41646716/article/details/82733410

智能推荐

Sublime text 3搭建Python开发环境及常用插件安装_sublime python 环境搭建-程序员宅基地

文章浏览阅读4.9k次。Sublime text 3搭建Python开发环境及常用插件安装_sublime python 环境搭建

在CentOS 7上安装MySQL 8.0_centos7安装mysql8.0gpg密钥-程序员宅基地

文章浏览阅读643次。MySQL在首次安装后会执行一个安全脚本,用于设置root用户的密码以及其他安全选项。_centos7安装mysql8.0gpg密钥

echarts绘制圆角方形进度图_echarts symbolboundingdata-程序员宅基地

文章浏览阅读864次。这种场景下,可以使用两个系列,一个系列是完整的图形,当做『背景』来表达总数值,另一个系列是使用 `symbolClip` 进行剪裁过的图形,表达当前数值。_echarts symbolboundingdata

学python需要什么样的电脑,python需要什么样的电脑_python机器学习需要怎样配置的电脑-程序员宅基地

文章浏览阅读1k次,点赞18次,收藏16次。这篇文章主要介绍了学python对电脑配置要求高吗,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。_python机器学习需要怎样配置的电脑

最新OCR开源神器来了!-程序员宅基地

文章浏览阅读3.9k次。Datawhale开源开源方向:OCR开源项目01导读OCR方向的工程师,之前一定听说过PaddleOCR这个项目,其主要推荐的PP-OCR算法更是被国内外企业开发者广泛应用,短短半年..._github 2023年最新表格ocr

python 建筑建模_设计课开题 | Parameterized Complexities参数化建筑设计-程序员宅基地

文章浏览阅读317次。【竞赛+作品集,点燃你的设计理想】设计课开题啦!百川柯纳陆续推出以国际设计竞赛项目为参考的设计题目让大家参与,借此丰富履历,充实作品集。本期的设计题目为:Parameterized Complexities参数化建筑设计。喜欢参数化的小伙伴,你们兴奋吗?Parameterized Complexities 选题背景 近期不断有小伙伴在后台给我们留言,或者咨询百川柯纳顾问老师表达希望能够参加以“参数..._python 建筑平面图

随便推点

[ATF]-TEE/REE系统切换时ATF的寄存器的保存和恢复_atf-tee-程序员宅基地

文章浏览阅读1k次。ATF点滴1、设置运行时栈SP2、寄存器的保存和恢复的实现3、寄存器的保存和恢复的使用场景1、设置运行时栈SPbl31_entrypoint—>el3_entrypoint_common---->plat_set_my_stack—>platform_set_stack—>platform_get_stack动态找到该cpufunc platform_set_stackmov x9, x30 // lrbl platform_get_stackmov sp, x0r_atf-tee

PPT模板下载-程序员宅基地

文章浏览阅读134次。300多个各种类型的PPT模板下载,为您提供各种类型PPT模板、PPT图片、PPT素材、海报模板、新媒体配图等内容下载。

基于JAVA的智能小区物业管理系统【数据库设计、源码、开题报告】_智能化哪些系统需要数据库-程序员宅基地

文章浏览阅读546次。主要功能有:保安保洁管理、报修管理、房产信息管理、公告管理、管理员信息管理、业主信息管理、登录管理。_智能化哪些系统需要数据库

年度书单盘点 | 实用到爆炸,这份高性价比套系书单,越读越上头!-程序员宅基地

文章浏览阅读69次。本期年度书单,带大家盘点一下本年度图灵最受欢迎的套装图书,以前买套装书是为了凑单,如今套装书买回去不仅有一次性就能读完的酣畅感还极具收藏价值。一本好书往往要经过时间的验证,而阅读又是一种隐私,每个人的喜好大有不同,但能够集齐每个人的喜爱,这往往就是经典的诞生。今天这份书单里,有自成体系的套系书,还有一些因读者需求而产生的组成套系书。但不管哪种形式,它们都解决了读者在学习某些方面遇到的问题,也给大家...

thch30 steps/make_mfcc.sh详解-程序员宅基地

文章浏览阅读809次。这个脚本的输入参数有三个:1.data/mfcc/train 2.exp/make_mfcc/train 3.mfcc/train1.data/mfcc/train中有数据预处理后的一些文件:phone.txt spk2utt text utt2spk wav.scp word.txt2.exp/make_mfcc/train中应该是要保存程序运行的日志文件的3.mfcc/train中是提取出的特征文件1是输入目录,2,3是输出目录#!/bin/bash# Copyright 2012-2_thch30

smartclient listgrid style (加竖线、横线、背景色)_listgrid添加样式-程序员宅基地

文章浏览阅读2.5k次。如图所示:在jsp中引入: Style.css 代码:.myOtherGridCell { font-family:Verdana,Bitstream Vera Sans,sans-serif; font-size:11px; color:black; border-bottom:1px solid #a0a0a0;border-right:1px solid_listgrid添加样式