Elasticsearch: Use _field_caps instead of _mapping to get fields (#97607)

This commit is contained in:
Isabella Siu
2024-12-12 17:20:04 -05:00
committed by GitHub
parent a362769491
commit b3a12f486e
10 changed files with 296 additions and 5 deletions
+6
View File
@@ -1676,6 +1676,12 @@ var (
Owner: grafanaOperatorExperienceSquad,
HideFromDocs: true,
},
{
Name: "elasticsearchCrossClusterSearch",
Description: "Enables cross cluster search in the Elasticsearch datasource",
Stage: FeatureStagePublicPreview,
Owner: awsDatasourcesSquad,
},
}
)
+1
View File
@@ -223,3 +223,4 @@ alertingUIOptimizeReducer,GA,@grafana/alerting-squad,false,false,true
azureMonitorEnableUserAuth,GA,@grafana/partner-datasources,false,false,false
alertingNotificationsStepMode,experimental,@grafana/alerting-squad,false,false,true
feedbackButton,experimental,@grafana/grafana-operator-experience-squad,false,false,false
elasticsearchCrossClusterSearch,preview,@grafana/aws-datasources,false,false,false
1 Name Stage Owner requiresDevMode RequiresRestart FrontendOnly
223 azureMonitorEnableUserAuth GA @grafana/partner-datasources false false false
224 alertingNotificationsStepMode experimental @grafana/alerting-squad false false true
225 feedbackButton experimental @grafana/grafana-operator-experience-squad false false false
226 elasticsearchCrossClusterSearch preview @grafana/aws-datasources false false false
+4
View File
@@ -902,4 +902,8 @@ const (
// FlagFeedbackButton
// Enables a button to send feedback from the Grafana UI
FlagFeedbackButton = "feedbackButton"
// FlagElasticsearchCrossClusterSearch
// Enables cross cluster search in the Elasticsearch datasource
FlagElasticsearchCrossClusterSearch = "elasticsearchCrossClusterSearch"
)
+15
View File
@@ -1269,6 +1269,21 @@
"frontend": true
}
},
{
"metadata": {
"name": "elasticsearchCrossClusterSearch",
"resourceVersion": "1733848475752",
"creationTimestamp": "2024-12-09T13:53:38Z",
"annotations": {
"grafana.app/updatedTimestamp": "2024-12-10 16:34:35.752111 +0000 UTC"
}
},
"spec": {
"description": "Enables cross cluster search in the Elasticsearch datasource",
"stage": "preview",
"codeowner": "@grafana/aws-datasources"
}
},
{
"metadata": {
"name": "enableDatagridEditing",
+11 -1
View File
@@ -182,14 +182,21 @@ func (s *Service) getDSInfo(ctx context.Context, pluginCtx backend.PluginContext
return &instance, nil
}
func isFieldCaps(url string) bool {
return strings.HasSuffix(url, "/_field_caps") || url == "_field_caps"
}
func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
logger := s.logger.FromContext(ctx)
// allowed paths for resource calls:
// - empty string for fetching db version
// - /_mapping for fetching index mapping, e.g. requests going to `index/_mapping`
// - /_field_caps for fetching field capabilities, e.g. requests going to `index/_field_caps`
// - _msearch for executing getTerms queries
// - _mapping for fetching "root" index mappings
if req.Path != "" && !strings.HasSuffix(req.Path, "/_mapping") && req.Path != "_msearch" && req.Path != "_mapping" {
// - _field_caps for fetching "root" field capabilities
if req.Path != "" && !isFieldCaps(req.Path) && req.Path != "_msearch" &&
!strings.HasSuffix(req.Path, "/_mapping") && req.Path != "_mapping" {
logger.Error("Invalid resource path", "path", req.Path)
return fmt.Errorf("invalid resource URL: %s", req.Path)
}
@@ -266,6 +273,9 @@ func createElasticsearchURL(req *backend.CallResourceRequest, ds *es.DatasourceI
}
esUrl.Path = path.Join(esUrl.Path, req.Path)
if isFieldCaps(req.Path) {
esUrl.RawQuery = "fields=*"
}
esUrlString := esUrl.String()
// If the request path is empty and the URL does not end with a slash, add a slash to the URL.
// This ensures that for version checks executed to the root URL, the URL ends with a slash.