Skip to content

Commit

Permalink
Update clickhouse config storage and use (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner authored Jul 2, 2024
1 parent 011cae5 commit adce864
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/telemetry-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
_ = ctx

repoLogger := logger.With().Str("component", "repository").Logger()
chService, err := ch.NewService(settings, nil)
chService, err := ch.NewService(settings)
if err != nil {
logger.Fatal().Err(err).Msg("Couldn't create ClickHouse service.")
}
Expand Down
20 changes: 9 additions & 11 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package config

import "github.com/DIMO-Network/clickhouse-infra/pkg/connect/config"

// Settings contains the application config.
type Settings struct {
Port int `yaml:"PORT"`
MonPort int `yaml:"MON_PORT"`
ClickHouseHost string `yaml:"CLICKHOUSE_HOST"`
ClickHouseTCPPort int `yaml:"CLICKHOUSE_TCP_PORT"`
ClickHouseUser string `yaml:"CLICKHOUSE_USER"`
ClickHousePassword string `yaml:"CLICKHOUSE_PASSWORD"`
ClickHouseDatabase string `yaml:"CLICKHOUSE_DATABASE"`
TokenExchangeJWTKeySetURL string `yaml:"TOKEN_EXCHANGE_JWK_KEY_SET_URL"`
TokenExchangeIssuer string `yaml:"TOKEN_EXCHANGE_ISSUER_URL"`
VehicleNFTAddress string `yaml:"VEHICLE_NFT_ADDRESS"`
MaxRequestDuration string `yaml:"MAX_REQUEST_DURATION"`
Port int `yaml:"PORT"`
MonPort int `yaml:"MON_PORT"`
CLickhouse config.Settings `yaml:",inline"`
TokenExchangeJWTKeySetURL string `yaml:"TOKEN_EXCHANGE_JWK_KEY_SET_URL"`
TokenExchangeIssuer string `yaml:"TOKEN_EXCHANGE_ISSUER_URL"`
VehicleNFTAddress string `yaml:"VEHICLE_NFT_ADDRESS"`
MaxRequestDuration string `yaml:"MAX_REQUEST_DURATION"`
}
13 changes: 6 additions & 7 deletions internal/service/ch/ch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package ch
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"time"

Expand All @@ -27,21 +26,21 @@ type Service struct {
}

// NewService creates a new ClickHouse service.
func NewService(settings config.Settings, rootCAs *x509.CertPool) (*Service, error) {
func NewService(settings config.Settings) (*Service, error) {
maxExecutionTime, err := getMaxExecutionTime(settings.MaxRequestDuration)
if err != nil {
return nil, fmt.Errorf("failed to get max execution time: %w", err)
}
addr := fmt.Sprintf("%s:%d", settings.ClickHouseHost, settings.ClickHouseTCPPort)
addr := fmt.Sprintf("%s:%d", settings.CLickhouse.Host, settings.CLickhouse.Port)
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{addr},
Auth: clickhouse.Auth{
Username: settings.ClickHouseUser,
Password: settings.ClickHousePassword,
Database: settings.ClickHouseDatabase,
Username: settings.CLickhouse.User,
Password: settings.CLickhouse.Password,
Database: settings.CLickhouse.Database,
},
TLS: &tls.Config{
RootCAs: rootCAs,
RootCAs: settings.CLickhouse.RootCAs,
},
Settings: map[string]any{
// ClickHouse will interrupt a query if the projected execution time exceeds the specified max_execution_time.
Expand Down
16 changes: 4 additions & 12 deletions internal/service/ch/ch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ func (c *CHServiceTestSuite) SetupSuite() {
c.Require().NoError(err, "Failed to run migrations")

settings := config.Settings{
ClickHouseHost: cfg.Host,
ClickHouseTCPPort: cfg.Port,
ClickHouseUser: cfg.User,
ClickHousePassword: cfg.Password,
ClickHouseDatabase: cfg.Database,
CLickhouse: cfg,
MaxRequestDuration: "1s",
}
c.chService, err = NewService(settings, cfg.RootCAs)
c.chService, err = NewService(settings)
c.Require().NoError(err, "Failed to create repository")
c.dataStartTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
c.insertTestData()
Expand Down Expand Up @@ -267,14 +263,10 @@ func (c *CHServiceTestSuite) TestExecutionTimeout() {
cfg := c.container.Config()

settings := config.Settings{
ClickHouseHost: cfg.Host,
ClickHouseTCPPort: cfg.Port,
ClickHouseUser: cfg.User,
ClickHousePassword: cfg.Password,
ClickHouseDatabase: cfg.Database,
CLickhouse: cfg,
MaxRequestDuration: "1s",
}
chService, err := NewService(settings, cfg.RootCAs)
chService, err := NewService(settings)
c.Require().NoError(err, "Failed to create repository")

var delay bool
Expand Down

0 comments on commit adce864

Please sign in to comment.