From 3ee51770fd02ba1342d5e4681fd5a551b35edbf7 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Tue, 2 Jul 2024 12:03:01 +0530 Subject: [PATCH] chore: remove rules dependency in CH reader (#5396) --- .../app/clickhouseReader/reader.go | 31 ---------------- pkg/query-service/interfaces/interface.go | 1 - pkg/query-service/rules/db.go | 35 +++++++++++++++++++ pkg/query-service/rules/manager.go | 3 ++ pkg/query-service/telemetry/telemetry.go | 8 ++++- 5 files changed, 45 insertions(+), 33 deletions(-) diff --git a/pkg/query-service/app/clickhouseReader/reader.go b/pkg/query-service/app/clickhouseReader/reader.go index 4e5f342301..ae8fb64c94 100644 --- a/pkg/query-service/app/clickhouseReader/reader.go +++ b/pkg/query-service/app/clickhouseReader/reader.go @@ -53,7 +53,6 @@ import ( "go.signoz.io/signoz/pkg/query-service/interfaces" "go.signoz.io/signoz/pkg/query-service/model" v3 "go.signoz.io/signoz/pkg/query-service/model/v3" - "go.signoz.io/signoz/pkg/query-service/rules" "go.signoz.io/signoz/pkg/query-service/telemetry" "go.signoz.io/signoz/pkg/query-service/utils" ) @@ -3420,36 +3419,6 @@ func countPanelsInDashboard(data map[string]interface{}) model.DashboardsInfo { } } -func (r *ClickHouseReader) GetAlertsInfo(ctx context.Context) (*model.AlertsInfo, error) { - alertsInfo := model.AlertsInfo{} - // fetch alerts from rules db - query := "SELECT data FROM rules" - var alertsData []string - err := r.localDB.Select(&alertsData, query) - if err != nil { - zap.L().Error("Error in processing sql query", zap.Error(err)) - return &alertsInfo, err - } - for _, alert := range alertsData { - var rule rules.GettableRule - err = json.Unmarshal([]byte(alert), &rule) - if err != nil { - zap.L().Error("invalid rule data", zap.Error(err)) - continue - } - if rule.AlertType == "LOGS_BASED_ALERT" { - alertsInfo.LogsBasedAlerts = alertsInfo.LogsBasedAlerts + 1 - } else if rule.AlertType == "METRIC_BASED_ALERT" { - alertsInfo.MetricBasedAlerts = alertsInfo.MetricBasedAlerts + 1 - } else if rule.AlertType == "TRACES_BASED_ALERT" { - alertsInfo.TracesBasedAlerts = alertsInfo.TracesBasedAlerts + 1 - } - alertsInfo.TotalAlerts = alertsInfo.TotalAlerts + 1 - } - - return &alertsInfo, nil -} - func (r *ClickHouseReader) GetSavedViewsInfo(ctx context.Context) (*model.SavedViewsInfo, error) { savedViewsInfo := model.SavedViewsInfo{} savedViews, err := explorer.GetViews() diff --git a/pkg/query-service/interfaces/interface.go b/pkg/query-service/interfaces/interface.go index 9a5e33e5ff..0ab20fed0e 100644 --- a/pkg/query-service/interfaces/interface.go +++ b/pkg/query-service/interfaces/interface.go @@ -73,7 +73,6 @@ type Reader interface { LiveTailLogsV3(ctx context.Context, query string, timestampStart uint64, idStart string, client *v3.LogsLiveTailClient) GetDashboardsInfo(ctx context.Context) (*model.DashboardsInfo, error) - GetAlertsInfo(ctx context.Context) (*model.AlertsInfo, error) GetSavedViewsInfo(ctx context.Context) (*model.SavedViewsInfo, error) GetTotalSpans(ctx context.Context) (uint64, error) GetTotalLogs(ctx context.Context) (uint64, error) diff --git a/pkg/query-service/rules/db.go b/pkg/query-service/rules/db.go index abf584a375..4d51d462dd 100644 --- a/pkg/query-service/rules/db.go +++ b/pkg/query-service/rules/db.go @@ -2,6 +2,7 @@ package rules import ( "context" + "encoding/json" "fmt" "strconv" "time" @@ -9,6 +10,7 @@ import ( "github.com/jmoiron/sqlx" "go.signoz.io/signoz/pkg/query-service/auth" "go.signoz.io/signoz/pkg/query-service/common" + "go.signoz.io/signoz/pkg/query-service/model" "go.uber.org/zap" ) @@ -43,6 +45,9 @@ type RuleDB interface { // GetAllPlannedMaintenance fetches the maintenance definitions from db GetAllPlannedMaintenance(ctx context.Context) ([]PlannedMaintenance, error) + + // used for internal telemetry + GetAlertsInfo(ctx context.Context) (*model.AlertsInfo, error) } type StoredRule struct { @@ -295,3 +300,33 @@ func (r *ruleDB) EditPlannedMaintenance(ctx context.Context, maintenance Planned return "", nil } + +func (r *ruleDB) GetAlertsInfo(ctx context.Context) (*model.AlertsInfo, error) { + alertsInfo := model.AlertsInfo{} + // fetch alerts from rules db + query := "SELECT data FROM rules" + var alertsData []string + err := r.Select(&alertsData, query) + if err != nil { + zap.L().Error("Error in processing sql query", zap.Error(err)) + return &alertsInfo, err + } + for _, alert := range alertsData { + var rule GettableRule + err = json.Unmarshal([]byte(alert), &rule) + if err != nil { + zap.L().Error("invalid rule data", zap.Error(err)) + continue + } + if rule.AlertType == "LOGS_BASED_ALERT" { + alertsInfo.LogsBasedAlerts = alertsInfo.LogsBasedAlerts + 1 + } else if rule.AlertType == "METRIC_BASED_ALERT" { + alertsInfo.MetricBasedAlerts = alertsInfo.MetricBasedAlerts + 1 + } else if rule.AlertType == "TRACES_BASED_ALERT" { + alertsInfo.TracesBasedAlerts = alertsInfo.TracesBasedAlerts + 1 + } + alertsInfo.TotalAlerts = alertsInfo.TotalAlerts + 1 + } + + return &alertsInfo, nil +} diff --git a/pkg/query-service/rules/manager.go b/pkg/query-service/rules/manager.go index 20951f56a0..6050981de8 100644 --- a/pkg/query-service/rules/manager.go +++ b/pkg/query-service/rules/manager.go @@ -25,6 +25,7 @@ import ( "go.signoz.io/signoz/pkg/query-service/interfaces" "go.signoz.io/signoz/pkg/query-service/model" v3 "go.signoz.io/signoz/pkg/query-service/model/v3" + "go.signoz.io/signoz/pkg/query-service/telemetry" "go.signoz.io/signoz/pkg/query-service/utils/labels" ) @@ -112,6 +113,8 @@ func NewManager(o *ManagerOptions) (*Manager, error) { db := NewRuleDB(o.DBConn) + telemetry.GetInstance().SetAlertsInfoCallback(db.GetAlertsInfo) + m := &Manager{ tasks: map[string]Task{}, rules: map[string]Rule{}, diff --git a/pkg/query-service/telemetry/telemetry.go b/pkg/query-service/telemetry/telemetry.go index 9b75259296..555d4a5d6c 100644 --- a/pkg/query-service/telemetry/telemetry.go +++ b/pkg/query-service/telemetry/telemetry.go @@ -185,6 +185,12 @@ type Telemetry struct { patTokenUser bool countUsers int8 mutex sync.RWMutex + + alertsInfoCallback func(ctx context.Context) (*model.AlertsInfo, error) +} + +func (a *Telemetry) SetAlertsInfoCallback(callback func(ctx context.Context) (*model.AlertsInfo, error)) { + a.alertsInfoCallback = callback } func createTelemetry() { @@ -310,7 +316,7 @@ func createTelemetry() { telemetry.SendEvent(TELEMETRY_EVENT_HEART_BEAT, data, user.Email, false, false) } } - alertsInfo, err := telemetry.reader.GetAlertsInfo(context.Background()) + alertsInfo, err := telemetry.alertsInfoCallback(context.Background()) if err == nil { dashboardsInfo, err := telemetry.reader.GetDashboardsInfo(context.Background()) if err == nil {