Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Manila Usage Exporter #4

Merged
merged 4 commits into from
Nov 20, 2024
Merged
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
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ jobs:
context: .
push: true
tags: ghcr.io/scaleup-technologies/openstack-usage-exporter:${{ env.TAG_VERSION }}

permissions:
contents: write
packages: write
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [v0.4.0] - 2024-11-14

### Added

- Added support for Manila usage metrics
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Configuration is done via enviroment variables.
Exporters can be enabled or disabled:

```shell
NOVA_ENABLED=true # defaults to true
CINDER_ENABLED=false
# Default values
NOVA_ENABLED=true
CINDER_ENABLED=true
DESIGNATE_ENABLED=true
MANILA_ENABLED=false
NEUTRON_ENABLED=true
OCTAVIA_ENABLED=true
```
135 changes: 135 additions & 0 deletions exporters/manila.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package exporters

import (
"database/sql"
"log"

"github.com/prometheus/client_golang/prometheus"
)

type ManilaUsageExporter struct {
db *sql.DB
sharesSize *prometheus.Desc
shareSnapshotsSize *prometheus.Desc
shareBackupsSize *prometheus.Desc
}

func NewManilaUsageExporter(db *sql.DB) (*ManilaUsageExporter, error) {
return &ManilaUsageExporter{
db: db,
sharesSize: prometheus.NewDesc(
"openstack_project_shares_size_gb",
"Total share size in GB per OpenStack project",
[]string{"project_id"}, nil,
),
shareSnapshotsSize: prometheus.NewDesc(
"openstack_project_share_snapshots_size_gb",
"Total share snapshot size in GB per OpenStack project",
[]string{"project_id"}, nil,
),
shareBackupsSize: prometheus.NewDesc(
"openstack_project_share_backups_size_gb",
"Total share backup size in GB per OpenStack project",
[]string{"project_id"}, nil,
),
}, nil
}

func (e *ManilaUsageExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.sharesSize
ch <- e.shareSnapshotsSize
ch <- e.shareBackupsSize
}

func (e *ManilaUsageExporter) Collect(ch chan<- prometheus.Metric) {
e.collectShareSize(ch)
e.collectShareSnapshotSize(ch)
e.collectShareBackupSize(ch)
}

func (e *ManilaUsageExporter) collectShareSize(ch chan<- prometheus.Metric) {
rows, err := e.db.Query("SELECT project_id, SUM(size) AS shares_size FROM shares WHERE deleted='False' GROUP BY project_id")
if err != nil {
log.Println("Error querying Manila database:", err)
return
}
defer rows.Close()

for rows.Next() {
var projectID string
var sharesSize float64
if err := rows.Scan(&projectID, &sharesSize); err != nil {
log.Println("Error scanning Manila row:", err)
continue
}

ch <- prometheus.MustNewConstMetric(
e.sharesSize,
prometheus.GaugeValue,
sharesSize,
projectID,
)

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

func (e *ManilaUsageExporter) collectShareSnapshotSize(ch chan<- prometheus.Metric) {
rows, err := e.db.Query("SELECT project_id, SUM(size) AS share_snapshots_size FROM share_snapshots WHERE deleted='False' GROUP BY project_id")
if err != nil {
log.Println("Error querying Manila database:", err)
return
}
defer rows.Close()

for rows.Next() {
var projectID string
var shareSnapshotsSize float64
if err := rows.Scan(&projectID, &shareSnapshotsSize); err != nil {
log.Println("Error scanning Manila row:", err)
continue
}

ch <- prometheus.MustNewConstMetric(
e.shareSnapshotsSize,
prometheus.GaugeValue,
shareSnapshotsSize,
projectID,
)

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

func (e *ManilaUsageExporter) collectShareBackupSize(ch chan<- prometheus.Metric) {
rows, err := e.db.Query("SELECT project_id, SUM(size) AS share_backups_size FROM share_backups WHERE deleted='False' GROUP BY project_id")
if err != nil {
log.Println("Error querying Manila database:", err)
return
}
defer rows.Close()

for rows.Next() {
var projectID string
var shareBackupsSize float64
if err := rows.Scan(&projectID, &shareBackupsSize); err != nil {
log.Println("Error scanning Manila row:", err)
continue
}

ch <- prometheus.MustNewConstMetric(
e.shareBackupsSize,
prometheus.GaugeValue,
shareBackupsSize,
projectID,
)

}
if err := rows.Err(); err != nil {
log.Println("Error in Manila result set:", err)
}
}
63 changes: 63 additions & 0 deletions exporters/manila_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package exporters

import (
"strings"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/prometheus/client_golang/prometheus/testutil"
)

func TestManilaUsageExporter(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Failed to create sqlmock: %v", err)
}
defer db.Close()

sharesRows := sqlmock.NewRows([]string{
"project_id", "shares_size"}).
AddRow("6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096", 2).
AddRow("c352b0ed-30ca-4634-9c2d-1947efc29096", 12)
mock.ExpectQuery("SELECT project_id, SUM\\(size\\) AS shares_size FROM shares").WillReturnRows(sharesRows)

shareSnapshotsRows := sqlmock.NewRows([]string{
"project_id", "shares_size"}).
AddRow("6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096", 4).
AddRow("c352b0ed-30ca-4634-9c2d-1947efc29096", 24)
mock.ExpectQuery("SELECT project_id, SUM\\(size\\) AS share_snapshots_size FROM share_snapshots").WillReturnRows(shareSnapshotsRows)

shareBackupsRows := sqlmock.NewRows([]string{
"project_id", "shares_size"}).
AddRow("6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096", 1).
AddRow("c352b0ed-30ca-4634-9c2d-1947efc29096", 0)
mock.ExpectQuery("SELECT project_id, SUM\\(size\\) AS share_backups_size FROM share_backups").WillReturnRows(shareBackupsRows)

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

expectedMetrics := `
# HELP openstack_project_share_backups_size_gb Total share backup size in GB per OpenStack project
# TYPE openstack_project_share_backups_size_gb gauge
openstack_project_share_backups_size_gb{project_id="6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096"} 1
openstack_project_share_backups_size_gb{project_id="c352b0ed-30ca-4634-9c2d-1947efc29096"} 0
# HELP openstack_project_share_snapshots_size_gb Total share snapshot size in GB per OpenStack project
# TYPE openstack_project_share_snapshots_size_gb gauge
openstack_project_share_snapshots_size_gb{project_id="6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096"} 4
openstack_project_share_snapshots_size_gb{project_id="c352b0ed-30ca-4634-9c2d-1947efc29096"} 24
# HELP openstack_project_shares_size_gb Total share size in GB per OpenStack project
# TYPE openstack_project_shares_size_gb gauge
openstack_project_shares_size_gb{project_id="6ee08ba2-2ca1-4c91-b139-4bf0dbaa4096"} 2
openstack_project_shares_size_gb{project_id="c352b0ed-30ca-4634-9c2d-1947efc29096"} 12
`

if err := testutil.CollectAndCompare(exporter, strings.NewReader(expectedMetrics)); err != nil {
t.Errorf("unexpected collecting result:\n%s", err)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("There were unfulfilled expectations: %s", err)
}
}
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func main() {
}

enabledExporters := map[string]bool{
"cinder": GetBoolEnv("CINDER_ENABLED", true),
"nova": GetBoolEnv("NOVA_ENABLED", true),
"neutron": GetBoolEnv("NEUTRON_ENABLED", true),
"designate": GetBoolEnv("DESIGNATE_ENABLED", true),
"octavia": GetBoolEnv("OCTAVIA_ENABLED", true),
"cinder": GetBoolEnv("CINDER_ENABLED", true),
"nova": GetBoolEnv("NOVA_ENABLED", true),
"neutron": GetBoolEnv("NEUTRON_ENABLED", true),
"designate": GetBoolEnv("DESIGNATE_ENABLED", true),
"octavia": GetBoolEnv("OCTAVIA_ENABLED", true),
"manila": GetBoolEnv("MANILA_ENABLED", false),
}

for name, enabled := range enabledExporters {
Expand Down Expand Up @@ -66,6 +67,8 @@ func main() {
exporter, err = exporters.NewDesignateUsageExporter(db)
case "octavia":
exporter, err = exporters.NewOctaviaUsageExporter(db)
case "manila":
exporter, err = exporters.NewManilaUsageExporter(db)
default:
log.Fatalf("unknown exporter type: %s", name)
}
Expand Down