| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package configissues
- import (
- "context"
- "maps"
- "sync"
- "time"
- health "github.com/jamesread/armature-health/v2"
- log "github.com/sirupsen/logrus"
- )
- // Issues are kept by armature-health in memory, grouped by where they come from.
- // A rebuild replaces the "config" and "config.load" sources, and leaves "runtime"
- // alone, so issues found while OliveTin is running survive config reloads.
- const (
- sourceConfig = "config"
- sourceConfigLoad = "config.load"
- sourceRuntime = "runtime"
- )
- var sourceOrder = []string{sourceConfigLoad, sourceRuntime, sourceConfig}
- // OliveTin only shows open issues, so resolved ones are not kept for long.
- const resolvedRetention = time.Hour
- var (
- service = health.NewService(nil, nil, nil, health.Options{
- SourceTitles: map[string]string{
- sourceConfig: "Configuration",
- sourceConfigLoad: "Configuration loading",
- sourceRuntime: "Runtime",
- },
- ResolvedRetention: resolvedRetention,
- })
- stickyMu sync.Mutex
- sticky []Issue
- // writeMu makes each change and the logging of the issues it adds one step,
- // so concurrent reports cannot log the same new issue twice.
- writeMu sync.Mutex
- // current is the ordered issue list, rebuilt after each change. Every change
- // goes through this package, so it always matches what is stored.
- snapshotMu sync.RWMutex
- current []Issue
- currentSeverities map[string]string
- )
- // BeginConfigLoad clears sticky issues captured during config decode/include load.
- func BeginConfigLoad() {
- stickyMu.Lock()
- defer stickyMu.Unlock()
- sticky = nil
- }
- // ReportSticky records an issue that cannot be re-derived after config decode
- // (for example unset environment variables that were already expanded).
- func ReportSticky(issue Issue) {
- stickyMu.Lock()
- defer stickyMu.Unlock()
- sticky = append(sticky, issue)
- }
- // CopySticky returns a copy of sticky load-time issues.
- func CopySticky() []Issue {
- stickyMu.Lock()
- defer stickyMu.Unlock()
- out := make([]Issue, len(sticky))
- copy(out, sticky)
- return out
- }
- // Replace sets the issues derived from the current configuration. Issues that
- // were already present are kept (and not logged again), and missing ones are
- // resolved.
- func Replace(next []Issue) {
- setSource(sourceConfig, next)
- }
- // ReplaceLoadIssues sets the issues found while loading configuration.
- func ReplaceLoadIssues(next []Issue) {
- setSource(sourceConfigLoad, next)
- }
- // Report adds a live issue if it is not already present and logs it when new.
- // Use this for runtime discoveries that happen outside Rebuild (for example
- // filesystem watcher setup failures). The next Rebuild resolves it unless the
- // rebuild finds it again.
- func Report(issue Issue) {
- raise(sourceConfig, issue)
- }
- // List returns the current issues.
- func List() []Issue {
- snapshotMu.RLock()
- defer snapshotMu.RUnlock()
- out := make([]Issue, len(current))
- copy(out, current)
- return out
- }
- // Count returns the number of current issues.
- func Count() int {
- snapshotMu.RLock()
- defer snapshotMu.RUnlock()
- return len(current)
- }
- func setSource(sourceID string, next []Issue) {
- writeMu.Lock()
- defer writeMu.Unlock()
- before := copyCurrentSeverities()
- rememberOrder(sourceID, next)
- if err := service.SetIssues(context.Background(), sourceID, toHealthIssues(next)); err != nil {
- log.WithError(err).WithField("source", sourceID).Error("Could not save configuration issues")
- return
- }
- refreshSnapshot()
- logNewIssues(before, next)
- }
- func raise(sourceID string, issue Issue) {
- writeMu.Lock()
- defer writeMu.Unlock()
- before := copyCurrentSeverities()
- if err := service.Raise(context.Background(), sourceID, toHealthIssue(issue)); err != nil {
- log.WithError(err).WithField("source", sourceID).Error("Could not save configuration issue")
- return
- }
- // Raising an issue that is already listed with the same severity only adds an
- // occurrence, so the list does not need rebuilding. This keeps repeated reports,
- // such as every call to a deprecated API method, cheap.
- if before[issueKey(issue)] == string(healthSeverity(issue.Severity)) {
- return
- }
- refreshSnapshot()
- logNewIssues(before, []Issue{issue})
- }
- // refreshSnapshot rebuilds the list returned by List after a change, so that
- // reading the list does not have to sort every stored issue each time.
- func refreshSnapshot() {
- stored, err := service.ListIssues(context.Background(), false, true)
- if err != nil {
- log.WithError(err).Error("Could not list configuration issues")
- return
- }
- // Severities are keyed by the stored key rather than one worked out again from
- // the listed issue, because armature-health shortens long titles and attributes.
- views := orderedIssueViews(stored)
- ordered := make([]Issue, 0, len(views))
- severities := make(map[string]string, len(views))
- for _, view := range views {
- ordered = append(ordered, fromHealthIssue(view))
- severities[view.IssueKey] = view.Severity
- }
- snapshotMu.Lock()
- defer snapshotMu.Unlock()
- current = ordered
- currentSeverities = severities
- }
- func copyCurrentSeverities() map[string]string {
- snapshotMu.RLock()
- defer snapshotMu.RUnlock()
- out := make(map[string]string, len(currentSeverities))
- maps.Copy(out, currentSeverities)
- return out
- }
- // logNewIssues logs issues that were not open before, or whose severity changed,
- // in the order they were collected, so startup and reloads do not repeat the
- // same warnings.
- func logNewIssues(before map[string]string, issues []Issue) {
- for _, issue := range issues {
- key := issueKey(issue)
- severity := string(healthSeverity(issue.Severity))
- if before[key] == severity {
- continue
- }
- before[key] = severity
- logNewIssue(issue)
- }
- }
- func logNewIssue(issue Issue) {
- fields := log.Fields{
- "code": issue.Code,
- "actionTitle": issue.ActionTitle,
- "argumentName": issue.ArgumentName,
- "configFile": issue.ConfigFile,
- "source": issue.Source,
- }
- if issue.Severity == SeverityError {
- log.WithFields(fields).Error(issue.Message)
- return
- }
- log.WithFields(fields).Warn(issue.Message)
- }
|