Skip to content

Commit

Permalink
feat(signoz): refactor connection config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
grandwizard28 committed Jan 9, 2025
1 parent e1cb905 commit 2060905
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
20 changes: 13 additions & 7 deletions pkg/sqlstore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import "go.signoz.io/signoz/pkg/config"
var _ config.Config = (*Config)(nil)

type Config struct {
Provider string `mapstructure:"provider"`
Sqlite SqliteConfig `mapstructure:"sqlite"`
Postgres PostgresConfig `mapstructure:"postgres"`
Provider string `mapstructure:"provider"`
Connection ConnectionConfig `mapstructure:",squash"`
Sqlite SqliteConfig `mapstructure:"sqlite"`
Postgres PostgresConfig `mapstructure:"postgres"`
}

type SqliteConfig struct {
Path string `mapstructure:"path"`
MaxOpenConns int `mapstructure:"max_open_conns"`
Path string `mapstructure:"path"`
}

type ConnectionConfig struct {
MaxOpenConns int `mapstructure:"max_open_conns"`
}

type PostgresConfig struct {
Expand All @@ -31,10 +35,12 @@ func NewConfigFactory() config.ConfigFactory {
func newConfig() config.Config {
return &Config{
Provider: "sqlite",
Sqlite: SqliteConfig{
Path: "/var/lib/signoz/signoz.db",
Connection: ConnectionConfig{
MaxOpenConns: 100,
},
Sqlite: SqliteConfig{
Path: "/var/lib/signoz/signoz.db",
},
}

}
Expand Down
16 changes: 6 additions & 10 deletions pkg/sqlstore/provider/sqlite/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ func New(config sqlstore.Config, providerConfig sqlstore.ProviderConfig) (sqlsto
return nil, fmt.Errorf("provider %q is not supported by sqlite", config.Provider)
}

sqlDB, err := sql.Open("sqlite3", "file:"+config.Sqlite.Path)
sqlDB, err := sql.Open("sqlite3", "file:"+config.Sqlite.Path+"?_foreign_keys=true")
if err != nil {
return nil, err
}
sqlDB.SetMaxOpenConns(config.Sqlite.MaxOpenConns)
bunDB := bun.NewDB(sqlDB, sqlitedialect.New())
providerConfig.Logger.Info("connected to sqlite", zap.String("path", config.Sqlite.Path))

providerConfig.Logger.Info("connected to sqlite", zap.String("path", config.Sqlite.Path+"?_foreign_keys=true"))

// enable foreign key support
if _, err := bunDB.Exec("PRAGMA foreign_keys = ON;"); err != nil {
return nil, err
}
providerConfig.Logger.Info("enabled foreign key support in sqlite", zap.String("path", config.Sqlite.Path))
// Set connection options
sqlDB.SetMaxOpenConns(config.Connection.MaxOpenConns)

// Initialize ORMs
bunDB := bun.NewDB(sqlDB, sqlitedialect.New())
sqlxDB := sqlx.NewDb(sqlDB, "sqlite3")

return &provider{
Expand Down

0 comments on commit 2060905

Please sign in to comment.