ambient-local-exporter/pkg/weather/metrics.go

166 lines
9.2 KiB
Go
Raw Normal View History

2025-01-04 22:51:55 +00:00
package weather
2025-01-05 01:54:36 +00:00
import (
"context"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
2025-01-09 02:28:38 +00:00
"github.com/rs/zerolog"
2025-01-05 01:54:36 +00:00
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)
type WeatherMetrics struct {
// Weather Metrics
2025-01-07 15:27:02 +00:00
TempOutdoorF metric.Float64Gauge
TempIndoorF metric.Float64Gauge
HumidityOudoor metric.Int64Gauge
HumidityIndoor metric.Int64Gauge
WindSpeedMPH metric.Float64Gauge
WindGustMPH metric.Float64Gauge
MaxDailyGust metric.Float64Gauge
WindDir metric.Int64Gauge
WindDirAvg10m metric.Int64Gauge
UV metric.Int64Gauge
SolarRadiation metric.Float64Gauge
HourlyRainIn metric.Float64Gauge
EventRainIn metric.Float64Gauge
DailyRainIn metric.Float64Gauge
WeeklyRainIn metric.Float64Gauge
MonthlyRainIn metric.Float64Gauge
YearlyRainIn metric.Float64Gauge
TotalRainIn metric.Float64Gauge
BatteryStatus metric.Int64Gauge
BaromRelativeIn metric.Float64Gauge
BaromAbsoluteIn metric.Float64Gauge
DewPointF metric.Float64Gauge
WindChillF metric.Float64Gauge
2025-01-05 01:54:36 +00:00
// Internal Telemetry
UpdatesReceived metric.Int64Counter
appCtx context.Context
cfg *config.AppConfig
meter metric.Meter
2025-01-09 02:28:38 +00:00
recorder *MetricRecorder
2025-01-05 01:54:36 +00:00
}
2025-01-07 20:08:14 +00:00
var MetricPrefix = "weather"
2025-01-05 01:54:36 +00:00
func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
wm := &WeatherMetrics{
2025-01-09 02:28:38 +00:00
appCtx: appCtx,
cfg: config.MustFromCtx(appCtx),
recorder: &MetricRecorder{ctx: appCtx, l: zerolog.Ctx(appCtx)},
2025-01-05 01:54:36 +00:00
}
wm.meter = otel.GetMeter(appCtx, "weather", "metrics")
// Weather Metrics
2025-01-07 20:08:14 +00:00
wm.TempOutdoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_outdoor_f",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Outdoor Temperature in Faherenheit"))
2025-01-07 20:08:14 +00:00
wm.TempIndoorF, _ = wm.meter.Float64Gauge(MetricPrefix+"_temp_indoor_f",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Indoor Temperature in Faherenheit"))
2025-01-07 20:08:14 +00:00
wm.HumidityOudoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_outdoor",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Outdoor Humidity %"))
2025-01-07 20:08:14 +00:00
wm.HumidityIndoor, _ = wm.meter.Int64Gauge(MetricPrefix+"_humidity_indoor",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Indoor Humidity %"))
2025-01-07 20:08:14 +00:00
wm.WindSpeedMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_speed_mph",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Wind Speed in MPH"))
2025-01-07 20:08:14 +00:00
wm.WindGustMPH, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_gust_mph",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Wind Gust in MPH"))
2025-01-07 20:08:14 +00:00
wm.MaxDailyGust, _ = wm.meter.Float64Gauge(MetricPrefix+"_max_daily_gust",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Max Daily Wind Gust"))
2025-01-07 20:08:14 +00:00
wm.WindDir, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Wind Direction in Degrees"))
2025-01-07 20:08:14 +00:00
wm.WindDirAvg10m, _ = wm.meter.Int64Gauge(MetricPrefix+"_wind_dir_avg_10m",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Wind Direction 10m Average"))
2025-01-07 20:08:14 +00:00
wm.UV, _ = wm.meter.Int64Gauge(MetricPrefix+"_uv",
2025-01-05 01:54:36 +00:00
metric.WithDescription("UV Index"))
2025-01-07 20:08:14 +00:00
wm.SolarRadiation, _ = wm.meter.Float64Gauge(MetricPrefix+"_solar_radiation",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Solar Radiation in W/㎡"))
2025-01-07 20:08:14 +00:00
wm.HourlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_hourly_rain_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Hourly Rain in Inches"))
2025-01-07 20:08:14 +00:00
wm.EventRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_event_rain_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Event Rain in Inches"))
2025-01-07 20:08:14 +00:00
wm.DailyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_daily_rain_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Daily Rain in Inches"))
2025-01-07 20:08:14 +00:00
wm.WeeklyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_weekly_rain_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Weekly Rain in Inches"))
2025-01-07 20:08:14 +00:00
wm.MonthlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_monthly_rain_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Monthly Rain in Inches"))
2025-01-07 20:08:14 +00:00
wm.YearlyRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_yearly_rain_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Yearly Rain in Inches"))
2025-01-07 20:08:14 +00:00
wm.TotalRainIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_total_rain_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Total Rain in Inches"))
2025-01-07 20:08:14 +00:00
wm.BatteryStatus, _ = wm.meter.Int64Gauge(MetricPrefix+"_battery_status",
2025-01-07 15:27:02 +00:00
metric.WithDescription("Per-component battery status"))
2025-01-07 20:08:14 +00:00
wm.BaromRelativeIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_relative_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Relative Pressure in Inches of Mercury"))
2025-01-07 20:08:14 +00:00
wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge(MetricPrefix+"_barometric_pressure_absolute_in",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Absolute Pressure in Inches of Mercury"))
2025-01-07 20:08:14 +00:00
wm.DewPointF, _ = wm.meter.Float64Gauge(MetricPrefix+"_dew_point_f",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Dew Point in Faherenheit"))
2025-01-07 20:08:14 +00:00
wm.WindChillF, _ = wm.meter.Float64Gauge(MetricPrefix+"_wind_chill_f",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Wind Chill in Faherenheit"))
// Internal Telemetry
2025-01-07 20:08:14 +00:00
wm.UpdatesReceived, _ = wm.meter.Int64Counter(MetricPrefix+"_updates_received",
2025-01-05 01:54:36 +00:00
metric.WithDescription("Metric Updates Processed by Exporter"))
return wm
}
func (wm *WeatherMetrics) Update(u *WeatherUpdate) {
2025-01-09 02:28:38 +00:00
attributes := []attribute.KeyValue{
2025-01-05 01:54:36 +00:00
semconv.ServiceVersion(wm.cfg.Version),
2025-01-09 02:28:38 +00:00
}
if u.StationType != nil {
attributes = append(attributes,
attribute.String("station_type", *u.StationType))
}
if u.StationInfo != nil {
if u.StationInfo.Name != nil {
attributes = append(attributes,
attribute.String("station_name", *u.StationInfo.Name))
}
if u.StationInfo.Equipment != nil {
attributes = append(attributes,
attribute.String("station_equipment", *u.StationInfo.Equipment))
}
}
2025-01-05 01:54:36 +00:00
2025-01-09 02:28:38 +00:00
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempOutdoorF, FloatVal: u.TempOutdoorF, Field: FieldTempOutdoorF, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TempIndoorF, FloatVal: u.TempIndoorF, Field: FieldTempIndoorF, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.HumidityOudoor, IntVal: u.HumidityOudoor, Field: FieldHumidityOudoor, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.HumidityIndoor, IntVal: u.HumidityIndoor, Field: FieldHumidityIndoor, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindSpeedMPH, FloatVal: u.WindSpeedMPH, Field: FieldWindSpeedMPH, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindGustMPH, FloatVal: u.WindGustMPH, Field: FieldWindGustMPH, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.MaxDailyGust, FloatVal: u.MaxDailyGust, Field: FieldMaxDailyGust, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.WindDir, IntVal: u.WindDir, Field: FieldWindDir, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.WindDirAvg10m, IntVal: u.WindDirAvg10m, Field: FieldWindDirAvg10m, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.UV, IntVal: u.UV, Field: FieldUV, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.SolarRadiation, FloatVal: u.SolarRadiation, Field: FieldSolarRadiation, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.HourlyRainIn, FloatVal: u.HourlyRainIn, Field: FieldHourlyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.EventRainIn, FloatVal: u.EventRainIn, Field: FieldEventRainIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DailyRainIn, FloatVal: u.DailyRainIn, Field: FieldDailyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WeeklyRainIn, FloatVal: u.WeeklyRainIn, Field: FieldWeeklyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.MonthlyRainIn, FloatVal: u.MonthlyRainIn, Field: FieldMonthlyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.YearlyRainIn, FloatVal: u.YearlyRainIn, Field: FieldYearlyRainIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.TotalRainIn, FloatVal: u.TotalRainIn, Field: FieldTotalRainIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.BaromRelativeIn, FloatVal: u.BaromRelativeIn, Field: FieldBaromRelativeIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.BaromAbsoluteIn, FloatVal: u.BaromAbsoluteIn, Field: FieldBaromAbsoluteIn, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.DewPointF, FloatVal: u.DewPointF, Field: FieldDewPointF, Attributes: attributes, StationInfo: u.StationInfo})
wm.recorder.Record(&RecordOpts{Float64Gauge: wm.WindChillF, FloatVal: u.WindChillF, Field: FieldWindChillF, Attributes: attributes, StationInfo: u.StationInfo})
2025-01-05 01:54:36 +00:00
2025-01-07 15:27:02 +00:00
// Batteries
for _, battery := range u.Batteries {
2025-01-09 02:28:38 +00:00
batAttr := attributes
batAttr = append(batAttr, attribute.String("component", battery.Component))
wm.recorder.Record(&RecordOpts{Int64Gauge: wm.BatteryStatus, IntVal: battery.Status, Field: FieldBatteries, Attributes: batAttr, StationInfo: u.StationInfo})
2025-01-07 15:27:02 +00:00
}
2025-01-05 01:54:36 +00:00
wm.UpdatesReceived.Add(wm.appCtx, 1)
}