add: record sensors in metrics
This commit is contained in:
@@ -14,11 +14,58 @@ import (
|
||||
pb "github.com/chathaway-codes/home-sensors/v2/gen"
|
||||
internalpb "github.com/chathaway-codes/home-sensors/v2/gen/token"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
temperatureValues = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Name: "temperature_values_c",
|
||||
Help: "Temperature sensor values in celsius",
|
||||
Buckets: func() []float64 {
|
||||
var buckets []float64
|
||||
for i := -150.0; i < 150; i++ {
|
||||
buckets = append(buckets, i)
|
||||
}
|
||||
return buckets
|
||||
}(),
|
||||
}, []string{
|
||||
"home",
|
||||
"camera",
|
||||
})
|
||||
humidityValues = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Name: "humidity_values_prh",
|
||||
Help: "Temperature sensor values in %rH",
|
||||
Buckets: func() []float64 {
|
||||
var buckets []float64
|
||||
for i := 0.0; i < 100; i++ {
|
||||
buckets = append(buckets, i)
|
||||
}
|
||||
return buckets
|
||||
}(),
|
||||
}, []string{
|
||||
"home",
|
||||
"camera",
|
||||
})
|
||||
pressureValues = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Name: "pressure_values_hpa",
|
||||
Help: "Temperature sensor values in hPa",
|
||||
Buckets: func() []float64 {
|
||||
var buckets []float64
|
||||
for i := 900.0; i < 1500; i++ {
|
||||
buckets = append(buckets, i)
|
||||
}
|
||||
return buckets
|
||||
}(),
|
||||
}, []string{
|
||||
"home",
|
||||
"camera",
|
||||
})
|
||||
)
|
||||
|
||||
type camera struct {
|
||||
id string
|
||||
}
|
||||
@@ -50,6 +97,7 @@ func New() *Server {
|
||||
samplesByCamera: make(map[string]map[pb.Sample_Type]*pb.Sample),
|
||||
}
|
||||
go s.cleanup()
|
||||
go s.logSensorValues()
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -303,6 +351,35 @@ func (s *Server) ListSamples(ctx context.Context, request *connect.Request[pb.Li
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (s *Server) logSensorValues() {
|
||||
ticker := time.NewTicker(time.Second * 10)
|
||||
for range ticker.C {
|
||||
func() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
for home := range s.camerasByHome {
|
||||
for camera := range s.camerasByHome[home] {
|
||||
samples, ok := s.samplesByCamera[camera]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, sample := range samples {
|
||||
switch sample.Type {
|
||||
case pb.Sample_TEMPERATURE_C:
|
||||
temperatureValues.WithLabelValues(home, camera).Observe(sample.GetReading())
|
||||
case pb.Sample_PRESSURE:
|
||||
pressureValues.WithLabelValues(home, camera).Observe(sample.GetReading())
|
||||
case pb.Sample_HUMIDITY:
|
||||
humidityValues.WithLabelValues(home, camera).Observe(sample.GetReading())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) cleanup() {
|
||||
ticker := time.NewTicker(time.Minute * 5)
|
||||
for t := range ticker.C {
|
||||
|
||||
Reference in New Issue
Block a user