From f13279c700cb8ff562e850ef71440d6ee60a5ff0 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Thu, 1 May 2025 17:15:27 +0200 Subject: [PATCH 1/4] feat: go database metrics Signed-off-by: Danny Kopping --- cli/server.go | 11 +++++++++++ go.mod | 3 +++ go.sum | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/cli/server.go b/cli/server.go index 39cfa52571595..6bd17afd7dba4 100644 --- a/cli/server.go +++ b/cli/server.go @@ -37,6 +37,7 @@ import ( embeddedpostgres "github.com/fergusstrange/embedded-postgres" "github.com/google/go-github/v43/github" "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgconn" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -739,6 +740,16 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. _ = sqlDB.Close() }() + var dbName string + dbCfg, err := pgconn.ParseConfig(dbURL) + if err != nil { + options.Logger.Debug(ctx, "failed to determine database name for stats collection", slog.Error(err)) + dbName = "coder" + } else { + dbName = dbCfg.Database + } + options.PrometheusRegistry.MustRegister(collectors.NewDBStatsCollector(sqlDB, dbName)) + options.Database = database.New(sqlDB) ps, err := pubsub.New(ctx, logger.Named("pubsub"), sqlDB, dbURL) if err != nil { diff --git a/go.mod b/go.mod index 8ff0ba1fa2376..ca157b3ffc603 100644 --- a/go.mod +++ b/go.mod @@ -489,6 +489,7 @@ require ( require ( github.com/coder/preview v0.0.1 github.com/fsnotify/fsnotify v1.9.0 + github.com/jackc/pgx/v5 v5.7.4 github.com/kylecarbs/aisdk-go v0.0.5 github.com/mark3labs/mcp-go v0.23.1 ) @@ -514,6 +515,8 @@ require ( github.com/gorilla/websocket v1.5.3 // indirect github.com/hashicorp/go-getter v1.7.8 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/moby/sys/user v0.3.0 // indirect github.com/openai/openai-go v0.1.0-beta.6 // indirect diff --git a/go.sum b/go.sum index fc05152d34122..c47fa85b86b96 100644 --- a/go.sum +++ b/go.sum @@ -1410,6 +1410,12 @@ github.com/illarion/gonotify v1.0.1 h1:F1d+0Fgbq/sDWjj/r66ekjDG+IDeecQKUFH4wNwso github.com/illarion/gonotify v1.0.1/go.mod h1:zt5pmDofZpU1f8aqlK0+95eQhoEAn/d4G4B/FjVW4jE= github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2 h1:9K06NfxkBh25x56yVhWWlKFE8YpicaSfHwoV8SFbueA= github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2/go.mod h1:3A9PQ1cunSDF/1rbTq99Ts4pVnycWg+vlPkfeD2NLFI= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg= +github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jdkato/prose v1.2.1 h1:Fp3UnJmLVISmlc57BgKUzdjr0lOtjqTZicL3PaYy6cU= From 0192ab8e67eec78d6eaddfdd7c4f9d1a3903ae05 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Thu, 1 May 2025 20:16:47 +0200 Subject: [PATCH 2/4] fix: review feedback Signed-off-by: Danny Kopping --- cli/server.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cli/server.go b/cli/server.go index 6bd17afd7dba4..f158811e3b3f5 100644 --- a/cli/server.go +++ b/cli/server.go @@ -740,15 +740,17 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. _ = sqlDB.Close() }() - var dbName string - dbCfg, err := pgconn.ParseConfig(dbURL) - if err != nil { - options.Logger.Debug(ctx, "failed to determine database name for stats collection", slog.Error(err)) - dbName = "coder" - } else { - dbName = dbCfg.Database + if options.DeploymentValues.Prometheus.Enable { + var dbName string + dbCfg, err := pgconn.ParseConfig(dbURL) + if err != nil { + options.Logger.Warn(ctx, "failed to determine database name for stats collection; using 'coder' as placeholder", slog.Error(err)) + dbName = "coder" + } else { + dbName = dbCfg.Database + } + options.PrometheusRegistry.MustRegister(collectors.NewDBStatsCollector(sqlDB, dbName)) } - options.PrometheusRegistry.MustRegister(collectors.NewDBStatsCollector(sqlDB, dbName)) options.Database = database.New(sqlDB) ps, err := pubsub.New(ctx, logger.Named("pubsub"), sqlDB, dbURL) From be9d36ec9fae5eb651c6d01cee46739dba51d564 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Fri, 2 May 2025 09:44:49 +0200 Subject: [PATCH 3/4] fix: do not set database name in sql metrics Signed-off-by: Danny Kopping --- cli/server.go | 11 +---------- go.mod | 3 --- go.sum | 6 ------ 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/cli/server.go b/cli/server.go index f158811e3b3f5..3c274fe0c1c16 100644 --- a/cli/server.go +++ b/cli/server.go @@ -37,7 +37,6 @@ import ( embeddedpostgres "github.com/fergusstrange/embedded-postgres" "github.com/google/go-github/v43/github" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgconn" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -741,15 +740,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. }() if options.DeploymentValues.Prometheus.Enable { - var dbName string - dbCfg, err := pgconn.ParseConfig(dbURL) - if err != nil { - options.Logger.Warn(ctx, "failed to determine database name for stats collection; using 'coder' as placeholder", slog.Error(err)) - dbName = "coder" - } else { - dbName = dbCfg.Database - } - options.PrometheusRegistry.MustRegister(collectors.NewDBStatsCollector(sqlDB, dbName)) + options.PrometheusRegistry.MustRegister(collectors.NewDBStatsCollector(sqlDB, "")) } options.Database = database.New(sqlDB) diff --git a/go.mod b/go.mod index ca157b3ffc603..8ff0ba1fa2376 100644 --- a/go.mod +++ b/go.mod @@ -489,7 +489,6 @@ require ( require ( github.com/coder/preview v0.0.1 github.com/fsnotify/fsnotify v1.9.0 - github.com/jackc/pgx/v5 v5.7.4 github.com/kylecarbs/aisdk-go v0.0.5 github.com/mark3labs/mcp-go v0.23.1 ) @@ -515,8 +514,6 @@ require ( github.com/gorilla/websocket v1.5.3 // indirect github.com/hashicorp/go-getter v1.7.8 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect - github.com/jackc/pgpassfile v1.0.0 // indirect - github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/moby/sys/user v0.3.0 // indirect github.com/openai/openai-go v0.1.0-beta.6 // indirect diff --git a/go.sum b/go.sum index c47fa85b86b96..fc05152d34122 100644 --- a/go.sum +++ b/go.sum @@ -1410,12 +1410,6 @@ github.com/illarion/gonotify v1.0.1 h1:F1d+0Fgbq/sDWjj/r66ekjDG+IDeecQKUFH4wNwso github.com/illarion/gonotify v1.0.1/go.mod h1:zt5pmDofZpU1f8aqlK0+95eQhoEAn/d4G4B/FjVW4jE= github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2 h1:9K06NfxkBh25x56yVhWWlKFE8YpicaSfHwoV8SFbueA= github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2/go.mod h1:3A9PQ1cunSDF/1rbTq99Ts4pVnycWg+vlPkfeD2NLFI= -github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= -github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= -github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= -github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg= -github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jdkato/prose v1.2.1 h1:Fp3UnJmLVISmlc57BgKUzdjr0lOtjqTZicL3PaYy6cU= From b202843e789f937e94f5a32dfd873d149ae1f439 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Fri, 2 May 2025 11:59:45 +0200 Subject: [PATCH 4/4] chore: adding comment Signed-off-by: Danny Kopping --- cli/server.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cli/server.go b/cli/server.go index 3c274fe0c1c16..580dae369446c 100644 --- a/cli/server.go +++ b/cli/server.go @@ -740,6 +740,11 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. }() if options.DeploymentValues.Prometheus.Enable { + // At this stage we don't think the database name serves much purpose in these metrics. + // It requires parsing the DSN to determine it, which requires pulling in another dependency + // (i.e. https://github.com/jackc/pgx), but it's rather heavy. + // The conn string (https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) can + // take different forms, which make parsing non-trivial. options.PrometheusRegistry.MustRegister(collectors.NewDBStatsCollector(sqlDB, "")) }