Skip to content

Commit 5a721a1

Browse files
authored
Merge pull request #61 from foomo/fix/category-nil-pointer
Fix nil pointer access on categories
2 parents a38c984 + 90a7327 commit 5a721a1

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

collector/collector.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func collectLighthouseResults(prefix string, cats []string, lhr *pagespeedonline
172172
ch <- prometheus.MustNewConstMetric(
173173
prometheus.NewDesc(fqname(prefix, "total_duration_seconds"), "The total time spent in seconds loading the page and evaluating audits.", nil, constLabels),
174174
prometheus.GaugeValue,
175-
lhr.Timing.Total/1000) //ms -> seconds
175+
lhr.Timing.Total/1000) // ms -> seconds
176176

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

185185
for _, c := range cats {
186-
score, err := strconv.ParseFloat(fmt.Sprint(categories[c].Score), 64)
187-
if err != nil {
188-
logrus.WithError(err).Warn("could not parse category score")
189-
continue
190-
}
186+
if categories[c] != nil {
187+
score, err := strconv.ParseFloat(fmt.Sprint(categories[c].Score), 64)
188+
if err != nil {
189+
logrus.WithError(err).Warn("could not parse category score")
190+
continue
191+
}
191192

192-
ch <- prometheus.MustNewConstMetric(
193-
prometheus.NewDesc(fqname(prefix, "category_score"), "Lighthouse score for the specified category", []string{"category"}, constLabels),
194-
prometheus.GaugeValue,
195-
score,
196-
c)
193+
ch <- prometheus.MustNewConstMetric(
194+
prometheus.NewDesc(fqname(prefix, "category_score"), "Lighthouse score for the specified category", []string{"category"}, constLabels),
195+
prometheus.GaugeValue,
196+
score,
197+
c)
198+
}
197199
}
198200

199201
for k, v := range lhr.Audits {

0 commit comments

Comments
 (0)