Improve metrics recording
Some checks failed
Build and Publish / release (push) Failing after 40s

This commit is contained in:
2025-01-08 21:28:38 -05:00
parent c23177b62d
commit dc2470da71
11 changed files with 310 additions and 134 deletions

View File

@ -34,6 +34,7 @@ type AmbientWeather struct {
func New(appCtx context.Context, awConfig *AmbientLocalExporterConfig) *AmbientWeather {
return &AmbientWeather{
config: awConfig,
appCtx: appCtx,
}
}
@ -43,6 +44,8 @@ func (aw *AmbientWeather) Init() *AmbientWeather {
aw.awnProvider = &awn.AWNProvider{}
aw.wuProvider = &wunderground.WUProvider{}
aw.l = zerolog.Ctx(aw.appCtx)
aw.l.Trace().Any("awConfig", aw.config).Send()
return aw
}
@ -95,13 +98,15 @@ func (aw *AmbientWeather) handleProviderRequest(
// such as dew point and wind chill
update.Enrich()
// Update metrics
// Prepare metrics if this is the first update
if aw.metrics == nil {
if aw.config.MetricPrefix != "" {
weather.MetricPrefix = aw.config.MetricPrefix
}
aw.metrics = weather.MustInitMetrics(aw.appCtx)
aw.InitMetrics()
}
// Enrich station if configured
aw.enrichStation(update)
// Update metrics
aw.metrics.Update(update)
l.Debug().
@ -110,3 +115,26 @@ func (aw *AmbientWeather) handleProviderRequest(
Msg("successfully handled update")
w.Write([]byte("ok"))
}
func (aw *AmbientWeather) InitMetrics() {
if aw.config.MetricPrefix != "" {
weather.MetricPrefix = aw.config.MetricPrefix
}
aw.metrics = weather.MustInitMetrics(aw.appCtx)
}
func (aw *AmbientWeather) enrichStation(update *weather.WeatherUpdate) {
if update != nil && update.StationID != nil && *update.StationID != "" {
for _, station := range aw.config.WeatherStations {
if *update.StationID == station.AWNPassKey || *update.StationID == station.WundergroundID {
update.StationInfo = &weather.StationInfo{
Type: update.StationType,
Equipment: &station.Equipment,
Name: &station.Name,
Keep: station.KeepMetrics,
Drop: station.DropMetrics,
}
}
}
}
}