ds-querier: parse datasourceUid correctly (#95629)

* ds-querier: parse datasourceUid correctly

Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com>

* make test case smaller

---------

Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com>
This commit is contained in:
Adam Simpson
2024-10-31 14:07:26 -04:00
committed by GitHub
co-authored by Sarah Zinger
parent 6f74c19d75
commit 3adc7c8771
2 changed files with 58 additions and 3 deletions
+17 -3
View File
@@ -80,7 +80,7 @@ func (p *queryParser) parseRequest(ctx context.Context, input *query.QueryDataRe
return rsp, MakePublicQueryError(q.RefID, "multiple queries with same refId")
}
ds, err := p.getValidDataSourceRef(ctx, q.Datasource, q.DatasourceID)
ds, err := p.getValidDataSourceRef(ctx, q)
if err != nil {
return rsp, err
}
@@ -194,16 +194,30 @@ func getTimeRangeForQuery(parentTimerange, queryTimerange *data.TimeRange) data.
}
}
func (p *queryParser) getValidDataSourceRef(ctx context.Context, ds *data.DataSourceRef, id int64) (*data.DataSourceRef, error) {
func (p *queryParser) getValidDataSourceRef(ctx context.Context, dataQuery data.DataQuery) (*data.DataSourceRef, error) {
ds := dataQuery.Datasource
id := dataQuery.DatasourceID
if ds == nil {
if id == 0 {
return nil, fmt.Errorf("missing datasource reference or id")
return nil, NewErrorWithRefID(dataQuery.RefID, fmt.Errorf("missing datasource reference or id"))
}
if p.legacy == nil {
return nil, fmt.Errorf("legacy datasource lookup unsupported (id:%d)", id)
}
return p.legacy.GetDataSourceFromDeprecatedFields(ctx, "", id)
}
if ds.UID[0] == '$' {
uid, ok := dataQuery.Get("datasourceUid")
if ok {
return p.legacy.GetDataSourceFromDeprecatedFields(ctx, uid.(string), 0) // uid can be name in this scenario
} else {
return nil, NewErrorWithRefID(dataQuery.RefID, fmt.Errorf("missing datasource reference or id"))
}
}
if ds.Type == "" {
if ds.UID == "" {
return nil, fmt.Errorf("missing name/uid in data source reference")