diff --git a/cspell.config.json b/cspell.config.json index 13449dc6..90f6f3f9 100644 --- a/cspell.config.json +++ b/cspell.config.json @@ -114,5 +114,6 @@ "Throughputs", "mainstat", "secondarystat", + "x-ndjson" ] } diff --git a/pkg/opensearch/client/client.go b/pkg/opensearch/client/client.go index 16788a16..794bfe35 100644 --- a/pkg/opensearch/client/client.go +++ b/pkg/opensearch/client/client.go @@ -299,7 +299,7 @@ func (c *baseClientImpl) executeRequest(ctx context.Context, method, uriPath, ur } req.Header.Set("User-Agent", "Grafana") - req.Header.Set("Content-Type", "application/json") + req.Header.Set("Content-Type", "application/x-ndjson") dsHttpOpts, err := c.ds.HTTPClientOptions(ctx) if err != nil { diff --git a/pkg/opensearch/client/client_test.go b/pkg/opensearch/client/client_test.go index b67f1d3f..76da6da3 100644 --- a/pkg/opensearch/client/client_test.go +++ b/pkg/opensearch/client/client_test.go @@ -91,6 +91,7 @@ func TestClient(t *testing.T) { assert.NotNil(t, sc.request) assert.Equal(t, http.MethodPost, sc.request.Method) assert.Equal(t, "/_msearch", sc.request.URL.Path) + assert.Equal(t, "application/x-ndjson", sc.request.Header.Get("Content-Type")) assert.Equal(t, "max_concurrent_shard_requests=6", sc.request.URL.RawQuery) assert.NotNil(t, sc.requestBody) @@ -178,6 +179,7 @@ func TestClient(t *testing.T) { assert.NotNil(t, sc.request) assert.Equal(t, http.MethodPost, sc.request.Method) assert.Equal(t, "/_opendistro/_ppl", sc.request.URL.Path) + assert.Equal(t, "application/json", sc.request.Header.Get("Content-Type")) assert.NotNil(t, sc.requestBody) diff --git a/src/datasource.ts b/src/datasource.ts index 81ad205f..e40727a9 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -21,7 +21,14 @@ import { OpenSearchResponse } from './OpenSearchResponse'; import { IndexPattern } from './index_pattern'; import { QueryBuilder } from './QueryBuilder'; import { defaultBucketAgg, hasMetricOfType } from './query_def'; -import { DataSourceWithBackend, FetchError, getBackendSrv, getDataSourceSrv, getTemplateSrv } from '@grafana/runtime'; +import { + BackendSrvRequest, + DataSourceWithBackend, + FetchError, + getBackendSrv, + getDataSourceSrv, + getTemplateSrv, +} from '@grafana/runtime'; import { DataLinkConfig, Flavor, LuceneQueryType, OpenSearchOptions, OpenSearchQuery, QueryType } from './types'; import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils'; import { @@ -103,27 +110,26 @@ export class OpenSearchDatasource extends DataSourceWithBackend { @@ -175,8 +181,12 @@ export class OpenSearchDatasource extends DataSourceWithBackend { + private postMultiSearch(url: string, data: any) { + return this.post(url, data, { 'Content-Type': 'application/x-ndjson' }); + } + + private post(url: string, data: any, headers?: BackendSrvRequest['headers']) { + return this.request('POST', url, data, headers).then((results: any) => { results.data.$$config = results.config; return results.data; }); @@ -252,7 +262,7 @@ export class OpenSearchDatasource extends DataSourceWithBackend { + return this.postMultiSearch('_msearch', payload).then((res: any) => { const list = []; const hits = res.responses[0].hits.hits; @@ -651,7 +661,7 @@ export class OpenSearchDatasource extends DataSourceWithBackend 0 - ? from(this.post(this.getMultiSearchUrl(), createQuery(traceListTargets))).pipe( + ? from(this.postMultiSearch(this.getMultiSearchUrl(), createQuery(traceListTargets))).pipe( map((res: any) => { return createListTracesDataFrame(traceListTargets, res, this.uid, this.name, this.type); }) @@ -660,7 +670,7 @@ export class OpenSearchDatasource extends DataSourceWithBackend 0 - ? from(this.post(this.getMultiSearchUrl(), createQuery(traceTargets))).pipe( + ? from(this.postMultiSearch(this.getMultiSearchUrl(), createQuery(traceTargets))).pipe( map((res: any) => { return createTraceDataFrame(traceTargets, res); }) @@ -668,7 +678,7 @@ export class OpenSearchDatasource extends DataSourceWithBackend 0 - ? from(this.post(this.getMultiSearchUrl(), createQuery(otherTargets))).pipe( + ? from(this.postMultiSearch(this.getMultiSearchUrl(), createQuery(otherTargets))).pipe( map((res: any) => { const er = new OpenSearchResponse(otherTargets, res); // this condition that checks if some targets are logs, and if some are, enhance ALL data frames, even the ones that aren't @@ -907,7 +917,7 @@ export class OpenSearchDatasource extends DataSourceWithBackend { + return this.postMultiSearch(url, esQuery).then((res: any) => { if (!res.responses[0].aggregations) { return []; }