From 0bee0a6d90cd04a27eeb4db4f62923acc1b8de91 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 24 Aug 2023 12:14:16 +0530 Subject: [PATCH 1/2] fix: update dashboards to use placeholder params (#3408) --- pkg/query-service/app/dashboards/model.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/query-service/app/dashboards/model.go b/pkg/query-service/app/dashboards/model.go index 398b151399..2403775cb8 100644 --- a/pkg/query-service/app/dashboards/model.go +++ b/pkg/query-service/app/dashboards/model.go @@ -49,7 +49,7 @@ func InitDB(dataSourceName string) (*sqlx.DB, error) { _, err = db.Exec(table_schema) if err != nil { - return nil, fmt.Errorf("Error in creating dashboard table: %s", err.Error()) + return nil, fmt.Errorf("error in creating dashboard table: %s", err.Error()) } table_schema = `CREATE TABLE IF NOT EXISTS rules ( @@ -61,7 +61,7 @@ func InitDB(dataSourceName string) (*sqlx.DB, error) { _, err = db.Exec(table_schema) if err != nil { - return nil, fmt.Errorf("Error in creating rules table: %s", err.Error()) + return nil, fmt.Errorf("error in creating rules table: %s", err.Error()) } table_schema = `CREATE TABLE IF NOT EXISTS notification_channels ( @@ -76,7 +76,7 @@ func InitDB(dataSourceName string) (*sqlx.DB, error) { _, err = db.Exec(table_schema) if err != nil { - return nil, fmt.Errorf("Error in creating notification_channles table: %s", err.Error()) + return nil, fmt.Errorf("error in creating notification_channles table: %s", err.Error()) } table_schema = `CREATE TABLE IF NOT EXISTS ttl_status ( @@ -92,7 +92,7 @@ func InitDB(dataSourceName string) (*sqlx.DB, error) { _, err = db.Exec(table_schema) if err != nil { - return nil, fmt.Errorf("Error in creating ttl_status table: %s", err.Error()) + return nil, fmt.Errorf("error in creating ttl_status table: %s", err.Error()) } return db, nil @@ -179,7 +179,7 @@ func CreateDashboard(data map[string]interface{}, fm interfaces.FeatureLookup) ( func GetDashboards() ([]Dashboard, *model.ApiError) { dashboards := []Dashboard{} - query := fmt.Sprintf("SELECT * FROM dashboards;") + query := `SELECT * FROM dashboards` err := db.Select(&dashboards, query) if err != nil { @@ -197,9 +197,9 @@ func DeleteDashboard(uuid string, fm interfaces.FeatureLookup) *model.ApiError { return dErr } - query := fmt.Sprintf("DELETE FROM dashboards WHERE uuid='%s';", uuid) + query := `DELETE FROM dashboards WHERE uuid=?` - result, err := db.Exec(query) + result, err := db.Exec(query, uuid) if err != nil { return &model.ApiError{Typ: model.ErrorExec, Err: err} @@ -224,9 +224,9 @@ func DeleteDashboard(uuid string, fm interfaces.FeatureLookup) *model.ApiError { func GetDashboard(uuid string) (*Dashboard, *model.ApiError) { dashboard := Dashboard{} - query := fmt.Sprintf("SELECT * FROM dashboards WHERE uuid='%s';", uuid) + query := `SELECT * FROM dashboards WHERE uuid=?` - err := db.Get(&dashboard, query) + err := db.Get(&dashboard, query, uuid) if err != nil { return nil, &model.ApiError{Typ: model.ErrorNotFound, Err: fmt.Errorf("no dashboard found with uuid: %s", uuid)} } From 7586b50c5a7360b2c494afde159a1e6de975ed39 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 24 Aug 2023 17:14:42 +0530 Subject: [PATCH 2/2] fix: use query-service API to fetch triggered alerts (#3417) --- deploy/docker-swarm/common/nginx-config.conf | 4 ---- deploy/docker/common/nginx-config.conf | 4 ---- frontend/src/api/alerts/getTriggered.ts | 10 +++++----- pkg/query-service/app/http_handler.go | 21 ++++++++++++++++++++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/deploy/docker-swarm/common/nginx-config.conf b/deploy/docker-swarm/common/nginx-config.conf index 158effc8bf..f7943e21aa 100644 --- a/deploy/docker-swarm/common/nginx-config.conf +++ b/deploy/docker-swarm/common/nginx-config.conf @@ -24,10 +24,6 @@ server { try_files $uri $uri/ /index.html; } - location /api/alertmanager { - proxy_pass http://alertmanager:9093/api/v2; - } - location ~ ^/api/(v1|v3)/logs/(tail|livetail){ proxy_pass http://query-service:8080; proxy_http_version 1.1; diff --git a/deploy/docker/common/nginx-config.conf b/deploy/docker/common/nginx-config.conf index 158effc8bf..f7943e21aa 100644 --- a/deploy/docker/common/nginx-config.conf +++ b/deploy/docker/common/nginx-config.conf @@ -24,10 +24,6 @@ server { try_files $uri $uri/ /index.html; } - location /api/alertmanager { - proxy_pass http://alertmanager:9093/api/v2; - } - location ~ ^/api/(v1|v3)/logs/(tail|livetail){ proxy_pass http://query-service:8080; proxy_http_version 1.1; diff --git a/frontend/src/api/alerts/getTriggered.ts b/frontend/src/api/alerts/getTriggered.ts index 160b9a3b93..6955cc315c 100644 --- a/frontend/src/api/alerts/getTriggered.ts +++ b/frontend/src/api/alerts/getTriggered.ts @@ -1,4 +1,4 @@ -import { AxiosAlertManagerInstance } from 'api'; +import axios from 'api'; import { ErrorResponseHandler } from 'api/ErrorResponseHandler'; import { AxiosError } from 'axios'; import convertObjectIntoParams from 'lib/query/convertObjectIntoParams'; @@ -11,15 +11,15 @@ const getTriggered = async ( try { const queryParams = convertObjectIntoParams(props); - const response = await AxiosAlertManagerInstance.get( - `/alerts?${queryParams}`, - ); + const response = await axios.get(`/alerts?${queryParams}`); + + const amData = JSON.parse(response.data.data); return { statusCode: 200, error: null, message: response.data.status, - payload: response.data, + payload: amData.data, }; } catch (error) { return ErrorResponseHandler(error as AxiosError); diff --git a/pkg/query-service/app/http_handler.go b/pkg/query-service/app/http_handler.go index 46029a9e39..fa55154ac2 100644 --- a/pkg/query-service/app/http_handler.go +++ b/pkg/query-service/app/http_handler.go @@ -319,6 +319,8 @@ func (aH *APIHandler) RegisterRoutes(router *mux.Router, am *AuthMiddleware) { router.HandleFunc("/api/v1/channels", am.EditAccess(aH.createChannel)).Methods(http.MethodPost) router.HandleFunc("/api/v1/testChannel", am.EditAccess(aH.testChannel)).Methods(http.MethodPost) + router.HandleFunc("/api/v1/alerts", am.ViewAccess(aH.getAlerts)).Methods(http.MethodGet) + router.HandleFunc("/api/v1/rules", am.ViewAccess(aH.listRules)).Methods(http.MethodGet) router.HandleFunc("/api/v1/rules/{id}", am.ViewAccess(aH.getRule)).Methods(http.MethodGet) router.HandleFunc("/api/v1/rules", am.EditAccess(aH.createRule)).Methods(http.MethodPost) @@ -1195,6 +1197,25 @@ func (aH *APIHandler) createChannel(w http.ResponseWriter, r *http.Request) { } +func (aH *APIHandler) getAlerts(w http.ResponseWriter, r *http.Request) { + params := r.URL.Query() + amEndpoint := constants.GetAlertManagerApiPrefix() + resp, err := http.Get(amEndpoint + "v1/alerts" + "?" + params.Encode()) + if err != nil { + RespondError(w, &model.ApiError{Typ: model.ErrorInternal, Err: err}, nil) + return + } + + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + RespondError(w, &model.ApiError{Typ: model.ErrorInternal, Err: err}, nil) + return + } + + aH.Respond(w, string(body)) +} + func (aH *APIHandler) createRule(w http.ResponseWriter, r *http.Request) { defer r.Body.Close()