Skip to content

Commit

Permalink
Merge pull request #61 from foomo/fix/category-nil-pointer
Browse files Browse the repository at this point in the history
Fix nil pointer access on categories
  • Loading branch information
smartinov authored May 10, 2024
2 parents a38c984 + 90a7327 commit 5a721a1
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func collectLighthouseResults(prefix string, cats []string, lhr *pagespeedonline
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(fqname(prefix, "total_duration_seconds"), "The total time spent in seconds loading the page and evaluating audits.", nil, constLabels),
prometheus.GaugeValue,
lhr.Timing.Total/1000) //ms -> seconds
lhr.Timing.Total/1000) // ms -> seconds

categories := map[string]*pagespeedonline.LighthouseCategoryV5{
CategoryPerformance: lhr.Categories.Performance,
Expand All @@ -183,17 +183,19 @@ func collectLighthouseResults(prefix string, cats []string, lhr *pagespeedonline
}

for _, c := range cats {
score, err := strconv.ParseFloat(fmt.Sprint(categories[c].Score), 64)
if err != nil {
logrus.WithError(err).Warn("could not parse category score")
continue
}
if categories[c] != nil {
score, err := strconv.ParseFloat(fmt.Sprint(categories[c].Score), 64)
if err != nil {
logrus.WithError(err).Warn("could not parse category score")
continue
}

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(fqname(prefix, "category_score"), "Lighthouse score for the specified category", []string{"category"}, constLabels),
prometheus.GaugeValue,
score,
c)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(fqname(prefix, "category_score"), "Lighthouse score for the specified category", []string{"category"}, constLabels),
prometheus.GaugeValue,
score,
c)
}
}

for k, v := range lhr.Audits {
Expand Down

0 comments on commit 5a721a1

Please sign in to comment.