Compare commits

...

5 Commits

Author SHA1 Message Date
3329c980a9 tweak spans
All checks were successful
Build and Publish / release (push) Successful in 3m23s
2025-03-05 13:23:04 -05:00
b4412a461f tweak spans
All checks were successful
Build and Publish / release (push) Successful in 3m22s
2025-03-05 13:18:37 -05:00
042fd221c4 move enrich span end to defer 2025-03-05 13:04:11 -05:00
29433cddd7 support sensor name mapping
All checks were successful
Build and Publish / release (push) Successful in 4m6s
2025-03-05 12:53:57 -05:00
f98a4cf348 Add support for temp+humidity sensors
All checks were successful
Build and Publish / release (push) Successful in 6m32s
2025-03-04 20:11:08 -05:00
12 changed files with 484 additions and 153 deletions

14
go.mod
View File

@ -22,12 +22,12 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_golang v1.21.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
@ -43,11 +43,11 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/net v0.36.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250212204824-5a70512c5d8b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b // indirect
google.golang.org/grpc v1.70.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
google.golang.org/grpc v1.71.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

15
go.sum
View File

@ -33,6 +33,7 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/schema v1.4.1 h1:jUg5hUjCSDZpNGLuXQOgIWGdlgrIdYvgQ0wZtdK1M3E=
@ -41,8 +42,12 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0 h1:VD1gqscl4nYs1YxVuSdemTrSgTK
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0/go.mod h1:4EgsQoS4TOhJizV+JTFg40qx1Ofh3XmXEQNBpgvNT40=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 h1:e9Rjr40Z98/clHv5Yg79Is0NtosR5LXRvdr7o/6NwbA=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1/go.mod h1:tIxuGz/9mpox++sgp9fJjHO0+q1X9/UOWd798aAm22M=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@ -63,6 +68,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_golang v1.21.1 h1:DOvXXTqVzvkIewV/CDPFdejpMCGeMcbGCQ8YOmu+Ibk=
github.com/prometheus/client_golang v1.21.1/go.mod h1:U9NM32ykUErtVBxdvD3zfi+EuFkkaBvMb09mIfe0Zgg=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
@ -110,6 +117,8 @@ golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -127,12 +136,18 @@ google.golang.org/genproto/googleapis/api v0.0.0-20250127172529-29210b9bc287 h1:
google.golang.org/genproto/googleapis/api v0.0.0-20250127172529-29210b9bc287/go.mod h1:iYONQfRdizDB8JJBybql13nArx91jcUk7zCXEsOofM4=
google.golang.org/genproto/googleapis/api v0.0.0-20250212204824-5a70512c5d8b h1:i+d0RZa8Hs2L/MuaOQYI+krthcxdEbEM2N+Tf3kJ4zk=
google.golang.org/genproto/googleapis/api v0.0.0-20250212204824-5a70512c5d8b/go.mod h1:iYONQfRdizDB8JJBybql13nArx91jcUk7zCXEsOofM4=
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb h1:p31xT4yrYrSM/G4Sn2+TNUkVhFCbG9y8itM2S6Th950=
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb/go.mod h1:jbe3Bkdp+Dh2IrslsFCklNhweNTBgSYanP1UXhJDhKg=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287 h1:J1H9f+LEdWAfHcez/4cvaVBox7cOYT+IU6rgqj5x++8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b h1:FQtJ1MxbXoIIrZHZ33M+w5+dAP9o86rgpjoKr/ZmT7k=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb h1:TLPQVbx1GJ8VKZxz52VAxl1EBgKXXbTiU9Fc5fZeLn4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ=
google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw=
google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=

View File

@ -13,6 +13,7 @@ import (
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/ambient/config"
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/provider"
@ -67,6 +68,11 @@ func (aw *AmbientWeather) GetWundergroundHandlerFunc(appCtx context.Context) fun
// 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
//
// This will call Update on metrics, and will also proxy
// requests to AWN/Wunderground if enabled
// This is the main work performed when a weather station or
// weather hub sends an update
func (aw *AmbientWeather) handleProviderRequest(
p provider.AmbientProvider,
w http.ResponseWriter,
@ -75,9 +81,9 @@ func (aw *AmbientWeather) handleProviderRequest(
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()))
defer span.End()
ctx, updateSpan := tracer.Start(r.Context(), p.Name()+".update")
updateSpan.SetAttributes(attribute.String("provider", p.Name()))
defer updateSpan.End()
l.Trace().Str("p", p.Name()).
Any("query", r.URL.Query()).Send()
@ -86,8 +92,8 @@ func (aw *AmbientWeather) handleProviderRequest(
update, err := p.ReqToWeather(ctx, r)
if err != nil {
l.Err(err).Send()
span.RecordError(err)
span.SetStatus(codes.Error,
updateSpan.RecordError(err)
updateSpan.SetStatus(codes.Error,
fmt.Sprintf("failed to handle %s update: %s",
p.Name(), err.Error()))
@ -96,20 +102,16 @@ func (aw *AmbientWeather) handleProviderRequest(
return
}
// Calculate any fields that may be missing
// such as dew point and wind chill
update.Enrich()
// Perform enrichment
aw.enrichUpdate(ctx, p, update)
// Prepare metrics if this is the first update
if aw.metrics == nil {
aw.InitMetrics()
// We may know which station this was for now
if update.StationConfig != nil {
updateSpan.SetAttributes(attribute.String("stationName", update.StationConfig.Name))
}
// Enrich station if configured
aw.enrichStation(update)
// Update metrics
aw.metrics.Update(update)
aw.metricsUpdate(ctx, p, update)
l.Debug().
Str("provider", p.Name()).
@ -120,44 +122,110 @@ func (aw *AmbientWeather) handleProviderRequest(
// Proxy update to one or both services if configured to do so
// Uses a weather update to allow awn to publish to wunderground and
// visa versa.
if station := update.StationConfig; station != nil {
// Perform proxy updates in parallel if enabled
var proxyWg sync.WaitGroup
if station.ProxyToAWN {
proxyWg.Add(1)
go func() {
defer proxyWg.Done()
err := aw.awnProvider.ProxyReq(ctx, update)
if err != nil {
zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
return
}
zerolog.Ctx(aw.appCtx).Debug().
Str("station", station.Name).
Msg("proxied weather update to awn")
}()
}
if station.ProxyToWunderground {
proxyWg.Add(1)
go func() {
defer proxyWg.Done()
err := aw.wuProvider.ProxyReq(ctx, update)
if err != nil {
zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
return
}
zerolog.Ctx(aw.appCtx).Debug().
Str("station", station.Name).
Msg("proxied weather update to wunderground")
}()
}
proxyWg.Wait()
if update.StationConfig != nil {
aw.proxyUpdate(ctx, p, update)
}
}
func (aw *AmbientWeather) enrichUpdate(
ctx context.Context,
p provider.AmbientProvider,
update *weather.WeatherUpdate,
) {
tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler")
// Calculate any fields that may be missing
// such as dew point and wind chill
_, enrichSpan := tracer.Start(ctx, p.Name()+".update.enrich")
defer enrichSpan.End()
// Metric enrichment
update.Enrich()
// Enrich station if configured
aw.enrichStation(update)
// Map sensor names
update.MapSensors()
enrichSpan.SetStatus(codes.Ok, "")
}
func (aw *AmbientWeather) metricsUpdate(
ctx context.Context,
p provider.AmbientProvider,
update *weather.WeatherUpdate,
) {
tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler")
_, metricsSpan := tracer.Start(ctx, p.Name()+".update.metrics")
if aw.metrics == nil {
aw.InitMetrics()
}
aw.metrics.Update(update)
metricsSpan.SetStatus(codes.Ok, "")
metricsSpan.End()
}
func (aw *AmbientWeather) proxyUpdate(
ctx context.Context,
p provider.AmbientProvider,
update *weather.WeatherUpdate,
) {
var proxyWg sync.WaitGroup
tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler")
station := update.StationConfig
_, proxySpan := tracer.Start(ctx, p.Name()+".update.proxy", trace.WithAttributes(
attribute.Bool("proxyToWunderground", station.ProxyToWunderground),
attribute.Bool("proxyToAWN", station.ProxyToAWN),
))
defer proxySpan.End()
// Perform proxy updates in parallel if enabled
if station.ProxyToAWN {
proxyWg.Add(1)
go func() {
defer proxyWg.Done()
defer proxySpan.AddEvent("proxied to ambient weather network")
err := aw.awnProvider.ProxyReq(ctx, update)
if err != nil {
zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
proxySpan.RecordError(err)
proxySpan.SetStatus(codes.Error, err.Error())
return
}
zerolog.Ctx(aw.appCtx).Debug().
Str("station", station.Name).
Msg("proxied weather update to awn")
}()
}
if station.ProxyToWunderground {
proxyWg.Add(1)
go func() {
defer proxyWg.Done()
defer proxySpan.AddEvent("proxied to wunderground")
err := aw.wuProvider.ProxyReq(ctx, update)
if err != nil {
zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
proxySpan.RecordError(err)
proxySpan.SetStatus(codes.Error, err.Error())
return
}
zerolog.Ctx(aw.appCtx).Debug().
Str("station", station.Name).
Msg("proxied weather update to wunderground")
}()
}
proxyWg.Wait()
}
func (aw *AmbientWeather) InitMetrics() {
if aw.config.MetricPrefix != "" {
weather.MetricPrefix = aw.config.MetricPrefix

View File

@ -33,4 +33,14 @@ type WeatherStation struct {
// Check weather.WeatherUpdateField for options
KeepMetrics []string `yaml:"keepMetrics"`
DropMetrics []string `yaml:"dropMetrics"`
// Relabels battery and sensor names
// Temp+Humidity Sensors:
// - TempHumiditySensor[1-8]
// Batteries:
// - IndoorSensor
// - OutdoorSensor
// - RainSensor
// - CO2Sensor
SensorMappings map[string]string `yaml:"sensorMappings"`
}

View File

@ -0,0 +1,12 @@
package config
// If the weather-station has a mapping, returns the new
// name for the sensor
func (ws *WeatherStation) MapSensor(sensor string) string {
for name, replacement := range ws.SensorMappings {
if name == sensor && replacement != "" {
return replacement
}
}
return sensor
}

View File

@ -0,0 +1,67 @@
package config
import "testing"
func TestWeatherStation_MapSensor(t *testing.T) {
type fields struct {
Name string
Equipment string
WundergroundID string
WundergroundPassword string
AWNPassKey string
ProxyToAWN bool
ProxyToWunderground bool
KeepMetrics []string
DropMetrics []string
SensorMappings map[string]string
}
type args struct {
sensor string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "Check sensor mapping",
fields: fields{
SensorMappings: map[string]string{
"TempHumiditySensor1": "TestSensor",
},
},
args: args{sensor: "TempHumiditySensor1"},
want: "TestSensor",
},
{
name: "Check sensor no mapping",
fields: fields{
SensorMappings: map[string]string{
"TempHumiditySensor1": "TestSensor",
},
},
args: args{sensor: "TempHumiditySensor2"},
want: "TempHumiditySensor2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ws := &WeatherStation{
Name: tt.fields.Name,
Equipment: tt.fields.Equipment,
WundergroundID: tt.fields.WundergroundID,
WundergroundPassword: tt.fields.WundergroundPassword,
AWNPassKey: tt.fields.AWNPassKey,
ProxyToAWN: tt.fields.ProxyToAWN,
ProxyToWunderground: tt.fields.ProxyToWunderground,
KeepMetrics: tt.fields.KeepMetrics,
DropMetrics: tt.fields.DropMetrics,
SensorMappings: tt.fields.SensorMappings,
}
if got := ws.MapSensor(tt.args.sensor); got != tt.want {
t.Errorf("WeatherStation.MapSensor() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -24,6 +24,7 @@ const (
BattIndoorSensor = "IndoorSensor"
BattRainSensor = "RainSensor"
BattCO2Sensor = "CO2Sensor"
THSensor = "TempHumiditySensor"
)
func (awn *AWNProvider) Name() string {
@ -89,11 +90,54 @@ func MapAwnUpdate(awnUpdate *AmbientWeatherUpdate) *weather.WeatherUpdate {
Component: BattCO2Sensor,
Status: awnUpdate.BattCO2,
},
// Temp and Humidity Sensors
{
Component: THSensor + "1",
Status: awnUpdate.Batt1,
},
{
Component: THSensor + "2",
Status: awnUpdate.Batt2,
},
{
Component: THSensor + "3",
Status: awnUpdate.Batt3,
},
{
Component: THSensor + "4",
Status: awnUpdate.Batt4,
},
{
Component: THSensor + "5",
Status: awnUpdate.Batt5,
},
{
Component: THSensor + "6",
Status: awnUpdate.Batt6,
},
{
Component: THSensor + "7",
Status: awnUpdate.Batt7,
},
{
Component: THSensor + "8",
Status: awnUpdate.Batt8,
},
},
TempIndoorF: awnUpdate.TempInF,
HumidityIndoor: awnUpdate.HumidityIn,
BaromRelativeIn: awnUpdate.BaromRelIn,
BaromAbsoluteIn: awnUpdate.BaromAbsIn,
TempHumiditySensors: []*weather.TempHumiditySensor{
{Name: THSensor + "1", TempF: awnUpdate.Temp1F, Humidity: awnUpdate.Humidity1},
{Name: THSensor + "2", TempF: awnUpdate.Temp2F, Humidity: awnUpdate.Humidity2},
{Name: THSensor + "3", TempF: awnUpdate.Temp3F, Humidity: awnUpdate.Humidity3},
{Name: THSensor + "4", TempF: awnUpdate.Temp4F, Humidity: awnUpdate.Humidity4},
{Name: THSensor + "5", TempF: awnUpdate.Temp5F, Humidity: awnUpdate.Humidity5},
{Name: THSensor + "6", TempF: awnUpdate.Temp6F, Humidity: awnUpdate.Humidity6},
{Name: THSensor + "7", TempF: awnUpdate.Temp7F, Humidity: awnUpdate.Humidity7},
{Name: THSensor + "8", TempF: awnUpdate.Temp8F, Humidity: awnUpdate.Humidity8},
},
}
}

View File

@ -28,4 +28,32 @@ type AmbientWeatherUpdate struct {
BaromAbsIn *float64 `json:"baromabsin,omitempty" schema:"baromabsin"`
BattIn *int `json:"battin,omitempty" schema:"battin"`
BattCO2 *int `json:"batt_co2,omitempty" schema:"batt_co2"`
*AmbientTempHumiditySensors
}
type AmbientTempHumiditySensors struct {
Temp1F *float64 `json:"temp1f,omitempty" schema:"temp1f"`
Temp2F *float64 `json:"temp2f,omitempty" schema:"temp2f"`
Temp3F *float64 `json:"temp3f,omitempty" schema:"temp3f"`
Temp4F *float64 `json:"temp4f,omitempty" schema:"temp4f"`
Temp5F *float64 `json:"temp5f,omitempty" schema:"temp5f"`
Temp6F *float64 `json:"temp6f,omitempty" schema:"temp6f"`
Temp7F *float64 `json:"temp7f,omitempty" schema:"temp7f"`
Temp8F *float64 `json:"temp8f,omitempty" schema:"temp8f"`
Humidity1 *int `json:"humidity1,omitempty" schema:"humidity1"`
Humidity2 *int `json:"humidity2,omitempty" schema:"humidity2"`
Humidity3 *int `json:"humidity3,omitempty" schema:"humidity3"`
Humidity4 *int `json:"humidity4,omitempty" schema:"humidity4"`
Humidity5 *int `json:"humidity5,omitempty" schema:"humidity5"`
Humidity6 *int `json:"humidity6,omitempty" schema:"humidity6"`
Humidity7 *int `json:"humidity7,omitempty" schema:"humidity7"`
Humidity8 *int `json:"humidity8,omitempty" schema:"humidity8"`
Batt1 *int `json:"batt1,omitempty" schema:"batt1"`
Batt2 *int `json:"batt2,omitempty" schema:"batt2"`
Batt3 *int `json:"batt3,omitempty" schema:"batt3"`
Batt4 *int `json:"batt4,omitempty" schema:"batt4"`
Batt5 *int `json:"batt5,omitempty" schema:"batt5"`
Batt6 *int `json:"batt6,omitempty" schema:"batt6"`
Batt7 *int `json:"batt7,omitempty" schema:"batt7"`
Batt8 *int `json:"batt8,omitempty" schema:"batt8"`
}

View File

@ -10,6 +10,7 @@ import (
// Attempts to complete missing fields that may not
// be set by a specific provider, such as DewPoint and WindChill
// TODO: Add span
func (u *WeatherUpdate) Enrich(weatherStations ...*config.WeatherStation) {
if u == nil {
return
@ -49,6 +50,24 @@ func (u *WeatherUpdate) Enrich(weatherStations ...*config.WeatherStation) {
}
}
// Swaps sensor and component names based on
// user provided configuration
func (u *WeatherUpdate) MapSensors() {
if u.StationConfig == nil {
return
}
// Map sensor battery components
for i, batt := range u.Batteries {
u.Batteries[i].Component = u.StationConfig.MapSensor(batt.Component)
}
// Map other sensors
for _, th := range u.TempHumiditySensors {
th.Name = u.StationConfig.MapSensor(th.Name)
}
}
func CalculateDewPoint(tempF, humidity float64) float64 {
// Convert temperature from Fahrenheit to Celsius
tempC := (tempF - 32) * 5 / 9

View File

@ -4,13 +4,13 @@ import (
"context"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)
var MetricPrefix = "weather"
type WeatherMetrics struct {
// Weather Metrics
TempOutdoorF metric.Float64Gauge
@ -37,6 +37,10 @@ type WeatherMetrics struct {
DewPointF metric.Float64Gauge
WindChillF metric.Float64Gauge
// Temp and Humidity Sensors
SensorTempF metric.Float64Gauge
SensorHumidity metric.Int64Gauge
// Internal Telemetry
UpdatesReceived metric.Int64Counter
appCtx context.Context
@ -45,92 +49,8 @@ type WeatherMetrics struct {
recorder *MetricRecorder
}
var MetricPrefix = "weather"
func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
wm := &WeatherMetrics{
appCtx: appCtx,
cfg: config.MustFromCtx(appCtx),
recorder: &MetricRecorder{ctx: appCtx, l: zerolog.Ctx(appCtx)},
}
wm.meter = otel.GetMeter(appCtx, "weather", "metrics")
// Weather Metrics
wm.TempOutdoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_outdoor_f",
metric.WithDescription("Outdoor Temperature in Faherenheit"))
wm.TempIndoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_indoor_f",
metric.WithDescription("Indoor Temperature in Faherenheit"))
wm.HumidityOudoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_outdoor",
metric.WithDescription("Outdoor Humidity %"))
wm.HumidityIndoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_indoor",
metric.WithDescription("Indoor Humidity %"))
wm.WindSpeedMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_speed_mph",
metric.WithDescription("Wind Speed in MPH"))
wm.WindGustMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_gust_mph",
metric.WithDescription("Wind Gust in MPH"))
wm.MaxDailyGust, _ = wm.meter.Float64Gauge(MetricPrefix+"_max_daily_gust",
metric.WithDescription("Max Daily Wind Gust"))
wm.WindDir, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir",
metric.WithDescription("Wind Direction in Degrees"))
wm.WindDirAvg10m, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir_avg_10m",
metric.WithDescription("Wind Direction 10m Average"))
wm.UV, _ = wm.meter.Int64Gauge(MetricPrefix+"_uv",
metric.WithDescription("UV Index"))
wm.SolarRadiation, _ = wm.meter.Float64Gauge(MetricPrefix+"_solar_radiation",
metric.WithDescription("Solar Radiation in W/㎡"))
wm.HourlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_hourly_rain_in",
metric.WithDescription("Hourly Rain in Inches"))
wm.EventRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_event_rain_in",
metric.WithDescription("Event Rain in Inches"))
wm.DailyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_daily_rain_in",
metric.WithDescription("Daily Rain in Inches"))
wm.WeeklyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_weekly_rain_in",
metric.WithDescription("Weekly Rain in Inches"))
wm.MonthlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_monthly_rain_in",
metric.WithDescription("Monthly Rain in Inches"))
wm.YearlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_yearly_rain_in",
metric.WithDescription("Yearly Rain in Inches"))
wm.TotalRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_total_rain_in",
metric.WithDescription("Total Rain in Inches"))
wm.BatteryStatus, _ = wm.meter.Int64Gauge(MetricPrefix+"_battery_status",
metric.WithDescription("Per-component battery status"))
wm.BaromRelativeIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_relative_in",
metric.WithDescription("Relative Pressure in Inches of Mercury"))
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_absolute_in",
metric.WithDescription("Absolute Pressure in Inches of Mercury"))
wm.DewPointF, _ = wm.meter.Float64Gauge(MetricPrefix+"_dew_point_f",
metric.WithDescription("Dew Point in Faherenheit"))
wm.WindChillF, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_chill_f",
metric.WithDescription("Wind Chill in Faherenheit"))
// Internal Telemetry
wm.UpdatesReceived, _ = wm.meter.Int64Counter(MetricPrefix+"_updates_received",
metric.WithDescription("Metric Updates Processed by Exporter"))
return wm
}
func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
attributes := []attribute.KeyValue{
semconv.ServiceVersion(wm.cfg.Version),
semconv.ServiceName(wm.cfg.Name),
semconv.DeploymentEnvironment(wm.cfg.Environment),
}
if u.StationType != nil {
attributes = append(attributes,
attribute.String("station_type", *u.StationType))
}
if u.StationConfig != nil {
if u.StationConfig.Name != "" {
attributes = append(attributes,
attribute.String("station_name", u.StationConfig.Name))
}
if u.StationConfig.Equipment != "" {
attributes = append(attributes,
attribute.String("station_equipment", u.StationConfig.Equipment))
}
}
attributes := wm.GetAttributes(u)
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempOutdoorF, FloatVal: u.TempOutdoorF, Field: FieldTempOutdoorF, Attributes: attributes, Station: u.StationConfig})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempIndoorF, FloatVal: u.TempIndoorF, Field: FieldTempIndoorF, Attributes: attributes, Station: u.StationConfig})
@ -155,13 +75,73 @@ func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DewPointF, FloatVal: u.DewPointF, Field: FieldDewPointF, Attributes: attributes, Station: u.StationConfig})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindChillF, FloatVal: u.WindChillF, Field: FieldWindChillF, Attributes: attributes, Station: u.StationConfig})
// Batteries
for _, battery := range u.Batteries {
batAttr := attributes
batAttr = append(batAttr, attribute.String("component", battery.Component))
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.BatteryStatus, IntVal: battery.Status, Field: FieldBatteries, Attributes: batAttr, Station: u.StationConfig})
}
wm.RecordBatteries(u, attributes)
wm.RecordTempHumiditySensors(u, attributes)
wm.UpdatesReceived.Add(wm.appCtx, 1)
}
func (wm *WeatherMetrics) RecordBatteries(u *WeatherUpdate, attr []attribute.KeyValue) {
for _, battery := range u.Batteries {
batAttr := attr
batAttr = append(batAttr, attribute.String("component", battery.Component))
wm.recorder.Record(&RecordOpts{
Int64Gauge: wm.BatteryStatus,
IntVal: battery.Status,
Field: FieldBatteries,
Attributes: batAttr,
Station: u.StationConfig,
})
}
}
func (wm *WeatherMetrics) RecordTempHumiditySensors(u *WeatherUpdate, attr []attribute.KeyValue) {
if u == nil || u.TempHumiditySensors == nil {
return
}
for _, sensor := range u.TempHumiditySensors {
sensorAttr := attr
sensorAttr = append(sensorAttr, attribute.String("sensorName", sensor.Name))
wm.recorder.Record(&RecordOpts{
Float64Gauge: wm.SensorTempF,
FloatVal: sensor.TempF,
Field: FieldSensorTempF,
Attributes: sensorAttr,
Station: u.StationConfig,
})
wm.recorder.Record(&RecordOpts{
Int64Gauge: wm.SensorHumidity,
IntVal: sensor.Humidity,
Field: FieldSensorHumidity,
Attributes: sensorAttr,
Station: u.StationConfig,
})
}
}
func (wm *WeatherMetrics) GetAttributes(u *WeatherUpdate) []attribute.KeyValue {
attributes := []attribute.KeyValue{
semconv.ServiceVersion(wm.cfg.Version),
semconv.ServiceName(wm.cfg.Name),
semconv.DeploymentEnvironment(wm.cfg.Environment),
}
if u.StationType != nil {
attributes = append(attributes,
attribute.String("station_type", *u.StationType))
}
if u.StationConfig != nil {
if u.StationConfig.Name != "" {
attributes = append(attributes,
attribute.String("station_name", u.StationConfig.Name))
}
if u.StationConfig.Equipment != "" {
attributes = append(attributes,
attribute.String("station_equipment", u.StationConfig.Equipment))
}
}
return attributes
}

View File

@ -0,0 +1,80 @@
package weather
import (
"context"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/metric"
)
func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
wm := &WeatherMetrics{
appCtx: appCtx,
cfg: config.MustFromCtx(appCtx),
recorder: &MetricRecorder{ctx: appCtx, l: zerolog.Ctx(appCtx)},
}
wm.meter = otel.GetMeter(appCtx, "weather", "metrics")
// Weather Metrics
wm.TempOutdoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_outdoor_f",
metric.WithDescription("Outdoor Temperature in Faherenheit"))
wm.TempIndoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_indoor_f",
metric.WithDescription("Indoor Temperature in Faherenheit"))
wm.HumidityOudoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_outdoor",
metric.WithDescription("Outdoor Humidity %"))
wm.HumidityIndoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_indoor",
metric.WithDescription("Indoor Humidity %"))
wm.WindSpeedMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_speed_mph",
metric.WithDescription("Wind Speed in MPH"))
wm.WindGustMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_gust_mph",
metric.WithDescription("Wind Gust in MPH"))
wm.MaxDailyGust, _ = wm.meter.Float64Gauge(MetricPrefix+"_max_daily_gust",
metric.WithDescription("Max Daily Wind Gust"))
wm.WindDir, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir",
metric.WithDescription("Wind Direction in Degrees"))
wm.WindDirAvg10m, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir_avg_10m",
metric.WithDescription("Wind Direction 10m Average"))
wm.UV, _ = wm.meter.Int64Gauge(MetricPrefix+"_uv",
metric.WithDescription("UV Index"))
wm.SolarRadiation, _ = wm.meter.Float64Gauge(MetricPrefix+"_solar_radiation",
metric.WithDescription("Solar Radiation in W/㎡"))
wm.HourlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_hourly_rain_in",
metric.WithDescription("Hourly Rain in Inches"))
wm.EventRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_event_rain_in",
metric.WithDescription("Event Rain in Inches"))
wm.DailyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_daily_rain_in",
metric.WithDescription("Daily Rain in Inches"))
wm.WeeklyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_weekly_rain_in",
metric.WithDescription("Weekly Rain in Inches"))
wm.MonthlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_monthly_rain_in",
metric.WithDescription("Monthly Rain in Inches"))
wm.YearlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_yearly_rain_in",
metric.WithDescription("Yearly Rain in Inches"))
wm.TotalRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_total_rain_in",
metric.WithDescription("Total Rain in Inches"))
wm.BatteryStatus, _ = wm.meter.Int64Gauge(MetricPrefix+"_battery_status",
metric.WithDescription("Per-component battery status"))
wm.BaromRelativeIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_relative_in",
metric.WithDescription("Relative Pressure in Inches of Mercury"))
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_absolute_in",
metric.WithDescription("Absolute Pressure in Inches of Mercury"))
wm.DewPointF, _ = wm.meter.Float64Gauge(MetricPrefix+"_dew_point_f",
metric.WithDescription("Dew Point in Faherenheit"))
wm.WindChillF, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_chill_f",
metric.WithDescription("Wind Chill in Faherenheit"))
// Temp and Humidity Sensors
wm.SensorTempF, _ = wm.meter.Float64Gauge(MetricPrefix+"_sensor_temp_f",
metric.WithDescription("Temperature Sensor in Faherenheit"))
wm.SensorHumidity, _ = wm.meter.Int64Gauge(MetricPrefix+"_sensor_humidity",
metric.WithDescription("Humidity % Sensor"))
// Internal Telemetry
wm.UpdatesReceived, _ = wm.meter.Int64Counter(MetricPrefix+"_updates_received",
metric.WithDescription("Metric Updates Processed by Exporter"))
return wm
}

View File

@ -38,8 +38,14 @@ type WeatherUpdate struct {
// if not otherwise set
DewPointF *float64
WindChillF *float64
// First URL parameters given to AWN/Wunderground
// if proxying is enabled
// Extra Temp+Humidity Sensors
TempHumiditySensors []*TempHumiditySensor
}
type TempHumiditySensor struct {
Name string
TempF *float64
Humidity *int
}
type BatteryStatus struct {
@ -76,6 +82,8 @@ const (
FieldBaromAbsoluteIn = "BaromAbsoluteIn"
FieldDewPointF = "DewPointF"
FieldWindChillF = "WindChillF"
FieldSensorTempF = "SensorTempF"
FieldSensorHumidity = "SensorHumidity"
)
func (u *WeatherUpdate) GetStationName() string {