Skip to content

Commit

Permalink
Move metrics to own module
Browse files Browse the repository at this point in the history
  • Loading branch information
GJSBRT committed Mar 16, 2024
1 parent 08dd1d9 commit 573bc3e
Show file tree
Hide file tree
Showing 7 changed files with 561 additions and 402 deletions.
58 changes: 58 additions & 0 deletions metrics/luna2000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package metrics

var luna2000Metrics = []Metric{
{
Namespace: "luna2000",
Name: "running_status",
Help: "The running status",
Fields: []string{
"inverter",
"battery",
},
},
{
Namespace: "luna2000",
Name: "charging_status",
Help: "The charging status",
Fields: []string{
"inverter",
"battery",
},
},
{
Namespace: "luna2000",
Name: "bus_voltage",
Help: "The bus voltage",
Fields: []string{
"inverter",
"battery",
},
},
{
Namespace: "luna2000",
Name: "battery_capacity",
Help: "The battery capacity",
Fields: []string{
"inverter",
"battery",
},
},
{
Namespace: "luna2000",
Name: "total_charge",
Help: "The total charge",
Fields: []string{
"inverter",
"battery",
},
},
{
Namespace: "luna2000",
Name: "total_discharge",
Help: "The total discharge",
Fields: []string{
"inverter",
"battery",
},
},
}
159 changes: 159 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

type MetricValue struct {
Fields map[string]string
Values []float64
}

type Metric struct {
Namespace string
Name string
Help string
Fields []string

Values []MetricValue
PrometheusGauge *prometheus.GaugeVec
}

var metrics = []Metric{}

func init() {
metrics = append(metrics, sun2000Metrics...)
metrics = append(metrics, luna2000Metrics...)
metrics = append(metrics, powerMeterMetrics...)

for index, metric := range metrics {
metric.Values = []MetricValue{}
metric.PrometheusGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metric.Namespace,
Name: metric.Name,
Help: metric.Help,
},
metric.Fields,
)

metrics[index] = metric
}
}

func AddMetric(namespace string, name string, help string, fields []string) *Metric {
metrics = append(metrics, Metric{
Namespace: namespace,
Name: name,
Help: help,
Fields: fields,
Values: []MetricValue{},
PrometheusGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: name,
Help: help,
}, fields),
})

return &metrics[len(metrics) - 1]
}

func GetMetric(namespace string, name string) *Metric {
for _, metric := range metrics {
if metric.Namespace == namespace && metric.Name == name {
return &metric
}
}

return nil
}

func GetMetricValueAverageLastMin(namespace string, name string, labels map[string]string, minutes uint) float64 {
var newMetric *Metric
for _, metric := range metrics {
if metric.Namespace == namespace && metric.Name == name {
newMetric = &metric
break
}
}

if newMetric == nil {
return 0
}

var matches *MetricValue
for _, metricValue := range newMetric.Values {
match := true
for key, value := range labels {
if metricValue.Fields[key] != value {
match = false
break
}
}

if !match {
continue
}

matches = &metricValue
}

if matches == nil {
return 0
}

var total float64
for _, value := range matches.Values {
total += value
}

return total / float64(len(matches.Values))
}

func SetMetricValue(namespace string, name string, labels map[string]string, value float64) {
var newMetric *Metric
var metricIndex int
for i, metric := range metrics {
if metric.Namespace == namespace && metric.Name == name {
newMetric = &metric
metricIndex = i
break
}
}

if newMetric == nil {
return
}

var matches *MetricValue
for _, metricValue := range newMetric.Values {
match := true
for key, value := range labels {
if metricValue.Fields[key] != value {
match = false
break
}
}

if !match {
continue
}

matches = &metricValue
}

if matches != nil {
matches.Values = append(matches.Values, value)
if len(matches.Values) > 5760 { // 1 days worth of data if we have a value every 15 seconds
matches.Values = matches.Values[1:]
}
} else {
newMetric.Values = append(newMetric.Values, MetricValue{
Fields: labels,
Values: []float64{value},
})
}

metrics[metricIndex] = *newMetric
newMetric.PrometheusGauge.With(labels).Set(value)
}
42 changes: 42 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package metrics

import (
"testing"
)

func TestGet(t *testing.T) {
AddMetric("tests", "get_test", "A test testing the get function", []string{
"inverter",
"string",
})

metric := GetMetric("tests", "get_test")
if metric == nil {
t.Fatalf("Metric not found")
}

if metric.Name != "get_test" {
t.Fatalf("Incorrect name")
}
}

func TestSet(t *testing.T) {
AddMetric("tests", "set_test", "A test testing the set function", []string{
"inverter",
"string",
})

SetMetricValue("tests", "set_test", map[string]string{
"inverter": "1",
"string": "1",
}, 1.0)

metric := GetMetric("tests", "set_test")
if metric == nil {
t.Fatalf("Metric not found")
}

if metric.Values[0].Value != 1.0 {

Check failure on line 39 in metrics/metrics_test.go

View workflow job for this annotation

GitHub Actions / build

metric.Values[0].Value undefined (type MetricValue has no field or method Value)
t.Fatalf("Incorrect value")
}
}
112 changes: 112 additions & 0 deletions metrics/power_meter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package metrics

var powerMeterMetrics = []Metric{
{
Namespace: "power_meter",
Name: "status",
Help: "The status of the power meter",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "phase_voltage",
Help: "The phase voltage",
Fields: []string{
"inverter",
"phase",
},
},
{
Namespace: "power_meter",
Name: "phase_current",
Help: "The phase current",
Fields: []string{
"inverter",
"phase",
},
},
{
Namespace: "power_meter",
Name: "active_power",
Help: "The active power",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "reactive_power",
Help: "The reactive power",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "power_factor",
Help: "The power factor",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "frequency",
Help: "The frequency",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "positive_active_electricity",
Help: "The positive active electricity",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "reverse_active_power",
Help: "The reverse active power",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "accumulated_reactive_power",
Help: "The accumulated reactive power",
Fields: []string{
"inverter",
},
},
{
Namespace: "power_meter",
Name: "line_voltage",
Help: "The line voltage",
Fields: []string{
"inverter",
"line",
},
},
{
Namespace: "power_meter",
Name: "phase_active_power",
Help: "The phase active power",
Fields: []string{
"inverter",
"phase",
},
},
{
Namespace: "power_meter",
Name: "model_result",
Help: "The power meter model result",
Fields: []string{
"inverter",
},
},
}
Loading

0 comments on commit 573bc3e

Please sign in to comment.