Skip to content

Commit

Permalink
Handle empty value in environmental sources
Browse files Browse the repository at this point in the history
Sometimes a topic might be availabe but no value is published to it/or
is unpublished, to indicate a lack of data. Explicitly handle that case,
so we don't get a log full of empty string to float conversion errors.
  • Loading branch information
daenney committed Jun 30, 2022
1 parent 640f15a commit e336fc1
Showing 1 changed file with 62 additions and 22 deletions.
84 changes: 62 additions & 22 deletions collectors/environmental.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,96 +141,136 @@ func (c *EnvironmentalCollector) Collect(ch chan<- prometheus.Metric) {

for _, s := range devices {
if s.Feature(feature.CurrentRelativeHumidity.String()).Exists() {
v, err := toFloat(s.Feature(feature.CurrentRelativeHumidity.String()).Value())
v := s.Feature(feature.CurrentRelativeHumidity.String()).Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
humidity[s.Info().Topic] = v
humidity[s.Info().Topic] = vf
ch <- prometheus.MustNewConstMetric(c.relativeHumidity,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if s.Feature(feature.CurrentTemperature.String()).Exists() {
v, err := toFloat(s.Feature(feature.CurrentTemperature.String()).Value())
v := s.Feature(feature.CurrentTemperature.String()).Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
temperature[s.Info().Topic] = v
temperature[s.Info().Topic] = vf
ch <- prometheus.MustNewConstMetric(c.temperature,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("precipitation"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.precipitation,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("airPressure"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.airPressure,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("windSpeed"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.windSpeed,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("windDirection"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.windDirection,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("globalRadiation"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.globalRadiation,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("pm2_5Density"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.pm25,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("airQuality"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.airQuality,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if ft := s.Feature("waterLevel"); ft.Exists() {
v, err := toFloat(ft.Value())
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.waterLevel,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
}

Expand Down

0 comments on commit e336fc1

Please sign in to comment.