Skip to content

Commit

Permalink
Added support collect Patroni metrics (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Mikhail Grigorev <[email protected]>
  • Loading branch information
CHERTS and Mikhail Grigorev authored Apr 16, 2024
1 parent da0f031 commit e36ed2e
Show file tree
Hide file tree
Showing 15 changed files with 1,022 additions and 51 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ COPY --from=build /app/bin/pgscv /bin/pgscv
EXPOSE 9890
ENTRYPOINT ["/bin/pgscv"]
#ENTRYPOINT ["/bin/docker_entrypoint.sh"]
CMD ["--log-level=info"]
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
DOCKER_ACCOUNT = cherts
APPNAME = pgscv
APPOS = linux
#APPOS = ${GOOS}

TAG=$(shell git tag -l --sort=-creatordate | head -n 1)
COMMIT=$(shell git rev-parse --short HEAD)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
This project is a continuation of the development of the original pgSCV by [Alexey Lesovsky](https://github.com/lesovsky)

### Features
- **Supported services:** support collecting metrics of PostgreSQL and Pgbouncer.
- **Supported services:** support collecting metrics of PostgreSQL, Pgbouncer and Patroni.
- **OS metrics:** support collecting metrics of operating system.
- **TLS and authentication**. `/metrics` endpoint could be protected with basic authentication and TLS.
- **Collecting metrics from multiple services**. pgSCV can collect metrics from many databases instances.
Expand Down
2 changes: 1 addition & 1 deletion README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Данный проект является продолжением развития оригинального pgSCV авторства [Alexey Lesovsky](https://github.com/lesovsky)

### Основные возможности
- **Поддерживаемые сервисы**: поддержка сбора показателей работы PostgreSQL и Pgbouncer;
- **Поддерживаемые сервисы**: поддержка сбора показателей работы PostgreSQL, Pgbouncer и Patroni;
- **Метрики ОС:** поддержка сбора показателей работы операционной системы;
- **TLS и аутентификация**: Эндпойнт `/metrics` может быть защищен с помощью базовой аутентификации и TLS;
- **Сбор показателей из нескольких сервисов**: pgSCV может собирать метрики из многих экземпляров баз данных, включая базы данных расположенные в облачных средах (Amazon AWS, Yandex.Cloud, VK.Cloud);
Expand Down
26 changes: 25 additions & 1 deletion internal/collector/collector.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package collector

import (
"sync"

"github.com/cherts/pgscv/internal/filter"
"github.com/cherts/pgscv/internal/log"
"github.com/cherts/pgscv/internal/model"
"github.com/prometheus/client_golang/prometheus"
"sync"
)

// Factories defines collector functions which used for collecting metrics.
Expand Down Expand Up @@ -106,6 +107,29 @@ func (f Factories) RegisterPgbouncerCollectors(disabled []string) {
}
}

// RegisterPatroniCollectors unions all patroni-related collectors and registers them in single place.
func (f Factories) RegisterPatroniCollectors(disabled []string) {
if stringsContains(disabled, "patroni") {
log.Debugln("disable all patroni collectors")
return
}

funcs := map[string]func(labels, model.CollectorSettings) (Collector, error){
"patroni/pgscv": NewPgscvServicesCollector,
"patroni/common": NewPatroniCommonCollector,
}

for name, fn := range funcs {
if stringsContains(disabled, name) {
log.Debugln("disable ", name)
continue
}

log.Debugln("enable ", name)
f.register(name, fn)
}
}

// register is the generic routine which register any kind of collectors.
func (f Factories) register(collector string, factory func(labels, model.CollectorSettings) (Collector, error)) {
f[collector] = factory
Expand Down
22 changes: 13 additions & 9 deletions internal/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package collector

import (
"context"
"github.com/jackc/pgx/v4"
"github.com/cherts/pgscv/internal/log"
"github.com/cherts/pgscv/internal/model"
"github.com/cherts/pgscv/internal/store"
"fmt"
"net"
"regexp"
"strconv"
"strings"

"github.com/cherts/pgscv/internal/log"
"github.com/cherts/pgscv/internal/model"
"github.com/cherts/pgscv/internal/store"
"github.com/jackc/pgx/v4"
)

// Config defines collector's global configuration.
Expand All @@ -19,6 +21,8 @@ type Config struct {
ServiceType string
// ConnString defines a connection string used to connecting to the service
ConnString string
// BaseURL defines a URL string for connecting to HTTP service
BaseURL string
// NoTrackMode controls collector to gather and send sensitive information, such as queries texts.
NoTrackMode bool
// postgresServiceConfig defines collector's options specific for Postgres service
Expand Down Expand Up @@ -79,7 +83,7 @@ func newPostgresServiceConfig(connStr string) (postgresServiceConfig, error) {
// Get Postgres block size.
err = conn.Conn().QueryRow(context.Background(), "SELECT setting FROM pg_settings WHERE name = 'block_size'").Scan(&setting)
if err != nil {
return config, err
return config, fmt.Errorf("failed to get block_size setting from pg_settings, %s, please check user grants", err)
}
bsize, err := strconv.ParseUint(setting, 10, 64)
if err != nil {
Expand All @@ -91,7 +95,7 @@ func newPostgresServiceConfig(connStr string) (postgresServiceConfig, error) {
// Get Postgres WAL segment size.
err = conn.Conn().QueryRow(context.Background(), "SELECT setting FROM pg_settings WHERE name = 'wal_segment_size'").Scan(&setting)
if err != nil {
return config, err
return config, fmt.Errorf("failed to get wal_segment_size setting from pg_settings, %s, please check user grants", err)
}
walSegSize, err := strconv.ParseUint(setting, 10, 64)
if err != nil {
Expand All @@ -103,7 +107,7 @@ func newPostgresServiceConfig(connStr string) (postgresServiceConfig, error) {
// Get Postgres server version
err = conn.Conn().QueryRow(context.Background(), "SELECT setting FROM pg_settings WHERE name = 'server_version_num'").Scan(&setting)
if err != nil {
return config, err
return config, fmt.Errorf("failed to get server_version_num setting from pg_settings, %s, please check user grants", err)
}
version, err := strconv.Atoi(setting)
if err != nil {
Expand All @@ -119,15 +123,15 @@ func newPostgresServiceConfig(connStr string) (postgresServiceConfig, error) {
// Get Postgres data directory
err = conn.Conn().QueryRow(context.Background(), "SELECT setting FROM pg_settings WHERE name = 'data_directory'").Scan(&setting)
if err != nil {
return config, err
return config, fmt.Errorf("failed to get data_directory setting from pg_settings, %s, please check user grants", err)
}

config.dataDirectory = setting

// Get setting of 'logging_collector' GUC.
err = conn.Conn().QueryRow(context.Background(), "SELECT setting FROM pg_settings WHERE name = 'logging_collector'").Scan(&setting)
if err != nil {
return config, err
return config, fmt.Errorf("failed to get logging_collector setting from pg_settings, %s, please check user grants", err)
}

if setting == "on" {
Expand Down
Loading

0 comments on commit e36ed2e

Please sign in to comment.