forked from cloudnative-pg/cloudnative-pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add pool.Pooler interface to pgbouncer/metricsserver (cloudnat…
…ive-pg#2480) This patch leverages the pool.Pooler interface to add unit tests for pgbouncer metrics server. Partially Close cloudnative-pg#2358 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Jaime Silvela <[email protected]> Co-authored-by: Jaime Silvela <[email protected]>
- Loading branch information
Showing
5 changed files
with
399 additions
and
2 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
pkg/management/pgbouncer/metricsserver/metricsserver_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metricsserver | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("MetricsServer", func() { | ||
Describe("Setup", func() { | ||
BeforeEach(func() { | ||
server = nil | ||
registry = nil | ||
exporter = nil | ||
}) | ||
|
||
It("should register exporters and collectors successfully", func() { | ||
err := Setup() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
mfs, err := registry.Gather() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(mfs).NotTo(BeEmpty()) | ||
|
||
Expect(exporter.Metrics.CollectionsTotal).NotTo(BeNil()) | ||
Expect(exporter.Metrics.PgCollectionErrors).NotTo(BeNil()) | ||
Expect(exporter.Metrics.Error).NotTo(BeNil()) | ||
Expect(exporter.Metrics.CollectionDuration).NotTo(BeNil()) | ||
Expect(exporter.Metrics.PgbouncerUp).NotTo(BeNil()) | ||
Expect(exporter.Metrics.ShowLists).NotTo(BeNil()) | ||
Expect(exporter.Metrics.ShowPools).NotTo(BeNil()) | ||
Expect(exporter.Metrics.ShowStats).NotTo(BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metricsserver | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Exporter", func() { | ||
var ( | ||
registry *prometheus.Registry | ||
db *sql.DB | ||
mock sqlmock.Sqlmock | ||
exp *Exporter | ||
ch chan prometheus.Metric | ||
columns16 = []string{ | ||
"database", | ||
"user", | ||
"cl_active", | ||
"cl_waiting", | ||
"cl_active_cancel_req", | ||
"cl_waiting_cancel_req", | ||
"sv_active", | ||
"sv_active_cancel", | ||
"sv_being_canceled", | ||
"sv_idle", | ||
"sv_used", | ||
"sv_tested", | ||
"sv_login", | ||
"maxwait", | ||
"maxwait_us", | ||
"pool_mode", | ||
} | ||
) | ||
|
||
BeforeEach(func() { | ||
var err error | ||
db, mock, err = sqlmock.New() | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
exp = &Exporter{ | ||
Metrics: newMetrics(), | ||
pool: fakePooler{db: db}, | ||
} | ||
|
||
registry = prometheus.NewRegistry() | ||
registry.MustRegister(exp.Metrics.PgbouncerUp) | ||
registry.MustRegister(exp.Metrics.Error) | ||
|
||
ch = make(chan prometheus.Metric, 1000) | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(mock.ExpectationsWereMet()).To(Succeed()) | ||
}) | ||
|
||
Context("collectShowPools", func() { | ||
It("should react properly if SQL shows no pools", func() { | ||
mock.ExpectQuery("SHOW POOLS;").WillReturnError(sql.ErrNoRows) | ||
exp.collectShowPools(ch, db) | ||
|
||
metrics, err := registry.Gather() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pgBouncerUpValue := getMetric(metrics, pgBouncerUpKey).GetMetric()[0].GetGauge().GetValue() | ||
Expect(pgBouncerUpValue).Should(BeEquivalentTo(0)) | ||
|
||
errorValue := getMetric(metrics, lastCollectionErrorKey).GetMetric()[0].GetGauge().GetValue() | ||
Expect(errorValue).To(BeEquivalentTo(1)) | ||
}) | ||
|
||
It("should handle SQL rows scanning properly", func() { | ||
mock.ExpectQuery("SHOW POOLS;"). | ||
WillReturnRows(sqlmock.NewRows(columns16). | ||
AddRow("db1", "user1", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "session")) | ||
|
||
exp.collectShowPools(ch, db) | ||
|
||
metrics, err := registry.Gather() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pgBouncerUpValue := getMetric(metrics, pgBouncerUpKey).GetMetric()[0].GetGauge().GetValue() | ||
Expect(pgBouncerUpValue).Should(BeEquivalentTo(1)) | ||
|
||
errorValue := getMetric(metrics, lastCollectionErrorKey).GetMetric()[0].GetGauge().GetValue() | ||
Expect(errorValue).To(BeEquivalentTo(0)) | ||
}) | ||
|
||
It("should handle error during SQL rows scanning", func() { | ||
mock.ExpectQuery("SHOW POOLS;"). | ||
WillReturnRows(sqlmock.NewRows(columns16). | ||
AddRow("db1", "user1", "error", 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "session")) | ||
|
||
exp.collectShowPools(ch, db) | ||
|
||
registry.MustRegister(exp.Metrics.PgCollectionErrors) | ||
|
||
metrics, err := registry.Gather() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pgBouncerUpValue := getMetric(metrics, pgBouncerUpKey).GetMetric()[0].GetGauge().GetValue() | ||
Expect(pgBouncerUpValue).Should(BeEquivalentTo(1)) | ||
|
||
errorsMetric := getMetric(metrics, collectionErrorsTotalKey).GetMetric()[0] | ||
label := errorsMetric.GetLabel()[0] | ||
Expect(*label.Name).To(BeEquivalentTo("collector")) | ||
Expect(*label.Value).To(BeEquivalentTo("sql: Scan error on column index 2, name \"cl_active\": " + | ||
"converting driver.Value type string (\"error\") to a int: invalid syntax")) | ||
Expect(errorsMetric.GetCounter().GetValue()).To(BeEquivalentTo(1)) | ||
}) | ||
|
||
It("should return the correct integer value", func() { | ||
Expect(poolModeToInt("session")).To(Equal(1)) | ||
Expect(poolModeToInt("transaction")).To(Equal(2)) | ||
Expect(poolModeToInt("statement")).To(Equal(3)) | ||
Expect(poolModeToInt("random")).To(Equal(-1)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metricsserver | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("MetricsServer", func() { | ||
const ( | ||
showStatsQuery = "SHOW STATS;" | ||
) | ||
|
||
var ( | ||
db *sql.DB | ||
mock sqlmock.Sqlmock | ||
exp *Exporter | ||
registry *prometheus.Registry | ||
ch chan prometheus.Metric | ||
) | ||
|
||
BeforeEach(func() { | ||
var err error | ||
db, mock, err = sqlmock.New() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
exp = &Exporter{ | ||
Metrics: newMetrics(), | ||
pool: fakePooler{db: db}, | ||
} | ||
registry = prometheus.NewRegistry() | ||
registry.MustRegister(exp.Metrics.Error) | ||
registry.MustRegister(exp.Metrics.ShowStats.TotalXactCount) | ||
registry.MustRegister(exp.Metrics.ShowStats.TotalQueryCount) | ||
registry.MustRegister(exp.Metrics.PgbouncerUp) | ||
|
||
ch = make(chan prometheus.Metric, 1000) | ||
}) | ||
|
||
Context("collectShowStats", func() { | ||
It("should handle successful SQL query execution", func() { | ||
mock.ExpectQuery(showStatsQuery).WillReturnRows(getShowStatsRows()) | ||
|
||
exp.collectShowStats(ch, db) | ||
|
||
metrics, err := registry.Gather() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pgbouncerUpMetric := getMetric(metrics, "cnpg_pgbouncer_up") | ||
Expect(pgbouncerUpMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(1)) | ||
|
||
lastCollectionErrorMetric := getMetric(metrics, "cnpg_pgbouncer_last_collection_error") | ||
Expect(lastCollectionErrorMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(0)) | ||
|
||
totalXactCountMetric := getMetric(metrics, "cnpg_pgbouncer_stats_total_xact_count") | ||
Expect(totalXactCountMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(1)) | ||
|
||
totalQueryCountMetric := getMetric(metrics, "cnpg_pgbouncer_stats_total_query_count") | ||
Expect(totalQueryCountMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(2)) | ||
}) | ||
|
||
It("should handle error during SQL query execution", func() { | ||
mock.ExpectQuery(showStatsQuery).WillReturnError(errors.New("database error")) | ||
|
||
exp.collectShowStats(ch, db) | ||
|
||
metrics, err := registry.Gather() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pgbouncerUpMetric := getMetric(metrics, "cnpg_pgbouncer_up") | ||
Expect(pgbouncerUpMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(0)) | ||
|
||
lastCollectionErrorMetric := getMetric(metrics, "cnpg_pgbouncer_last_collection_error") | ||
Expect(lastCollectionErrorMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(1)) | ||
}) | ||
|
||
It("should handle error during rows scanning", func() { | ||
mock.ExpectQuery(showStatsQuery). | ||
WillReturnRows(sqlmock.NewRows([]string{"total_xact_count"}). | ||
AddRow("invalid")) | ||
|
||
exp.collectShowStats(ch, db) | ||
|
||
metrics, err := registry.Gather() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pgbouncerUpMetric := getMetric(metrics, "cnpg_pgbouncer_up") | ||
Expect(pgbouncerUpMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(1)) | ||
|
||
lastCollectionErrorMetric := getMetric(metrics, "cnpg_pgbouncer_last_collection_error") | ||
Expect(lastCollectionErrorMetric.GetMetric()[0].GetGauge().GetValue()).To(BeEquivalentTo(1)) | ||
}) | ||
}) | ||
}) | ||
|
||
func getShowStatsRows() *sqlmock.Rows { | ||
columns := []string{ | ||
"database", | ||
"total_xact_count", | ||
"total_query_count", | ||
"total_received", | ||
"total_sent", | ||
"total_xact_time", | ||
"total_query_time", | ||
"total_wait_time", | ||
"avg_xact_count", | ||
"avg_query_count", | ||
"avg_recv", | ||
"avg_sent", | ||
"avg_xact_time", | ||
"avg_query_time", | ||
"avg_wait_time", | ||
} | ||
|
||
return sqlmock.NewRows(columns). | ||
AddRow("db1", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) | ||
} |
Oops, something went wrong.