signoz icon indicating copy to clipboard operation
signoz copied to clipboard

chore: standardize env for query service

Open slayer321 opened this issue 1 year ago • 6 comments

Summary

Standardize ENV variable for query service

Related Issues / PR's

fixes #5266

slayer321 avatar Aug 11 '24 06:08 slayer321

Welcome to the SigNoz community! Thank you for your first pull request and making this project better. 🤗

welcome[bot] avatar Aug 11 '24 06:08 welcome[bot]

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Aug 11 '24 06:08 CLAassistant

@shivanshuraj1333 Can you please review it.

slayer321 avatar Aug 13 '24 14:08 slayer321

This is not what I intended, add something like this, and then do a bootstrap load at query service start, handle errors if there is any access issue.

package config

import (
	"os"
)

type Config struct {
	SignozJwtSecret                               string
	StorageType                                   string
	ClickHouseUrl                                 string
	ClickHouseOptimizeReadInOrderRegex            string
	ClickHouseMaxExecutionTimeLeaf                string
	ClickHouseTimeoutBeforeCheckingExecutionSpeed string
	ClickHouseMaxBytesToRead                      string
	SmtpEnabled                                   string
	DeploymentType                                string
	SmtpHost                                      string
	SmtpPort                                      string
	SmtpUsername                                  string
	SmtpPassword                                  string
	SmtpFrom                                      string
}

var AppConfig Config

func LoadConfig() {
	AppConfig = Config{
		SignozJwtSecret:                               os.Getenv("SIGNOZ_JWT_SECRET"),
		StorageType:                                   os.Getenv("STORAGE"),
		ClickHouseUrl:                                 os.Getenv("CLICKHOUSE_URL"),
		ClickHouseOptimizeReadInOrderRegex:            os.Getenv("CLICKHOUSE_OPTIMIZE_READ_IN_ORDER_REGEX"),
		ClickHouseMaxExecutionTimeLeaf:                os.Getenv("CLICKHOUSE_MAX_EXECUTION_TIME_LEAF"),
		ClickHouseTimeoutBeforeCheckingExecutionSpeed: os.Getenv("CLICKHOUSE_TIMEOUT_BEFORE_CHECKING_EXECUTION_SPEED"),
		ClickHouseMaxBytesToRead:                      os.Getenv("CLICKHOUSE_MAX_BYTES_TO_READ"),
		SmtpEnabled:                                   os.Getenv("SMTP_ENABLED"),
		DeploymentType:                                os.Getenv("DEPLOYMENT_TYPE"),
		SmtpHost:                                      os.Getenv("SMTP_HOST"),
		SmtpPort:                                      os.Getenv("SMTP_PORT"),
		SmtpUsername:                                  os.Getenv("SMTP_USERNAME"),
		SmtpPassword:                                  os.Getenv("SMTP_PASSWORD"),
		SmtpFrom:                                      os.Getenv("SMTP_FROM"),
	}
}

and use it like below jwtSecret := config.AppConfig.SignozJwtSecret

shivanshuraj1333 avatar Aug 13 '24 14:08 shivanshuraj1333

Ok, make sense. About handle errors part do we want to have something like below ?

 // validateConfig checks if the configuration is valid
func validateConfig(config Config) error {
	if config.SignozJwtSecret == "" {
		return errors.New("missing SIGNOZ_JWT_SECRET")
	}
	if config.StorageType == "" {
		return errors.New("missing STORAGE_TYPE")
	}
	if config.ClickHouseUrl == "" {
		return errors.New("missing CLICKHOUSE_URL")
	}
	if config.SmtpHost == "" {
		return errors.New("missing SMTP_HOST")
	}
	if config.SmtpPort == 0 {
		return errors.New("invalid SMTP_PORT")
	}
	if config.SmtpUsername == "" {
		return errors.New("missing SMTP_USERNAME")
	}
	if config.SmtpPassword == "" {
		return errors.New("missing SMTP_PASSWORD")
	}
	if config.SmtpFrom == "" {
		return errors.New("missing SMTP_FROM")
	}
	return nil
}

slayer321 avatar Aug 15 '24 07:08 slayer321

No, before accessing the flags in the code, handle the error there using existing error handling (mostly log the error).

shivanshuraj1333 avatar Aug 15 '24 07:08 shivanshuraj1333

Shouldn't this check and alert all errors on load? I don't think the system is much use without a ClickhouseServer variable defined.

Aeolun avatar Oct 04 '24 09:10 Aeolun

Hi @slayer321, Thank you for the PR! We are however in the middle of a major codebase revamp where we will also be standardising config as per https://github.com/SigNoz/signoz/issues/6805.

As part of the config being created, we are also going to validate and fail fast. That should take care of @Aeolun's point!

therealpandey avatar Feb 01 '25 19:02 therealpandey