Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 8829d22

Browse files
authored
Fix adminconsole's Elasticsearch status (#1035)
When backend cluster's SSL setup wasn't printine, we've been showing `Failed` status in the UI, even though everything was working just fine. Fixed by reusing our existing Elasticsearch client, which also cleaned up the code a little. <img width="1660" alt="image" src="https://github.com/user-attachments/assets/a2fb4c25-c6e2-495a-b435-09ecd7cf26a1">
1 parent 458fdde commit 8829d22

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

quesma/health/elastic.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package health
44

55
import (
6+
"context"
67
"encoding/json"
78
"fmt"
89
"io"
@@ -14,26 +15,21 @@ import (
1415
)
1516

1617
type ElasticHealthChecker struct {
17-
cfg *config.QuesmaConfiguration
18-
httpClient *http.Client
18+
cfg *config.QuesmaConfiguration
19+
client *elasticsearch.SimpleClient
1920
}
2021

2122
func NewElasticHealthChecker(cfg *config.QuesmaConfiguration) Checker {
22-
return &ElasticHealthChecker{cfg: cfg, httpClient: &http.Client{}}
23+
return &ElasticHealthChecker{cfg: cfg, client: elasticsearch.NewSimpleClient(&cfg.Elasticsearch)}
2324
}
2425

2526
func (c *ElasticHealthChecker) checkIfElasticsearchDiskIsFull() (isFull bool, reason string) {
26-
const catAllocationPath = "/_cat/allocation?format=json"
27+
const catAllocationPath = "_cat/allocation?format=json"
2728
const maxDiskPercent = 90
2829

29-
req, err := http.NewRequest(http.MethodGet, c.cfg.Elasticsearch.Url.String()+catAllocationPath, nil)
30+
resp, err := c.client.Request(context.Background(), http.MethodGet, catAllocationPath, nil)
3031
if err != nil {
31-
logger.Error().Err(err).Msgf("Can't create '%s' request", catAllocationPath)
32-
}
33-
req = elasticsearch.AddBasicAuthIfNeeded(req, c.cfg.Elasticsearch.User, c.cfg.Elasticsearch.Password)
34-
resp, err := c.httpClient.Do(req)
35-
if err != nil {
36-
return
32+
logger.Error().Err(err).Msgf("Failed calling %s", catAllocationPath)
3733
}
3834
defer resp.Body.Close()
3935
body, err := io.ReadAll(resp.Body)
@@ -67,16 +63,10 @@ func (c *ElasticHealthChecker) checkIfElasticsearchDiskIsFull() (isFull bool, re
6763
}
6864

6965
func (c *ElasticHealthChecker) CheckHealth() Status {
70-
const elasticsearchHealthPath = "/_cluster/health/*"
71-
72-
req, err := http.NewRequest(http.MethodGet, c.cfg.Elasticsearch.Url.String()+elasticsearchHealthPath, nil)
73-
if err != nil {
74-
return NewStatus("red", fmt.Sprintf("Can't create '%s' request", elasticsearchHealthPath), err.Error())
75-
}
76-
req = elasticsearch.AddBasicAuthIfNeeded(req, c.cfg.Elasticsearch.User, c.cfg.Elasticsearch.Password)
77-
resp, err := c.httpClient.Do(req)
66+
const elasticsearchHealthPath = "_cluster/health/*"
67+
resp, err := c.client.Request(context.Background(), http.MethodGet, elasticsearchHealthPath, nil)
7868
if err != nil {
79-
return NewStatus("red", "Ping failed", err.Error())
69+
return NewStatus("red", fmt.Sprintf("Failed calling %s", elasticsearchHealthPath), err.Error())
8070
}
8171
defer resp.Body.Close()
8272
body, err := io.ReadAll(resp.Body)

0 commit comments

Comments
 (0)