Skip to content

Commit 078be7e

Browse files
idoqodependabot[bot]ademidoff
authored
PMM-14700 Limit advisors to available services only (#4945)
* reduce saas package dependency * drop tiering * add license headers * fix file generation * ignore severity string file * golang ci lint * fix errors * fix go-consistent * fix alignment * fix go-consistent * drop logger package * limit advisors to available services only * replace fallthrough * PMM-14012 drop portal connections (#4912) * drop portal communication * drop platform connections that are unrelated to telemetry * mark platform fields as deprecated * Bump anchore/sbom-action from 0.21.1 to 0.22.0 (#4952) Bumps [anchore/sbom-action](https://github.com/anchore/sbom-action) from 0.21.1 to 0.22.0. - [Release notes](https://github.com/anchore/sbom-action/releases) - [Changelog](https://github.com/anchore/sbom-action/blob/main/RELEASE.md) - [Commits](anchore/sbom-action@0b82b0b...62ad528) --- updated-dependencies: - dependency-name: anchore/sbom-action dependency-version: 0.22.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * tidy modules * PMM-14700 Filter out only pmm-server-postgresql svc * chore: format the code * PMM-14700 Remove redundant comments --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Alex Demidoff <a@demidoff.me>
1 parent 0224e7a commit 078be7e

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

managed/models/service_helpers.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ func FindActiveServiceTypes(q *reform.Querier) ([]ServiceType, error) {
162162
return res, nil
163163
}
164164

165+
// FindActiveUserServiceTypes returns all active Service Types, excluding pmm-server-postgresql service.
166+
func FindActiveUserServiceTypes(q *reform.Querier) ([]ServiceType, error) {
167+
query := fmt.Sprintf(`SELECT DISTINCT service_type FROM %s WHERE service_name != $1`, ServiceTable.s.SQLName)
168+
rows, err := q.Query(query, PMMServerPostgreSQLServiceName)
169+
if err != nil {
170+
return nil, err
171+
}
172+
173+
defer func() {
174+
if rowsErr := rows.Close(); rowsErr != nil {
175+
logrus.Debug(rowsErr)
176+
}
177+
}()
178+
179+
var res []ServiceType
180+
for rows.Next() {
181+
var serviceType ServiceType
182+
if err = rows.Scan(&serviceType); err != nil {
183+
return nil, err
184+
}
185+
186+
res = append(res, serviceType)
187+
}
188+
189+
return res, nil
190+
}
191+
165192
// FindServiceByID searches Service by ID.
166193
func FindServiceByID(q *reform.Querier, id string) (*Service, error) {
167194
if id == "" {
@@ -188,7 +215,7 @@ func FindServicesByIDs(q *reform.Querier, ids []string) (map[string]*Service, er
188215

189216
p := strings.Join(q.Placeholders(1, len(ids)), ", ")
190217
tail := fmt.Sprintf("WHERE service_id IN (%s) ORDER BY service_id", p)
191-
args := make([]interface{}, len(ids))
218+
args := make([]any, len(ids))
192219
for i, id := range ids {
193220
args[i] = id
194221
}

managed/services/checks/checks.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,20 @@ func (s *Service) filterChecks(checks map[string]check.Check, group check.Interv
600600
return res
601601
}
602602

603+
// getActiveUserServiceTypes returns a set of service types that have at least one user-added service.
604+
func (s *Service) getActiveUserServiceTypes() (map[models.ServiceType]struct{}, error) {
605+
serviceTypes, err := models.FindActiveUserServiceTypes(s.db.Querier)
606+
if err != nil {
607+
return nil, errors.WithStack(err)
608+
}
609+
610+
result := make(map[models.ServiceType]struct{}, len(serviceTypes))
611+
for _, st := range serviceTypes {
612+
result[st] = struct{}{}
613+
}
614+
return result, nil
615+
}
616+
603617
// executeChecks runs checks for all reachable services. If intervalGroup specified only checks from that group will be
604618
// executed. If checkNames specified then only matched checks will be executed.
605619
func (s *Service) executeChecks(ctx context.Context, intervalGroup check.Interval, checkNames []string) ([]services.CheckResult, error) {
@@ -608,24 +622,44 @@ func (s *Service) executeChecks(ctx context.Context, intervalGroup check.Interva
608622
return nil, errors.WithStack(err)
609623
}
610624

625+
activeServiceTypes, err := s.getActiveUserServiceTypes()
626+
if err != nil {
627+
return nil, errors.WithStack(err)
628+
}
629+
611630
var res []services.CheckResult
612631
checks, err := s.GetChecks()
613632
if err != nil {
614633
return nil, errors.WithStack(err)
615634
}
616635
mySQLChecks, postgreSQLChecks, mongoDBChecks := groupChecksByDB(s.l, checks)
617636

618-
mySQLChecks = s.filterChecks(mySQLChecks, intervalGroup, disabledChecks, checkNames)
619-
mySQLCheckResults := s.executeChecksForTargetType(ctx, models.MySQLServiceType, mySQLChecks)
620-
res = append(res, mySQLCheckResults...)
637+
// Execute MySQL checks only if MySQL services exist
638+
if _, hasMySQL := activeServiceTypes[models.MySQLServiceType]; hasMySQL {
639+
mySQLChecks = s.filterChecks(mySQLChecks, intervalGroup, disabledChecks, checkNames)
640+
mySQLCheckResults := s.executeChecksForTargetType(ctx, models.MySQLServiceType, mySQLChecks)
641+
res = append(res, mySQLCheckResults...)
642+
} else {
643+
s.l.Info("Skipping MySQL advisor checks: no MySQL services in inventory")
644+
}
621645

622-
postgreSQLChecks = s.filterChecks(postgreSQLChecks, intervalGroup, disabledChecks, checkNames)
623-
postgreSQLCheckResults := s.executeChecksForTargetType(ctx, models.PostgreSQLServiceType, postgreSQLChecks)
624-
res = append(res, postgreSQLCheckResults...)
646+
// Execute PostgreSQL checks only if PostgreSQL services exist
647+
if _, hasPostgreSQL := activeServiceTypes[models.PostgreSQLServiceType]; hasPostgreSQL {
648+
postgreSQLChecks = s.filterChecks(postgreSQLChecks, intervalGroup, disabledChecks, checkNames)
649+
postgreSQLCheckResults := s.executeChecksForTargetType(ctx, models.PostgreSQLServiceType, postgreSQLChecks)
650+
res = append(res, postgreSQLCheckResults...)
651+
} else {
652+
s.l.Info("Skipping PostgreSQL advisor checks: no PostgreSQL services in inventory")
653+
}
625654

626-
mongoDBChecks = s.filterChecks(mongoDBChecks, intervalGroup, disabledChecks, checkNames)
627-
mongoDBCheckResults := s.executeChecksForTargetType(ctx, models.MongoDBServiceType, mongoDBChecks)
628-
res = append(res, mongoDBCheckResults...)
655+
// Execute MongoDB checks only if MongoDB services exist
656+
if _, hasMongoDB := activeServiceTypes[models.MongoDBServiceType]; hasMongoDB {
657+
mongoDBChecks = s.filterChecks(mongoDBChecks, intervalGroup, disabledChecks, checkNames)
658+
mongoDBCheckResults := s.executeChecksForTargetType(ctx, models.MongoDBServiceType, mongoDBChecks)
659+
res = append(res, mongoDBCheckResults...)
660+
} else {
661+
s.l.Info("Skipping MongoDB advisor checks: no MongoDB services in inventory")
662+
}
629663

630664
return res, nil
631665
}

0 commit comments

Comments
 (0)