我想创建一个使用MSSQL数据库的Grafana插件。我知道有一个内置的MSSQL数据源插件,但这需要用户键入整个SQL查询。我想创建一个自定义数据源,该数据源允许用户从列表中选择一个名称,插件将为此创建相应的查询。因此,我想构建一个完整的自定义查询生成器(它是数据源插件的一部分)。
我从简单的数据源插件[https://github.com/grafana/simple-datasource/tree/master][1]开始,我能够对其进行编译,并且可以在Grafana中看到它。
我认为如果我的数据源扩展现有的MSSQL插件会很容易。
我的testDatasource看起来像这样:
async testDatasource() {
return getBackendSrv()
.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
from: '5m',
to: 'now',
queries: [
{
refId: 'A',
intervalMs: 1,
maxDataPoints: 1,
datasourceId: this.id,
rawSql: 'SELECT 1',
format: 'table',
},
],
},
})
.then((res: any) => {
return { status: 'success', message: 'Database Connection OK' };
})
.catch((err: any) => {
if (err.data && err.data.message) {
return { status: 'error', message: err.data.message };
} else {
return { status: 'error', message: err.status };
}
});
每次测试时,日志中都会出现错误
error="Could not find executor for data source type: my-mssql-datasource" remote_addr=[::1]
我已经在github上检查了此错误的源,并且我发现grafana / pkg / tsdb / query_endpoint.go中的Grafana代码在其已注册插件中找不到my-mssql-datasource
名称。
有没有一种方法可以使用Grafana中已经存在的MSSQL后端处理?如果是这样,是否有任何指南或提示显示如何执行此操作?我已尝试遵循Grafana文档,但这并没有帮助我。
例如:我可以在datasourceRequest
中添加一些东西来使Grafana对MSSQL执行查询吗?或者我可以注册我的数据源以便它使用MSSQL吗?
编辑 正如@Jan Garaj在评论中提到的那样,可以使用仪表板变量来完成。但是,仪表板变量用于整个仪表板。如果我想让2个面板具有不同的信号(可由用户选择),则不能使用仪表板变量。然后,我需要类似面板变量的东西。因为它是按面板显示的,所以我想使用查询构建器来完成此操作。 [1]:https://github.com/grafana/simple-datasource/tree/master