Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/prometheus/procfs v0.16.1 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/crypto v0.43.0 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/oauth2 v0.32.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBseWJUpBw5I82+2U4M=
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y=
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/net v0.45.0 h1:RLBg5JKixCy82FtLJpeNlVM0nrSqpCRYzVU1n8kj0tM=
Expand Down
106 changes: 106 additions & 0 deletions internal/collector/neutron/floating_ips.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package neutron

import (
"context"
"database/sql"
"log/slog"

"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
neutrondb "github.com/vexxhost/openstack_database_exporter/internal/db/neutron"
)

var (
floatingIPDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, Subsystem, "floating_ip"),
"floating_ip",
[]string{
"floating_ip_address",
"floating_network_id",
"id",
"project_id",
"router_id",
"status",
},
nil,
)
floatingIPsDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, Subsystem, "floating_ips"),
"floating_ips",
nil,
nil,
)

floatingsIPsAssociatedNotActive = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, Subsystem, "floating_ips_associated_not_active"),
"floating_ips_associated_not_active",
nil,
nil,
)
)

type FloatingIPCollector struct {
db *sql.DB
queries *neutrondb.Queries
logger *slog.Logger
}

func NewFloatingIPCollector(db *sql.DB, logger *slog.Logger) *FloatingIPCollector {
return &FloatingIPCollector{
db: db,
queries: neutrondb.New(db),
logger: logger.With(
"namespace", Namespace,
"subsystem", Subsystem,
"collector", "floating_ips",
),
}
}

func (c *FloatingIPCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- floatingIPDesc
ch <- floatingIPsDesc
ch <- floatingsIPsAssociatedNotActive
}

func (c *FloatingIPCollector) Collect(ch chan<- prometheus.Metric) error {
ctx := context.Background()

anaFips := 0

fips, err := c.queries.GetFloatingIPs(ctx)
if err != nil {
c.logger.Error("failed to query", "error", err)
return err
}
ch <- prometheus.MustNewConstMetric(
floatingIPsDesc,
prometheus.GaugeValue,
float64(len(fips)),
)

for _, fip := range fips {
if fip.FixedIpAddress.Valid && fip.Status.String != "ACTIVE" {
anaFips += 1
}

ch <- prometheus.MustNewConstMetric(
floatingIPDesc,
prometheus.GaugeValue,
cast.ToFloat64(1),
fip.FloatingIpAddress,
fip.FloatingNetworkID,
fip.ID,
fip.ProjectID.String,
fip.RouterID.String,
fip.Status.String,
)
}

ch <- prometheus.MustNewConstMetric(
floatingsIPsAssociatedNotActive,
prometheus.GaugeValue,
float64(anaFips),
)
return nil
}
66 changes: 66 additions & 0 deletions internal/collector/neutron/floating_ips_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package neutron

import (
"database/sql"
"log/slog"
"regexp"
"testing"

"github.com/prometheus/client_golang/prometheus"

"github.com/DATA-DOG/go-sqlmock"
neutrondb "github.com/vexxhost/openstack_database_exporter/internal/db/neutron"
"github.com/vexxhost/openstack_database_exporter/internal/testutil"
)

func TestFloatingIPCollector(t *testing.T) {
tests := []testutil.CollectorTestCase{
{
Name: "successful collection of floating ips",
SetupMock: func(mock sqlmock.Sqlmock) {
rows := sqlmock.NewRows([]string{
"id",
"floating_ip_address",
"floating_network_id",
"project_id",
"router_id",
"status",
"fixed_ip_address",
}).AddRow(
"8110ce2e-a287-4ea3-8273-65459bde329f",
"172.24.4.100",
"23fa64ed-bab5-4180-bc9f-7928c4562d73",
"02e67ccf829b44438e1c0397b2444a0f",
"bd221f59-33c9-4d67-8f9c-ccaea76b05c5",
"ACTIVE",
"",
)
mock.ExpectQuery(regexp.QuoteMeta(neutrondb.GetFloatingIPs)).WillReturnRows(rows)
},
ExpectedMetrics: `# HELP openstack_neutron_floating_ip floating_ip
# TYPE openstack_neutron_floating_ip gauge
openstack_neutron_floating_ip{floating_ip_address="172.24.4.100",floating_network_id="23fa64ed-bab5-4180-bc9f-7928c4562d73",id="8110ce2e-a287-4ea3-8273-65459bde329f",project_id="02e67ccf829b44438e1c0397b2444a0f",router_id="bd221f59-33c9-4d67-8f9c-ccaea76b05c5",status="ACTIVE"} 1
# HELP openstack_neutron_floating_ips floating_ips
# TYPE openstack_neutron_floating_ips gauge
openstack_neutron_floating_ips 1
# HELP openstack_neutron_floating_ips_associated_not_active floating_ips_associated_not_active
# TYPE openstack_neutron_floating_ips_associated_not_active gauge
openstack_neutron_floating_ips_associated_not_active 0
`,
},
}

testutil.RunCollectorTests(t, tests, func(db *sql.DB, logger *slog.Logger) prometheus.Collector {
return &testFloatingIPCollector{NewFloatingIPCollector(db, logger)}
})
}

type testFloatingIPCollector struct {
*FloatingIPCollector
}

func (t *testFloatingIPCollector) Collect(ch chan<- prometheus.Metric) {
if err := t.FloatingIPCollector.Collect(ch); err != nil {
panic("unexpected error: " + err.Error())
}
}
Loading