@@ -10,6 +10,7 @@ import (
1010 "time"
1111
1212 "github.com/sirupsen/logrus"
13+ "gopkg.in/yaml.v3"
1314
1415 "github.com/netobserv/network-observability-console-plugin/pkg/handler/lokiclientmock"
1516 "github.com/netobserv/network-observability-console-plugin/pkg/httpclient"
@@ -18,6 +19,11 @@ import (
1819 "github.com/netobserv/network-observability-console-plugin/pkg/model"
1920)
2021
22+ type LokiError struct {
23+ DisplayMessage string
24+ Message string
25+ }
26+
2127var hlog = logrus .WithField ("module" , "handler" )
2228
2329const (
@@ -52,15 +58,18 @@ func EncodeQuery(url string) string {
5258
5359func getLokiError (resp []byte , code int ) string {
5460 var f map [string ]string
61+ if code == http .StatusBadRequest {
62+ return fmt .Sprintf ("Loki message: %s" , resp )
63+ }
5564 err := json .Unmarshal (resp , & f )
5665 if err != nil {
57- return fmt .Sprintf ("Unknown error from Loki - cannot unmarshal (code: %d resp: %s)" , code , resp )
66+ return fmt .Sprintf ("Unknown error from Loki\n cannot unmarshal\n %s" , resp )
5867 }
5968 message , ok := f ["message" ]
6069 if ! ok {
61- return fmt . Sprintf ( "Unknown error from Loki - no message found (code: %d)" , code )
70+ return "Unknown error from Loki\n no message found"
6271 }
63- return fmt .Sprintf ("Error from Loki (code : %d): %s" , code , message )
72+ return fmt .Sprintf ("Loki message : %s" , message )
6473}
6574
6675func executeLokiQuery (flowsURL string , lokiClient httpclient.Caller ) ([]byte , int , error ) {
@@ -77,7 +86,7 @@ func executeLokiQuery(flowsURL string, lokiClient httpclient.Caller) ([]byte, in
7786 }
7887 if code != http .StatusOK {
7988 msg := getLokiError (resp , code )
80- return nil , http .StatusBadRequest , errors .New ("Loki backend responded: " + msg )
89+ return nil , http .StatusBadRequest , errors .New (msg )
8190 }
8291 code = http .StatusOK
8392 return resp , code , nil
@@ -154,3 +163,80 @@ func fetchParallel(lokiClient httpclient.Caller, queries []string, merger loki.M
154163 codeOut = http .StatusOK
155164 return codeOut , nil
156165}
166+
167+ func LokiReady (cfg loki.Config ) func (w http.ResponseWriter , r * http.Request ) {
168+ lokiClient := newLokiClient (& cfg )
169+
170+ return func (w http.ResponseWriter , r * http.Request ) {
171+ baseURL := strings .TrimRight (cfg .URL .String (), "/" )
172+
173+ resp , code , err := executeLokiQuery (fmt .Sprintf ("%s/%s" , baseURL , "ready" ), lokiClient )
174+ if err != nil {
175+ writeError (w , code , err .Error ())
176+ return
177+ }
178+
179+ status := string (resp )
180+ if strings .Contains (status , "ready" ) {
181+ code = http .StatusOK
182+ writeText (w , code , resp )
183+ return
184+ }
185+
186+ writeError (w , code , fmt .Sprintf ("Loki returned a non ready status: %s" , status ))
187+ }
188+ }
189+
190+ func LokiMetrics (cfg loki.Config ) func (w http.ResponseWriter , r * http.Request ) {
191+ lokiClient := newLokiClient (& cfg )
192+
193+ return func (w http.ResponseWriter , r * http.Request ) {
194+ baseURL := strings .TrimRight (cfg .URL .String (), "/" )
195+
196+ resp , code , err := executeLokiQuery (fmt .Sprintf ("%s/%s" , baseURL , "metrics" ), lokiClient )
197+ if err != nil {
198+ writeError (w , code , err .Error ())
199+ return
200+ }
201+
202+ writeText (w , code , resp )
203+ }
204+ }
205+
206+ func LokiBuildInfos (cfg loki.Config ) func (w http.ResponseWriter , r * http.Request ) {
207+ lokiClient := newLokiClient (& cfg )
208+
209+ return func (w http.ResponseWriter , r * http.Request ) {
210+ baseURL := strings .TrimRight (cfg .URL .String (), "/" )
211+
212+ resp , code , err := executeLokiQuery (fmt .Sprintf ("%s/%s" , baseURL , "loki/api/v1/status/buildinfo" ), lokiClient )
213+ if err != nil {
214+ writeError (w , code , err .Error ())
215+ return
216+ }
217+
218+ writeText (w , code , resp )
219+ }
220+ }
221+
222+ func LokiConfig (cfg loki.Config , param string ) func (w http.ResponseWriter , r * http.Request ) {
223+ lokiClient := newLokiClient (& cfg )
224+
225+ return func (w http.ResponseWriter , r * http.Request ) {
226+ baseURL := strings .TrimRight (cfg .URL .String (), "/" )
227+
228+ resp , code , err := executeLokiQuery (fmt .Sprintf ("%s/%s" , baseURL , "config" ), lokiClient )
229+ if err != nil {
230+ writeError (w , code , err .Error ())
231+ return
232+ }
233+
234+ cfg := make (map [string ]interface {})
235+ err = yaml .Unmarshal (resp , & cfg )
236+ if err != nil {
237+ writeError (w , code , err .Error ())
238+ return
239+ }
240+ writeJSON (w , code , cfg [param ])
241+ }
242+ }
0 commit comments