需求:grafana中的dashboard的metric需要根据不同的参数进行逻辑处理后获取可选项
如:不同的用户访问该dashboard,某指标需要根据该用户信息,返回不同的可选项。
这里,我们就需要simpod-json-datasource这一插件来帮助实现。
1. 安装simpod-json-datasource
1.1 本地安装
本地安装十分简单,只需要使用grafana-cli
工具,运行
grafana-cli plugins install simpod-json-datasource
然后,需要重新启动grafana。
mac安装直接执行上面的命令后,重启即可。
centos、redhat安装后,执行上面的插件安装命令重启后,看不到新的数据源。因为这个插件安装后的默认位置是 /var/lib/grafana/plugins,需要手动将这个插件复制到./grafana-7.2.0/data/plugins目录下,因为defaults.ini配置文件中指定了插件读取的位置。或将配置文件中的位置修改为/var/lib/grafana/plugins。
1.2 使用Dockerfile安装
如果是二次开发grafana,将插件安装到镜像中:
cd 到 /packaging/docker/custom
路径下,使用该路径下的Dockerfile构建镜像。
docker build \
--build-arg "GRAFANA_VERSION=latest" \
--build-arg "GF_INSTALL_PLUGINS=simpod-json-datasource" \
-t grafana:grafana -f Dockerfile .
如果需要在二次开发的代码基础上安装插件,可以将使用/Dockerfile
构建的镜像tag成grafana/grafana:latest
,或者将 /packaging/docker/custom
下的dockerfile的基础镜像改成使用/Dockerfile
构建好的镜像,在此基础上执行上述docker build命令,在镜像中安装插件。
1.3 签名
安装成功后,你可以在plugins列表中看到json插件
由于该插件未签名,需要在配置文件中添加
[plugins]
allow_loading_unsigned_plugins = simpod-json-datasource
这样,虽然没有签名也可以允许使用了。
2. 获取指标
2.1 接口开发
simpod-json-datasource插件需要后端实现4个接口,以实现不同的功能。
这里实现/
和/search
接口,/
接口返回{"success":true}
即可,用于test接口是否可以使用;/search
接口返回list即可,用于dashboard中metric的可选项。
grafana会将variable中的query参数封装为"target":"$query"
作为body传给/search
接口(后文也会解释),所以这个接口应该首先解析出target后面的内容作为参数。
示例代码如下:
@RequestMapping(value = "/**", method = RequestMethod.GET) public Object checkDataSource() throws Exception {
Map<String, Object> body = new HashMap<>();
body.put("success", true);
return successReturn(body);
}
@RequestMapping(value = "/search", method = RequestMethod.POST) public List<String> searchProjectIds(@RequestBody JSONObject request)
throws IOException {
String url = SafeFastJsonUtil.getString(request, "target");
String method = url.substring(0, url.indexOf("?"));
if ("getProjectId".equals(method)) {
String accountId = url.substring(url.indexOf("accountId=") + "accountId=".length(), url.indexOf("&"));
String tenantName = url.substring(url.indexOf("tenantId=") + "tenantId=".length());
String tenantId = tenantName.substring(tenantName.lastIndexOf("-") + 1);
if (accountId.isEmpty() || tenantId.isEmpty()) {
return new ArrayList<>();
}
return extendGrafanaDataSourceService.getProjectIds(accountId, tenantId);
} else if ("getTenantId".equals(method)) {
String accountId = url.substring(url.indexOf("accountId=") + "accountId=".length());
if (accountId.isEmpty()) {
return new ArrayList<>();
}
return extendGrafanaDataSourceService.getTenantIds(accountId);
}
return null;
}
/search
接口中,我将body中的参数解析出来,并返回一个list列表。
2.2 配置数据源
将接口部署好后,新建一个JSON类型的datasource,url即为部署好的接口("/"接口即可),save&test。
2.3 配置Variables
在dashboard的设置中,新建Variables,选择刚刚配置好的Data source。
这里grafana会先去get这个Data source,获取后端接口,然后将query里的值作为body发起post请求,获取结果。上图中就是将getTenantId?accountId=$account_id
包装成"target":"getTenantId?accountId=$account_id"
发给后端。
然后,update一下就可以看到接口的返回值。
保存一下dashboard,就完成这个需求啦。
3. 避坑必看
在安装json插件的过程中,我本来是基于grafana-v7.1.0版本进行开发,按上述流程制作镜像。结果页面前端一直报错"Datasource named JSO8N was not found"
。调试很久找不到问题出在哪。于是将代码版本升到v7.3.5,问题解决。(真是太坑了。)