add counter support, add lightning time
This commit is contained in:
		@@ -40,6 +40,7 @@ type WeatherMetrics struct {
 | 
			
		||||
	// Lightning Sensor
 | 
			
		||||
	LightningCountDay     metric.Int64Gauge
 | 
			
		||||
	LightningLastDistance metric.Int64Gauge
 | 
			
		||||
	LightningLastTime     metric.Int64Gauge
 | 
			
		||||
 | 
			
		||||
	// Temp and Humidity Sensors
 | 
			
		||||
	SensorTempF    metric.Float64Gauge
 | 
			
		||||
@@ -79,9 +80,9 @@ 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})
 | 
			
		||||
 | 
			
		||||
	lightningAttributes := append(attributes, attribute.String("unit", "km"))
 | 
			
		||||
	wm.recorder.Record(&RecordOpts{Int64Gauge: wm.LightningLastDistance, IntVal: u.LightningDistance, Field: FieldLightningDistance, Attributes: lightningAttributes, Station: u.StationConfig})
 | 
			
		||||
	wm.recorder.Record(&RecordOpts{Int64Gauge: wm.LightningLastDistance, IntVal: u.LightningDistance, Field: FieldLightningDistance, Attributes: attributes, Station: u.StationConfig})
 | 
			
		||||
	wm.recorder.Record(&RecordOpts{Int64Gauge: wm.LightningCountDay, IntVal: u.LightningDay, Field: FieldLightningDay, Attributes: attributes, Station: u.StationConfig})
 | 
			
		||||
	wm.recorder.Record(&RecordOpts{Int64Gauge: wm.LightningLastTime, IntVal: u.LightningTime, Field: FieldLightningLastTime, Attributes: attributes, Station: u.StationConfig})
 | 
			
		||||
 | 
			
		||||
	wm.RecordBatteries(u, attributes)
 | 
			
		||||
	wm.RecordTempHumiditySensors(u, attributes)
 | 
			
		||||
 
 | 
			
		||||
@@ -70,7 +70,11 @@ func MustInitMetrics(appCtx context.Context) *WeatherMetrics {
 | 
			
		||||
	wm.LightningCountDay, _ = wm.meter.Int64Gauge(MetricPrefix+"_lightning_day",
 | 
			
		||||
		metric.WithDescription("Count of lightning strikes for current day"))
 | 
			
		||||
	wm.LightningLastDistance, _ = wm.meter.Int64Gauge(MetricPrefix+"_lightning_last_distance",
 | 
			
		||||
		metric.WithDescription("Last measured lightning distance"))
 | 
			
		||||
		metric.WithDescription("Last measured lightning distance"),
 | 
			
		||||
		metric.WithUnit("kph"))
 | 
			
		||||
	wm.LightningLastTime, _ = wm.meter.Int64Gauge(MetricPrefix+"_lightning_last_time",
 | 
			
		||||
		metric.WithDescription("Last lightning time (epoch)"),
 | 
			
		||||
		metric.WithUnit("s"))
 | 
			
		||||
 | 
			
		||||
	// Temp and Humidity Sensors
 | 
			
		||||
	wm.SensorTempF, _ = wm.meter.Float64Gauge(MetricPrefix+"_sensor_temp_f",
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,7 @@ type MetricRecorder struct {
 | 
			
		||||
type RecordOpts struct {
 | 
			
		||||
	Float64Gauge metric.Float64Gauge
 | 
			
		||||
	Int64Gauge   metric.Int64Gauge
 | 
			
		||||
	Int64Counter metric.Int64Counter
 | 
			
		||||
	IntVal       *int
 | 
			
		||||
	FloatVal     *float64
 | 
			
		||||
	Attributes   []attribute.KeyValue
 | 
			
		||||
@@ -59,6 +60,16 @@ func (r *MetricRecorder) Record(opts *RecordOpts) {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		r.recordFloat(opts.Float64Gauge, *opts.FloatVal, opts.Attributes...)
 | 
			
		||||
	} else if opts.Int64Counter != nil {
 | 
			
		||||
		if opts.IntVal == nil {
 | 
			
		||||
			log := r.l.Trace().Str("field", string(opts.Field))
 | 
			
		||||
			if opts.Station != nil {
 | 
			
		||||
				log = log.Str("station", opts.Station.Name)
 | 
			
		||||
			}
 | 
			
		||||
			log.Msg("Dropping nil float metric")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		r.recordCounter(opts.Int64Counter, *opts.IntVal, opts.Attributes...)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -71,6 +82,19 @@ func (o *RecordOpts) keep() bool {
 | 
			
		||||
	return !slices.Contains(o.Station.DropMetrics, o.Field)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *MetricRecorder) recordCounter(
 | 
			
		||||
	m metric.Int64Counter, value int, attributes ...attribute.KeyValue,
 | 
			
		||||
) {
 | 
			
		||||
	// Prepare metric attributes
 | 
			
		||||
	options := make([]metric.AddOption, 0, len(attributes))
 | 
			
		||||
	if len(attributes) > 0 {
 | 
			
		||||
		options = append(options, metric.WithAttributes(attributes...))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	val := int64(value)
 | 
			
		||||
	m.Add(r.ctx, val, options...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *MetricRecorder) recordInt(
 | 
			
		||||
	m metric.Int64Gauge, value int, attributes ...attribute.KeyValue,
 | 
			
		||||
) {
 | 
			
		||||
 
 | 
			
		||||
@@ -90,6 +90,7 @@ const (
 | 
			
		||||
	FieldSensorHumidity    = "SensorHumidity"
 | 
			
		||||
	FieldLightningDay      = "LightningDay"
 | 
			
		||||
	FieldLightningDistance = "LightningDistance"
 | 
			
		||||
	FieldLightningLastTime = "LightningLastTime"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (u *WeatherUpdate) GetStationName() string {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user