prom.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package telemetry
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "github.com/prometheus/client_golang/prometheus"
  7. "github.com/prometheus/client_golang/prometheus/promauto"
  8. "github.com/prometheus/client_golang/prometheus/promhttp"
  9. )
  10. var (
  11. // SecondsActive is the number of seconds, since program start
  12. // that the heating elemnent has been active.
  13. SecondsActive = promauto.NewCounter(prometheus.CounterOpts{
  14. Name: "heater_element_active_seconds",
  15. Help: "The total number of seconds the heating element has been active",
  16. })
  17. // RelayActivations counts the number of times the relay has activated
  18. RelayActivations = promauto.NewCounter(prometheus.CounterOpts{
  19. Name: "heater_element_activations_total",
  20. Help: "The number of times the relay has been activated",
  21. })
  22. // CurrentTemperature is the current measured temperature in the boiler
  23. CurrentTemperature = promauto.NewGauge(prometheus.GaugeOpts{
  24. Name: "boiler_water_temperature_celsius",
  25. Help: "The current temperature of the water in the boiler",
  26. })
  27. // SetpointTemperature is the desired temperature
  28. SetpointTemperature = promauto.NewGauge(prometheus.GaugeOpts{
  29. Name: "setpoint_temperature_celsius",
  30. Help: "The current setpoint temperature",
  31. })
  32. // SensorFaultCount is the desired temperature
  33. SensorFaultCount = promauto.NewCounter(prometheus.CounterOpts{
  34. Name: "sensor_fault_total",
  35. Help: "The number of times the sensor has read faulty data",
  36. })
  37. )
  38. func exitProgram(w http.ResponseWriter, r *http.Request) {
  39. os.Exit(0)
  40. }
  41. // PrometheusMetrics handles the prometheus scrape endpoint
  42. func PrometheusMetrics(prometheusPort int) {
  43. http.Handle("/metrics", promhttp.Handler())
  44. http.HandleFunc("/exit", exitProgram)
  45. http.ListenAndServe(fmt.Sprintf(":%d", prometheusPort), nil)
  46. }