How to query OpenSearch through an OpenSearchDashboards plugin?

Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):

OpenSearch: v2.9.0
Dashboard: v2.9.0

Describe the issue:

Hi,
I created a dashboard plugin and want to get cluster information from OpenSearch. However, I don’t know how to query OpenSearch from within the plugin. I wrote the following code, but it will break if the OpenSearch host changes:

const baseUrl = 'http://localhost:9200';

export const getClusterStats = async () => {
    const response = await fetch(baseUrl + '/_cluster/stats')
    return await response.json()

This code is hard-coded to use the OpenSearch host localhost:9200. If the OpenSearch host changes, the code will need to be updated manually.

Is there a way to change the code so that it is more portable and will not break if the OpenSearch host changes?

@saji OpenSearch Dashboards is using either environmental variable OPENSEARCH_HOSTS or opensearch.hosts defined in opensearch_dashboards.yml to communicate with OpenSearch. This value must be set before starting OpenSearch Dashboards.
Maybe this is an option for you to source a valid OpenSearch hostname.

1 Like