Skip to content

Commit

Permalink
update neutron queries
Browse files Browse the repository at this point in the history
  • Loading branch information
cloucs committed Nov 7, 2024
1 parent 329bf2d commit c344a38
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
58 changes: 47 additions & 11 deletions exporters/neutron.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type NeutronUsageExporter struct {
db *sql.DB
floatingIPs *prometheus.Desc
routers *prometheus.Desc
routers *prometheus.Desc
}

func NewNeutronUsageExporter(db *sql.DB) (*NeutronUsageExporter, error) {
Expand Down Expand Up @@ -39,23 +39,63 @@ func (e *NeutronUsageExporter) Collect(ch chan<- prometheus.Metric) {
}

func (e *NeutronUsageExporter) collectMetrics(ch chan<- prometheus.Metric) {
rows, err := e.db.Query("SELECT f.project_id, COUNT(f.id) AS total_fips, COALESCE(r.total_routers, 0) AS total_routers FROM floatingips f LEFT JOIN (SELECT project_id, COUNT(id) AS total_routers FROM routers GROUP BY project_id) r ON f.project_id = r.project_id GROUP BY f.project_id")

floatingIPsCounts := make(map[string]float64)
rows, err := e.db.Query("SELECT project_id, COUNT(id) AS total_fips FROM floatingips GROUP BY project_id")
if err != nil {
log.Println("Error querying Neutron database:", err)
log.Println("Error querying floating IP counts:", err)
return
}

defer rows.Close()

for rows.Next() {
var projectID string
var totalFloatingIPs float64
if err := rows.Scan(&projectID, &totalFloatingIPs); err != nil {
log.Println("Error scanning floating IP row:", err)
continue
}
floatingIPsCounts[projectID] = totalFloatingIPs
}

if err := rows.Err(); err != nil {
log.Println("Error in floating IPs result set:", err)
return
}

routerCounts := make(map[string]float64)
rows, err = e.db.Query("SELECT project_id, COUNT(id) AS total_routers FROM routers GROUP BY project_id")
if err != nil {
log.Println("Error querying router counts:", err)
return
}
defer rows.Close()

for rows.Next() {
var projectID string
var totalRouters float64
if err := rows.Scan(&projectID, &totalFloatingIPs, &totalRouters); err != nil {
log.Println("Error scanning Neutron row:", err)
if err := rows.Scan(&projectID, &totalRouters); err != nil {
log.Println("Error scanning router row:", err)
continue
}
routerCounts[projectID] = totalRouters
}

if err := rows.Err(); err != nil {
log.Println("Error in routers result set:", err)
return
}

projectIDs := make(map[string]bool)
for projectID := range floatingIPsCounts {
projectIDs[projectID] = true
}
for projectID := range routerCounts {
projectIDs[projectID] = true
}

for projectID := range projectIDs {
totalFloatingIPs := floatingIPsCounts[projectID]
totalRouters := routerCounts[projectID]

ch <- prometheus.MustNewConstMetric(
e.floatingIPs,
Expand All @@ -71,8 +111,4 @@ func (e *NeutronUsageExporter) collectMetrics(ch chan<- prometheus.Metric) {
projectID,
)
}

if err := rows.Err(); err != nil {
log.Println("Error in Neutron result set:", err)
}
}
15 changes: 10 additions & 5 deletions exporters/neutron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ func TestNeutronUsageExporter(t *testing.T) {
}
defer db.Close()

rows := sqlmock.NewRows([]string{"project_id", "floatingIPs", "routers"}).
AddRow("c352b0ed-30ca-4634-9c2d-1947efc29096", 2, 1).
AddRow("6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096", 3, 2)
mock.ExpectQuery("SELECT project_id, COUNT").WillReturnRows(rows)
floatingIPRows := sqlmock.NewRows([]string{"project_id", "total_fips"}).
AddRow("c352b0ed-30ca-4634-9c2d-1947efc29096", 2).
AddRow("6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096", 3)
mock.ExpectQuery("SELECT project_id, COUNT\\(id\\) AS total_fips FROM floatingips GROUP BY project_id").WillReturnRows(floatingIPRows)

routerRows := sqlmock.NewRows([]string{"project_id", "total_routers"}).
AddRow("c352b0ed-30ca-4634-9c2d-1947efc29096", 1).
AddRow("6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096", 2)
mock.ExpectQuery("SELECT project_id, COUNT\\(id\\) AS total_routers FROM routers GROUP BY project_id").WillReturnRows(routerRows)

exporter, err := NewNeutronUsageExporter(db)
if err != nil {
t.Fatalf("Failed to create NewNeutronUsageExporter: %v", err)
t.Fatalf("Failed to create NeutronUsageExporter: %v", err)
}

expectedMetrics := `
Expand Down

0 comments on commit c344a38

Please sign in to comment.