Metric implementation
This commit is contained in:
@ -16,26 +16,42 @@ import (
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/provider"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/provider/awn"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/provider/wunderground"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
// These providers implement support for the update sent
|
||||
// when either "AmbientWeather" or "Wunderground" are selected
|
||||
// in the "Custom" section of the AWNet app, or the web UI
|
||||
// of an Ambient WeatherHub
|
||||
var (
|
||||
awnProvider = &awn.AWNProvider{}
|
||||
wuProvider = &wunderground.WUProvider{}
|
||||
)
|
||||
type AmbientWeather struct {
|
||||
// These providers implement support for the update sent
|
||||
// when either "AmbientWeather" or "Wunderground" are selected
|
||||
// in the "Custom" section of the AWNet app, or the web UI
|
||||
// of an Ambient WeatherHub
|
||||
awnProvider provider.AmbientProvider
|
||||
wuProvider provider.AmbientProvider
|
||||
appCtx context.Context
|
||||
metrics *weather.WeatherMetrics
|
||||
l *zerolog.Logger
|
||||
}
|
||||
|
||||
func GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
handleProviderRequest(appCtx, awnProvider, w, r)
|
||||
func New(appCtx context.Context) *AmbientWeather {
|
||||
return &AmbientWeather{
|
||||
appCtx: appCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func GetWundergroundHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
|
||||
func (aw *AmbientWeather) MustInit() {
|
||||
aw.awnProvider = &awn.AWNProvider{}
|
||||
aw.wuProvider = &wunderground.WUProvider{}
|
||||
aw.l = zerolog.Ctx(aw.appCtx)
|
||||
}
|
||||
|
||||
func (aw *AmbientWeather) GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
handleProviderRequest(appCtx, wuProvider, w, r)
|
||||
aw.handleProviderRequest(aw.awnProvider, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (aw *AmbientWeather) GetWundergroundHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
aw.handleProviderRequest(aw.wuProvider, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,14 +59,13 @@ func GetWundergroundHandlerFunc(appCtx context.Context) func(http.ResponseWriter
|
||||
// stable type. Enrich is called on the type to complete
|
||||
// any missing fields as the two providers supported by Ambient
|
||||
// devices (awn/wunderground) produce different fields
|
||||
func handleProviderRequest(
|
||||
appCtx context.Context,
|
||||
func (aw *AmbientWeather) handleProviderRequest(
|
||||
p provider.AmbientProvider,
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
l := zerolog.Ctx(appCtx)
|
||||
tracer := otel.GetTracer(appCtx, p.Name()+".http.handler")
|
||||
l := zerolog.Ctx(aw.appCtx)
|
||||
tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler")
|
||||
|
||||
ctx, span := tracer.Start(r.Context(), p.Name()+".update")
|
||||
span.SetAttributes(attribute.String("provider", p.Name()))
|
||||
@ -76,6 +91,12 @@ func handleProviderRequest(
|
||||
// such as dew point and wind chill
|
||||
update.Enrich()
|
||||
|
||||
// Update metrics
|
||||
if aw.metrics == nil {
|
||||
aw.metrics = weather.MustInitMetrics(aw.appCtx)
|
||||
}
|
||||
aw.metrics.Update(update)
|
||||
|
||||
l.Debug().
|
||||
Str("provider", p.Name()).
|
||||
Any("update", update).
|
||||
|
Reference in New Issue
Block a user