-
Notifications
You must be signed in to change notification settings - Fork 770
Add connection limits metrics for pg_roles and pg_database #997
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
Merged
sysadmind
merged 6 commits into
prometheus-community:master
from
jocelynthode:add-connection-limit
Feb 22, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d7c22c
Add database connection limits metrics
jocelynthode 20dc930
Add roles connection limits metrics
jocelynthode bacd6be
Fix copyright year
jocelynthode 89aeb6c
Fix spacing in pgDatabaseQuery
jocelynthode 3c1fb00
Fix case on pgRolesConnectionLimitsQuery
jocelynthode 95ef252
Do not add roleMetrics when row is not valid
jocelynthode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
// Copyright 2024 The Prometheus Authors | ||
// 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 collector | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const rolesSubsystem = "roles" | ||
|
||
func init() { | ||
registerCollector(rolesSubsystem, defaultEnabled, NewPGRolesCollector) | ||
} | ||
|
||
type PGRolesCollector struct { | ||
log log.Logger | ||
} | ||
|
||
func NewPGRolesCollector(config collectorConfig) (Collector, error) { | ||
return &PGRolesCollector{ | ||
log: config.logger, | ||
}, nil | ||
} | ||
|
||
var ( | ||
pgRolesConnectionLimitsDesc = prometheus.NewDesc( | ||
prometheus.BuildFQName( | ||
namespace, | ||
rolesSubsystem, | ||
"connection_limit", | ||
), | ||
"Connection limit set for the role", | ||
[]string{"rolname"}, nil, | ||
) | ||
|
||
pgRolesConnectionLimitsQuery = "SELECT pg_roles.rolname, pg_roles.rolconnlimit FROM pg_roles" | ||
) | ||
|
||
// Update implements Collector and exposes roles connection limits. | ||
// It is called by the Prometheus registry when collecting metrics. | ||
func (c PGRolesCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { | ||
db := instance.getDB() | ||
// Query the list of databases | ||
rows, err := db.QueryContext(ctx, | ||
pgRolesConnectionLimitsQuery, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var rolname sql.NullString | ||
var connLimit sql.NullInt64 | ||
if err := rows.Scan(&rolname, &connLimit); err != nil { | ||
return err | ||
} | ||
|
||
if !rolname.Valid { | ||
continue | ||
} | ||
rolnameLabel := rolname.String | ||
|
||
if !connLimit.Valid { | ||
continue | ||
} | ||
connLimitMetric := float64(connLimit.Int64) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
pgRolesConnectionLimitsDesc, | ||
prometheus.GaugeValue, connLimitMetric, rolnameLabel, | ||
) | ||
} | ||
|
||
return rows.Err() | ||
} |
This file contains hidden or 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,58 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// 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 collector | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
"github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestPGRolesCollector(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("Error opening a stub db connection: %s", err) | ||
} | ||
defer db.Close() | ||
|
||
inst := &instance{db: db} | ||
|
||
mock.ExpectQuery(sanitizeQuery(pgRolesConnectionLimitsQuery)).WillReturnRows(sqlmock.NewRows([]string{"rolname", "rolconnlimit"}). | ||
AddRow("postgres", 15)) | ||
|
||
ch := make(chan prometheus.Metric) | ||
go func() { | ||
defer close(ch) | ||
c := PGRolesCollector{} | ||
if err := c.Update(context.Background(), inst, ch); err != nil { | ||
t.Errorf("Error calling PGRolesCollector.Update: %s", err) | ||
} | ||
}() | ||
|
||
expected := []MetricResult{ | ||
{labels: labelMap{"rolname": "postgres"}, value: 15, metricType: dto.MetricType_GAUGE}, | ||
} | ||
convey.Convey("Metrics comparison", t, func() { | ||
for _, expect := range expected { | ||
m := readMetric(<-ch) | ||
convey.So(expect, convey.ShouldResemble, m) | ||
} | ||
}) | ||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled exceptions: %s", err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SuperQ How do you feel about this being default enabled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any update @SuperQ ?