|
| 1 | +package collectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + semver "github.com/blang/semver/v4" |
| 13 | + "github.com/prometheus/client_golang/prometheus" |
| 14 | + |
| 15 | + "github.com/rocket-pool/smartnode/shared" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + githubLatestReleaseURL = "https://api.github.com/repos/rocket-pool/smartnode/releases/latest" |
| 20 | + versionCheckInterval = time.Hour |
| 21 | + versionCheckTimeout = 15 * time.Second |
| 22 | +) |
| 23 | + |
| 24 | +// VersionUpdateCollector exposes whether a newer Smart Node release is available. |
| 25 | +type VersionUpdateCollector struct { |
| 26 | + versionUpdate *prometheus.Desc |
| 27 | + versionUpdateInfo *prometheus.Desc |
| 28 | + current string |
| 29 | + latestURL string |
| 30 | + client *http.Client |
| 31 | + logf func(string, ...interface{}) |
| 32 | + |
| 33 | + mu sync.Mutex |
| 34 | + updateAvailable float64 |
| 35 | + latestVersion string |
| 36 | + lastChecked time.Time |
| 37 | +} |
| 38 | + |
| 39 | +type githubReleaseResponse struct { |
| 40 | + TagName string `json:"tag_name"` |
| 41 | +} |
| 42 | + |
| 43 | +// NewVersionUpdateCollector creates a collector backed by an hourly GitHub release check. |
| 44 | +func NewVersionUpdateCollector(logf func(string, ...interface{})) *VersionUpdateCollector { |
| 45 | + return &VersionUpdateCollector{ |
| 46 | + versionUpdate: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "version_update"), |
| 47 | + "New Rocket Pool version available", |
| 48 | + nil, nil, |
| 49 | + ), |
| 50 | + versionUpdateInfo: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "version_update_info"), |
| 51 | + "The latest available Rocket Pool version", |
| 52 | + []string{"version"}, nil, |
| 53 | + ), |
| 54 | + current: shared.RocketPoolVersion(), |
| 55 | + latestURL: githubLatestReleaseURL, |
| 56 | + client: &http.Client{ |
| 57 | + Timeout: versionCheckTimeout, |
| 58 | + }, |
| 59 | + logf: logf, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Describe writes metric descriptions to the Prometheus channel. |
| 64 | +func (collector *VersionUpdateCollector) Describe(channel chan<- *prometheus.Desc) { |
| 65 | + channel <- collector.versionUpdate |
| 66 | + channel <- collector.versionUpdateInfo |
| 67 | +} |
| 68 | + |
| 69 | +// Collect emits the latest cached version update status. |
| 70 | +func (collector *VersionUpdateCollector) Collect(channel chan<- prometheus.Metric) { |
| 71 | + collector.checkIfDue(context.Background()) |
| 72 | + |
| 73 | + collector.mu.Lock() |
| 74 | + updateAvailable := collector.updateAvailable |
| 75 | + latestVersion := collector.latestVersion |
| 76 | + collector.mu.Unlock() |
| 77 | + |
| 78 | + channel <- prometheus.MustNewConstMetric( |
| 79 | + collector.versionUpdate, prometheus.GaugeValue, updateAvailable) |
| 80 | + if latestVersion != "" { |
| 81 | + channel <- prometheus.MustNewConstMetric( |
| 82 | + collector.versionUpdateInfo, prometheus.GaugeValue, 1, latestVersion) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (collector *VersionUpdateCollector) checkIfDue(ctx context.Context) { |
| 87 | + collector.mu.Lock() |
| 88 | + defer collector.mu.Unlock() |
| 89 | + |
| 90 | + if time.Since(collector.lastChecked) < versionCheckInterval { |
| 91 | + return |
| 92 | + } |
| 93 | + collector.lastChecked = time.Now() |
| 94 | + |
| 95 | + updateAvailable, latestVersion, err := collector.checkForUpdate(ctx) |
| 96 | + if err != nil { |
| 97 | + if collector.logf != nil { |
| 98 | + collector.logf("Error checking latest Rocket Pool release: %v", err) |
| 99 | + } |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + if updateAvailable { |
| 104 | + collector.updateAvailable = 1 |
| 105 | + } else { |
| 106 | + collector.updateAvailable = 0 |
| 107 | + } |
| 108 | + collector.latestVersion = latestVersion |
| 109 | +} |
| 110 | + |
| 111 | +func (collector *VersionUpdateCollector) checkForUpdate(ctx context.Context) (bool, string, error) { |
| 112 | + latest, err := collector.getLatestVersion(ctx) |
| 113 | + if err != nil { |
| 114 | + return false, "", err |
| 115 | + } |
| 116 | + |
| 117 | + updateAvailable, err := isNewerVersion(collector.current, latest) |
| 118 | + if err != nil { |
| 119 | + return false, "", err |
| 120 | + } |
| 121 | + |
| 122 | + return updateAvailable, latest, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (collector *VersionUpdateCollector) getLatestVersion(ctx context.Context) (string, error) { |
| 126 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, collector.latestURL, nil) |
| 127 | + if err != nil { |
| 128 | + return "", fmt.Errorf("error creating GitHub release request: %w", err) |
| 129 | + } |
| 130 | + req.Header.Set("Accept", "application/vnd.github+json") |
| 131 | + req.Header.Set("User-Agent", "rocketpool-smartnode") |
| 132 | + |
| 133 | + resp, err := collector.client.Do(req) |
| 134 | + if err != nil { |
| 135 | + return "", fmt.Errorf("error fetching latest GitHub release: %w", err) |
| 136 | + } |
| 137 | + defer resp.Body.Close() |
| 138 | + |
| 139 | + if resp.StatusCode != http.StatusOK { |
| 140 | + return "", fmt.Errorf("GitHub release request returned status %s", resp.Status) |
| 141 | + } |
| 142 | + |
| 143 | + var release githubReleaseResponse |
| 144 | + if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { |
| 145 | + return "", fmt.Errorf("error decoding latest GitHub release: %w", err) |
| 146 | + } |
| 147 | + if strings.TrimSpace(release.TagName) == "" { |
| 148 | + return "", fmt.Errorf("latest GitHub release did not include a tag_name") |
| 149 | + } |
| 150 | + |
| 151 | + return release.TagName, nil |
| 152 | +} |
| 153 | + |
| 154 | +func isNewerVersion(currentVersion string, latestVersion string) (bool, error) { |
| 155 | + current, err := semver.ParseTolerant(strings.TrimSpace(currentVersion)) |
| 156 | + if err != nil { |
| 157 | + return false, fmt.Errorf("error parsing current version %q: %w", currentVersion, err) |
| 158 | + } |
| 159 | + |
| 160 | + latest, err := semver.ParseTolerant(strings.TrimSpace(latestVersion)) |
| 161 | + if err != nil { |
| 162 | + return false, fmt.Errorf("error parsing latest version %q: %w", latestVersion, err) |
| 163 | + } |
| 164 | + |
| 165 | + return latest.Compare(current) > 0, nil |
| 166 | +} |
0 commit comments