Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

chore(sqlstore): added connection max lifetime support (#10913)

Vikrant Gupta3e241944e7ae
diff --git a/conf/example.yaml b/conf/example.yaml
index 4da088342c..857b799e69 100644
--- a/conf/example.yaml
+++ b/conf/example.yaml
@@ -82,6 +82,9 @@ sqlstore:
provider: sqlite
# The maximum number of open connections to the database.
max_open_conns: 100
+ # The maximum amount of time a connection may be reused.
+ # If max_conn_lifetime == 0, connections are not closed due to a connection's age.
+ max_conn_lifetime: 0
sqlite:
# The path to the SQLite database file.
path: /var/lib/signoz/signoz.db
diff --git a/ee/sqlstore/postgressqlstore/provider.go b/ee/sqlstore/postgressqlstore/provider.go
index 3400d21701..3a6215c931 100644
--- a/ee/sqlstore/postgressqlstore/provider.go
+++ b/ee/sqlstore/postgressqlstore/provider.go
@@ -54,6 +54,7 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
// Set the maximum number of open connections
pgConfig.MaxConns = int32(config.Connection.MaxOpenConns)
+ pgConfig.MaxConnLifetime = config.Connection.MaxConnLifetime
// Use pgxpool to create a connection pool
pool, err := pgxpool.NewWithConfig(ctx, pgConfig)
diff --git a/pkg/sqlstore/config.go b/pkg/sqlstore/config.go
index cb02c42820..f6d97dba17 100644
--- a/pkg/sqlstore/config.go
+++ b/pkg/sqlstore/config.go
@@ -39,6 +39,10 @@ type SqliteConfig struct {
type ConnectionConfig struct {
// MaxOpenConns is the maximum number of open connections to the database.
MaxOpenConns int `mapstructure:"max_open_conns"`
+
+ // MaxConnLifetime is the maximum amount of time a connection may be reused.
+ // If max_conn_lifetime == 0, connections are not closed due to a connection's age.
+ MaxConnLifetime time.Duration `mapstructure:"max_conn_lifetime"`
}
func NewConfigFactory() factory.ConfigFactory {
@@ -49,7 +53,8 @@ func newConfig() factory.Config {
return Config{
Provider: "sqlite",
Connection: ConnectionConfig{
- MaxOpenConns: 100,
+ MaxOpenConns: 100,
+ MaxConnLifetime: 0,
},
Sqlite: SqliteConfig{
Path: "/var/lib/signoz/signoz.db",
diff --git a/pkg/sqlstore/config_test.go b/pkg/sqlstore/config_test.go
index 32129759b8..db457f3f43 100644
--- a/pkg/sqlstore/config_test.go
+++ b/pkg/sqlstore/config_test.go
@@ -19,6 +19,7 @@ func TestNewWithEnvProvider(t *testing.T) {
t.Setenv("SIGNOZ_SQLSTORE_SQLITE_BUSY__TIMEOUT", "5s")
t.Setenv("SIGNOZ_SQLSTORE_SQLITE_TRANSACTION__MODE", "immediate")
t.Setenv("SIGNOZ_SQLSTORE_MAX__OPEN__CONNS", "50")
+ t.Setenv("SIGNOZ_SQLSTORE_MAX__CONN__LIFETIME", "3h")
conf, err := config.New(
context.Background(),
@@ -41,7 +42,8 @@ func TestNewWithEnvProvider(t *testing.T) {
expected := &Config{
Provider: "sqlite",
Connection: ConnectionConfig{
- MaxOpenConns: 50,
+ MaxOpenConns: 50,
+ MaxConnLifetime: time.Hour * 3,
},
Sqlite: SqliteConfig{
Path: "/tmp/test.db",
diff --git a/pkg/sqlstore/sqlitesqlstore/provider.go b/pkg/sqlstore/sqlitesqlstore/provider.go
index 4e050de1f6..4714af2283 100644
--- a/pkg/sqlstore/sqlitesqlstore/provider.go
+++ b/pkg/sqlstore/sqlitesqlstore/provider.go
@@ -56,6 +56,7 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
}
settings.Logger().InfoContext(ctx, "connected to sqlite", slog.String("path", config.Sqlite.Path))
sqldb.SetMaxOpenConns(config.Connection.MaxOpenConns)
+ sqldb.SetConnMaxLifetime(config.Connection.MaxConnLifetime)
sqliteDialect := sqlitedialect.New()
bunDB := sqlstore.NewBunDB(settings, sqldb, sqliteDialect, hooks)