Skip to content

Commit

Permalink
Polish config API (#238)
Browse files Browse the repository at this point in the history
* Refine code of config and logging
  • Loading branch information
louyuting authored Sep 16, 2020
1 parent 5250310 commit 1c01bbd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
15 changes: 12 additions & 3 deletions api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/alibaba/sentinel-golang/util"
)

// Initialization func initialize the Sentinel's runtime environment, including:
// 1. override global config, from manually config or yaml file or env variable
// 2. override global logger
// 3. initiate core component async task, including: metric log, system statistic...
// InitDefault initializes Sentinel using the configuration from system
// environment and the default value.
func InitDefault() error {
Expand Down Expand Up @@ -47,11 +51,16 @@ func InitWithConfigFile(configPath string) error {
// initCoreComponents init core components with default config
// it's better SetDefaultConfig before initCoreComponents
func initCoreComponents() error {
if err := metric.InitTask(); err != nil {
return err
if config.MetricLogFlushIntervalSec() > 0 {
if err := metric.InitTask(); err != nil {
return err
}
}

if config.SystemStatCollectIntervalMs() > 0 {
system.InitCollector(config.SystemStatCollectIntervalMs())
}

system.InitCollector(config.SystemStatCollectIntervalMs())
if config.UseCacheTime() {
util.StartTimeTicker()
}
Expand Down
9 changes: 7 additions & 2 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func OverrideConfigFromEnvAndInitLog() error {
return err
}

defer logging.Infof("effective global config: %+v", *globalCfg)
// Configured Logger is the highest priority
if configLogger := Logger(); configLogger != nil {
err = logging.ResetGlobalLogger(configLogger)
Expand All @@ -59,8 +60,12 @@ func OverrideConfigFromEnvAndInitLog() error {
}
return nil
}
err = initializeLogConfig(LogBaseDir(), LogUsePid())
if err != nil {

logDir := LogBaseDir()
if len(logDir) == 0 {
logDir = GetDefaultLogDir()
}
if err := initializeLogConfig(logDir, LogUsePid()); err != nil {
return err
}
logging.Infof("App name resolved: %s", AppName())
Expand Down
16 changes: 12 additions & 4 deletions core/config/entity.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import (
"encoding/json"
"fmt"

"github.com/alibaba/sentinel-golang/logging"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -30,7 +33,7 @@ type SentinelConfig struct {

// LogConfig represent the configuration of logging in Sentinel.
type LogConfig struct {
// logger indicates that using logger to replace default logging.
// Logger indicates that using logger to replace default logging.
Logger logging.Logger
// Dir represents the log directory path.
Dir string
Expand Down Expand Up @@ -114,12 +117,17 @@ func checkConfValid(conf *SentinelConfig) error {
if mc.SingleFileMaxSize <= 0 {
return errors.New("Illegal metric log globalCfg: singleFileMaxSize <= 0")
}
if conf.Stat.System.CollectIntervalMs == 0 {
return errors.New("Bad system stat globalCfg: collectIntervalMs = 0")
}
return nil
}

func (entity *Entity) String() string {
e, err := json.Marshal(entity)
if err != nil {
return fmt.Sprintf("%+v", *entity)
}
return string(e)
}

func (entity *Entity) AppName() string {
return entity.Sentinel.App.Name
}
Expand Down
6 changes: 5 additions & 1 deletion core/log/metric/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ func NewDefaultMetricLogWriterOfApp(maxSize uint64, maxFileAmount uint32, appNam
}
_, offset := time.Now().Zone()

baseDir := util.AddPathSeparatorIfAbsent(config.LogBaseDir())
logDir := config.LogBaseDir()
if len(logDir) == 0 {
logDir = config.GetDefaultLogDir()
}
baseDir := util.AddPathSeparatorIfAbsent(logDir)
baseFilename := FormMetricFileName(appName, config.LogUsePid())

writer := &DefaultMetricLogWriter{
Expand Down
6 changes: 3 additions & 3 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const (
)

const (
DefaultNamespace = "default"
defaultNamespace = "sentinel-go"
// RecordLogFileName represents the default file name of the record log.
RecordLogFileName = "sentinel-record.log"
DefaultDirName = "logs" + string(os.PathSeparator) + "csp" + string(os.PathSeparator)
)

var (
globalLogLevel = InfoLevel
globalLogger = NewConsoleLogger(DefaultNamespace)
globalLogger = NewConsoleLogger(defaultNamespace)
)

func GetGlobalLoggerLevel() Level {
Expand Down Expand Up @@ -64,7 +64,7 @@ func NewSimpleFileLogger(filepath, namespace string, flag int) (Logger, error) {
return &DefaultLogger{
log: log.New(logFile, "", flag),
namespace: namespace,
}, err
}, nil
}

type Logger interface {
Expand Down

0 comments on commit 1c01bbd

Please sign in to comment.