Skip to content
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ go build ./cmd/openstack-database-exporter

Integration tests load embedded fixtures from the pinned openstackdb module.
Database URLs and exporter configuration remain the same.

## Designate

Set `DESIGNATE_DATABASE_URL` to enable the five Designate metrics. Set
`DESIGNATE_DISABLE_RECORDSETS_STATUS=true` or pass
`--designate.disable-recordsets-status` to skip the per-recordset status metric
and its SQL query while retaining the other four metrics.
11 changes: 11 additions & 0 deletions cmd/openstack-database-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ var (
"cinder.database-url",
"Cinder database connection URL (oslo.db format)",
).Envar("CINDER_DATABASE_URL").String()
designateDatabaseURL = kingpin.Flag(
"designate.database-url",
"Designate database connection URL (oslo.db format)",
).Envar("DESIGNATE_DATABASE_URL").String()
designateDisableRecordsetsStatus = kingpin.Flag(
"designate.disable-recordsets-status",
"Disable per-recordset Designate status metrics and their database query.",
).Envar("DESIGNATE_DISABLE_RECORDSETS_STATUS").Bool()
glanceDatabaseURL = kingpin.Flag(
"glance.database-url",
"Glance database connection URL (oslo.db format)",
Expand Down Expand Up @@ -92,6 +100,7 @@ func main() {

reg := collector.NewRegistry(collector.Config{
CinderDatabaseURL: *cinderDatabaseURL,
DesignateDatabaseURL: *designateDatabaseURL,
GlanceDatabaseURL: *glanceDatabaseURL,
HeatDatabaseURL: *heatDatabaseURL,
IronicDatabaseURL: *ironicDatabaseURL,
Expand All @@ -104,6 +113,8 @@ func main() {
NovaDatabaseURL: *novaDatabaseURL,
NovaAPIDatabaseURL: *novaAPIDatabaseURL,
ProjectCacheTTL: *projectCacheTTL,

DesignateDisableRecordsetsStatus: *designateDisableRecordsetsStatus,
}, logger)

http.Handle(*metricsPath, promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
Expand Down
55 changes: 52 additions & 3 deletions cmd/openstack-database-exporter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
itest "github.com/vexxhost/openstack_database_exporter/internal/testutil"
)

// TestIntegration_E2E_FullExporter starts MySQL containers for all 11 services,
// TestIntegration_E2E_FullExporter starts MySQL containers for configured services,
// seeds them with representative data, wires up the full exporter registry,
// serves /metrics via an httptest server, and validates the Prometheus
// exposition output.
Expand All @@ -37,6 +37,7 @@ func TestIntegration_E2E_FullExporter(t *testing.T) {
sqlDir+"/cinder/schema.sql",
sqlDir+"/cinder/indexes.sql",
)
designateRes := itest.NewMySQLContainerWithURL(t, "designate", sqlDir+"/designate/schema.sql")
glanceRes := itest.NewMySQLContainerWithURL(t, "glance", sqlDir+"/glance/schema.sql")
keystoneRes := itest.NewMySQLContainerWithURL(t, "keystone", sqlDir+"/keystone/schema.sql")
magnumRes := itest.NewMySQLContainerWithURL(t, "magnum", sqlDir+"/magnum/schema.sql")
Expand All @@ -57,7 +58,7 @@ func TestIntegration_E2E_FullExporter(t *testing.T) {
)
heatRes := itest.NewMySQLContainerWithURL(t, "heat", sqlDir+"/heat/schema.sql")

t.Log("All 11 MariaDB containers are up")
t.Log("All MariaDB containers are up")

// ── 2. Seed data ─────────────────────────────────────────────────────────

Expand Down Expand Up @@ -232,13 +233,21 @@ func TestIntegration_E2E_FullExporter(t *testing.T) {
(3, 'proj-001', 'user-001', 'ram', 6144, 0)`,
)

t.Log("All 11 services seeded")
// Designate: one zone with a pending recordset.
itest.SeedSQL(t, designateRes.DB,
`INSERT INTO zones (id, name, tenant_id, status, type) VALUES ('a86dba5800434cc6a1bb69d5e86f3ca3', 'example.org.', 'proj-001', 'ACTIVE', 'PRIMARY')`,
`INSERT INTO recordsets (id, zone_id, name, type) VALUES ('f7b10e9b0cae4a91b162562bc6096648', 'a86dba5800434cc6a1bb69d5e86f3ca3', 'example.org.', 'A')`,
`INSERT INTO records (id, recordset_id, status) VALUES ('0d6ed29ef23e4c30bd85531ef3db0574', 'f7b10e9b0cae4a91b162562bc6096648', 'PENDING')`,
)

t.Log("All services seeded")

// ── 3. Wire up the full exporter ─────────────────────────────────────────
logger := promslog.New(&promslog.Config{})

cfg := collector.Config{
CinderDatabaseURL: cinderRes.URL,
DesignateDatabaseURL: designateRes.URL,
GlanceDatabaseURL: glanceRes.URL,
KeystoneDatabaseURL: keystoneRes.URL,
MagnumDatabaseURL: magnumRes.URL,
Expand Down Expand Up @@ -288,6 +297,7 @@ func TestIntegration_E2E_FullExporter(t *testing.T) {
// Every service must emit its _up metric = 1
upMetrics := []string{
"openstack_cinder_up",
"openstack_designate_up",
"openstack_glance_up",
"openstack_identity_up",
"openstack_container_infra_up",
Expand All @@ -312,6 +322,12 @@ func TestIntegration_E2E_FullExporter(t *testing.T) {
assertMetricExists(t, families, "openstack_cinder_limits_volume_max_gb")
assertMetricExists(t, families, "openstack_cinder_limits_volume_used_gb")

// Designate
assertGaugeValue(t, families, "openstack_designate_zones", 1)
assertGaugeValue(t, families, "openstack_designate_zone_status", 1)
assertGaugeValue(t, families, "openstack_designate_recordsets", 1)
assertGaugeValue(t, families, "openstack_designate_recordsets_status", 0)

// Glance
assertGaugeValue(t, families, "openstack_glance_images", 2)
assertMetricExists(t, families, "openstack_glance_image_bytes")
Expand Down Expand Up @@ -404,6 +420,39 @@ func TestIntegration_E2E_FullExporter(t *testing.T) {
if elapsed > 10*time.Second {
t.Errorf("scrape took %v, expected < 10s", elapsed)
}

t.Run("Designate recordset status disabled", func(t *testing.T) {
designateReg := collector.NewRegistry(collector.Config{
DesignateDatabaseURL: designateRes.URL,
DesignateDisableRecordsetsStatus: true,
}, logger)
designateServer := httptest.NewServer(promhttp.HandlerFor(designateReg, promhttp.HandlerOpts{}))
defer designateServer.Close()

families := scrapeAndParse(t, designateServer.URL)
assertGaugeValue(t, families, "openstack_designate_up", 1)
assertGaugeValue(t, families, "openstack_designate_zones", 1)
assertGaugeValue(t, families, "openstack_designate_zone_status", 1)
assertGaugeValue(t, families, "openstack_designate_recordsets", 1)
if _, present := families["openstack_designate_recordsets_status"]; present {
t.Error("recordset status metric present with status collection disabled")
}
})

t.Run("Designate database outage", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := designateRes.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate Designate database: %v", err)
}

families := scrapeAndParse(t, ts.URL)
assertGaugeValue(t, families, "openstack_designate_up", 0)
assertGaugeValue(t, families, "openstack_glance_up", 1)
if _, present := families["openstack_designate_zones"]; present {
t.Error("Designate zone metrics present after database outage")
}
})
}

// TestIntegration_E2E_PartialConfig tests that the exporter works when only
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/stretchr/testify v1.12.1
github.com/testcontainers/testcontainers-go v0.45.0
github.com/testcontainers/testcontainers-go/modules/mariadb v0.45.0
github.com/vexxhost/openstackdb v0.2.1
github.com/vexxhost/openstackdb v0.3.0
go4.org/netipx v0.0.0-20260823151212-3075585bcbeb
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+F
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4=
github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg=
github.com/vexxhost/openstackdb v0.2.1 h1:HHHQ/Z7d3q6a0rSxQRl/8qfQbuKCFrLFMvkO7TnL33Y=
github.com/vexxhost/openstackdb v0.2.1/go.mod h1:pyeNaJXXGVH9biXEeJyDvA1u6ARgH+ThPbqT0tNzR9c=
github.com/vexxhost/openstackdb v0.3.0 h1:sYh3qJgPa8wOObuCHnH6/CVQ7qbMUUVzbrz9z9rQs1E=
github.com/vexxhost/openstackdb v0.3.0/go.mod h1:TLd6ymsdfJHj9svEYbbDGS1Oj7q7ChjKrGAkxsMX7Nk=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
Expand Down
5 changes: 5 additions & 0 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus"

"github.com/vexxhost/openstack_database_exporter/internal/collector/cinder"
"github.com/vexxhost/openstack_database_exporter/internal/collector/designate"
"github.com/vexxhost/openstack_database_exporter/internal/collector/glance"
"github.com/vexxhost/openstack_database_exporter/internal/collector/heat"
"github.com/vexxhost/openstack_database_exporter/internal/collector/ironic"
Expand All @@ -28,6 +29,7 @@ const (

type Config struct {
CinderDatabaseURL string
DesignateDatabaseURL string
GlanceDatabaseURL string
HeatDatabaseURL string
IronicDatabaseURL string
Expand All @@ -40,6 +42,8 @@ type Config struct {
NovaDatabaseURL string
NovaAPIDatabaseURL string
ProjectCacheTTL time.Duration

DesignateDisableRecordsetsStatus bool
}

func NewRegistry(cfg Config, logger *slog.Logger) *prometheus.Registry {
Expand All @@ -60,6 +64,7 @@ func NewRegistry(cfg Config, logger *slog.Logger) *prometheus.Registry {
projectResolver := project.NewResolver(logger, keystoneQueries, cfg.ProjectCacheTTL)

cinder.RegisterCollectors(reg, cfg.CinderDatabaseURL, projectResolver, logger)
designate.RegisterCollectors(reg, cfg.DesignateDatabaseURL, cfg.DesignateDisableRecordsetsStatus, logger)
glance.RegisterCollectors(reg, cfg.GlanceDatabaseURL, logger)
heat.RegisterCollectors(reg, cfg.HeatDatabaseURL, logger)
ironic.RegisterCollectors(reg, cfg.IronicDatabaseURL, logger)
Expand Down
31 changes: 31 additions & 0 deletions internal/collector/designate/designate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package designate

import (
"log/slog"

"github.com/prometheus/client_golang/prometheus"
"github.com/vexxhost/openstack_database_exporter/internal/util"
"github.com/vexxhost/openstackdb"
)

const (
Namespace = "openstack"
Subsystem = "designate"
)

func RegisterCollectors(registry *prometheus.Registry, databaseURL string, disableRecordsetsStatus bool, logger *slog.Logger) {
if databaseURL == "" {
logger.Info("Collector not loaded", "service", "designate", "reason", "database URL not configured")
return
}

conn, err := openstackdb.Connect(databaseURL)
if err != nil {
logger.Error("Failed to connect to database", "service", "designate", "error", err)
registry.MustRegister(util.NewDownCollector(Namespace, Subsystem))
return
}

registry.MustRegister(NewZonesCollector(conn, disableRecordsetsStatus, logger))
logger.Info("Registered collectors", "service", "designate")
}
113 changes: 113 additions & 0 deletions internal/collector/designate/zones.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package designate

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

"github.com/prometheus/client_golang/prometheus"
"github.com/vexxhost/openstack_database_exporter/internal/util"
designatedb "github.com/vexxhost/openstackdb/designate/db"
)

var (
upDesc = prometheus.NewDesc(prometheus.BuildFQName(Namespace, Subsystem, "up"), "up", nil, nil)
zonesDesc = prometheus.NewDesc(prometheus.BuildFQName(Namespace, Subsystem, "zones"), "zones", nil, nil)
zoneStatusDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, Subsystem, "zone_status"), "zone_status",
[]string{"id", "name", "status", "tenant_id", "type"}, nil,
)
recordsetsDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, Subsystem, "recordsets"), "recordsets",
[]string{"zone_id", "zone_name", "tenant_id"}, nil,
)
recordsetsStatusDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, Subsystem, "recordsets_status"), "recordsets_status",
[]string{"id", "name", "status", "zone_id", "zone_name", "type"}, nil,
)
statuses = []string{"PENDING", "ACTIVE", "DELETED", "ERROR"}
)

type ZonesCollector struct {
db *sql.DB
disableRecordsetsStatus bool
logger *slog.Logger
}

func NewZonesCollector(db *sql.DB, disableRecordsetsStatus bool, logger *slog.Logger) *ZonesCollector {
return &ZonesCollector{
db: db,
disableRecordsetsStatus: disableRecordsetsStatus,
logger: logger.With("namespace", Namespace, "subsystem", Subsystem, "collector", "zones"),
}
}

func (c *ZonesCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upDesc
ch <- zonesDesc
ch <- zoneStatusDesc
ch <- recordsetsDesc
if !c.disableRecordsetsStatus {
ch <- recordsetsStatusDesc
}
}

func (c *ZonesCollector) Collect(ch chan<- prometheus.Metric) {
if err := c.collectMetrics(context.Background(), ch); err != nil {
c.logger.Error("failed to query Designate zones and recordsets", "error", err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0)
return
}
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1)
}

func (c *ZonesCollector) collectMetrics(ctx context.Context, ch chan<- prometheus.Metric) error {
// Each metric family reads from the same snapshot while zones or records change.
tx, err := c.db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead, ReadOnly: true})
if err != nil {
return fmt.Errorf("begin scrape transaction: %w", err)
}
defer func() { _ = tx.Rollback() }()
queries := designatedb.New(tx)

zones, err := queries.ZoneGetAll(ctx)
if err != nil {
return fmt.Errorf("query zones: %w", err)
}
counts, err := queries.RecordsetCountByZone(ctx)
if err != nil {
return fmt.Errorf("query recordset counts: %w", err)
}
var recordsets []designatedb.RecordsetGetAllWithStatusRow
if !c.disableRecordsetsStatus {
recordsets, err = queries.RecordsetGetAllWithStatus(ctx)
if err != nil {
return fmt.Errorf("query recordset status: %w", err)
}
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit scrape transaction: %w", err)
}

for _, zone := range zones {
ch <- prometheus.MustNewConstMetric(zoneStatusDesc, prometheus.GaugeValue,
designateStatusValue(zone.Status), zone.ID, zone.Name, zone.Status, zone.TenantID.String, zone.Type)
}
for _, count := range counts {
ch <- prometheus.MustNewConstMetric(recordsetsDesc, prometheus.GaugeValue,
float64(count.RecordsetCount), count.ZoneID, count.ZoneName, count.TenantID.String)
}
for _, recordset := range recordsets {
ch <- prometheus.MustNewConstMetric(recordsetsStatusDesc, prometheus.GaugeValue,
designateStatusValue(recordset.Status), recordset.ID, recordset.Name, recordset.Status,
recordset.ZoneID, recordset.ZoneName, recordset.Type)
}
ch <- prometheus.MustNewConstMetric(zonesDesc, prometheus.GaugeValue, float64(len(zones)))
return nil
}

func designateStatusValue(status string) float64 {
return util.StatusToValue(strings.ToUpper(status), statuses)
}
Loading