Skip to content

Commit

Permalink
unify config
Browse files Browse the repository at this point in the history
  • Loading branch information
louyuting committed Feb 24, 2020
1 parent d9ac523 commit 64106d0
Show file tree
Hide file tree
Showing 25 changed files with 384 additions and 310 deletions.
50 changes: 30 additions & 20 deletions api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@ package api
import (
"fmt"
"github.com/alibaba/sentinel-golang/core/config"
"github.com/alibaba/sentinel-golang/core/constant"
"github.com/alibaba/sentinel-golang/core/log/metric"
"github.com/alibaba/sentinel-golang/core/system"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
"os"
)

// InitDefault initializes Sentinel using the configuration from system
// environment and the default value.
func InitDefault() error {
return initSentinel("", "")
return initSentinel("")
}

// Init loads Sentinel general configuration from the given YAML file
// and initializes Sentinel. Note that the logging module will be initialized
// using the configuration from system environment or the default value.
func Init(configPath string) error {
return initSentinel(configPath, "")
return initSentinel(configPath)
}

// InitWithLogDir initializes Sentinel logging module with the given directory.
// Then it loads Sentinel general configuration from the given YAML file
// and initializes Sentinel.
func InitWithLogDir(configPath string, logDir string) error {
return initSentinel(configPath, logDir)
}

func initSentinel(configPath string, logDir string) (err error) {
//
// The priority: ENV > yaml file > default configuration
func initSentinel(configPath string) (err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
Expand All @@ -39,23 +36,36 @@ func initSentinel(configPath string, logDir string) (err error) {
}
}()

// First we initialize the logging module.
if logDir == "" {
err = logging.InitializeLogConfigFromEnv()
} else {
err = logging.InitializeLogConfig(logDir, true)
//Firstly, get config file path
if util.IsBlank(configPath) {
// If the config file path is absent, Sentinel will try to resolve it from the system env.
configPath = os.Getenv(constant.ConfFilePathEnvKey)
}
if util.IsBlank(configPath) {
configPath = constant.DefaultConfigFilename
}
// load config from yaml file
// if use don't set config path, then use default config
err = config.LoadFromYamlFile(configPath)
if err != nil {
return err
}
// Initialize the general configuration.
err = config.InitConfigFromFile(configPath)
// Secondly, use variable from ENV to override config
err = config.OverrideFromSystemEnv()
if err != nil {
return err
}

metric.InitTask()
system.InitCollector(config.SystemStatCollectIntervalMs())
err = config.InitializeLogConfig(config.LogBaseDir(), config.LogUsePid())
if err != nil {
return err
}

initCoreComponents()
return err
}

func initCoreComponents() {
metric.InitTask()
system.InitCollector(config.SystemStatCollectIntervalMs())
}
17 changes: 0 additions & 17 deletions core/base/global_variable.go

This file was deleted.

163 changes: 163 additions & 0 deletions core/config/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package config

import (
"fmt"
"github.com/alibaba/sentinel-golang/core/constant"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"sync"
)

var (
globalCfg = NewDefaultConfig()
initLogOnce sync.Once
)

func LoadFromYamlFile(filePath string) error {
if filePath == constant.DefaultConfigFilename {
if _, err := os.Stat(constant.DefaultConfigFilename); err != nil {
//use default globalCfg.
return nil
}
}
_, err := os.Stat(filePath)
if err != nil && !os.IsExist(err){
return err
}
content, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
err = yaml.Unmarshal(content, globalCfg)
if err != nil {
return err
}
logging.GetDefaultLogger().Infof("Resolving Sentinel globalCfg from file: %s", filePath)
return checkValid(&(globalCfg.Sentinel))
}

func OverrideFromSystemEnv() error{
if appName := os.Getenv(constant.AppNameEnvKey); !util.IsBlank(appName) {
globalCfg.Sentinel.App.Name = appName
}

if appTypeStr := os.Getenv(constant.AppTypeEnvKey); !util.IsBlank(appTypeStr) {
appType, err := strconv.ParseInt(appTypeStr, 10, 32)
if err != nil {
return err
} else {
globalCfg.Sentinel.App.Type = int32(appType)
}
}

if addPidStr := os.Getenv(constant.LogNamePidEnvKey); !util.IsBlank(addPidStr) {
addPid, err := strconv.ParseBool(addPidStr)
if err != nil {
return err
} else {
globalCfg.Sentinel.Log.UsePid = addPid
}
}

if logDir := os.Getenv(constant.LogDirEnvKey); !util.IsBlank(logDir) {
if _, err := os.Stat(logDir); err != nil && !os.IsExist(err) {
return err
}
globalCfg.Sentinel.Log.Dir = logDir
}
return checkValid(&(globalCfg.Sentinel))
}

func InitializeLogConfig(logDir string, usePid bool) (err error) {
if logDir == "" {
return errors.New("Invalid empty log path")
}

initLogOnce.Do(func() {
if err = util.CreateDirIfNotExists(logDir); err != nil {
return
}
err = reconfigureRecordLogger(logDir, usePid)
})
return err
}

func reconfigureRecordLogger(logBaseDir string, withPid bool) error {
logDir := addSeparatorIfNeeded(logBaseDir)
filePath := logDir + logging.RecordLogFileName
if withPid {
filePath = filePath + ".pid" + strconv.Itoa(os.Getpid())
}

defaultLogger := logging.GetDefaultLogger()
if defaultLogger == nil {
return errors.New("Unexpected state: defaultLogger == nil")
}
logFile, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
return err
}

// Note: not thread-safe!
logging.ResetDefaultLogger(log.New(logFile, "", log.LstdFlags), logging.DefaultNamespace)
fmt.Println("INFO: log base directory is: " + logDir)

return nil
}

func GetDefaultLogDir() string {
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return addSeparatorIfNeeded(home) + logging.DefaultDirName
}

func addSeparatorIfNeeded(path string) string {
s := string(os.PathSeparator)
if !strings.HasSuffix(path, s) {
return path + s
}
return path
}



func AppName() string {
return globalCfg.Sentinel.App.Name
}

func AppType() int32 {
return globalCfg.Sentinel.App.Type
}

func LogBaseDir() string {
return globalCfg.Sentinel.Log.Dir
}

func LogUsePid() bool {
return globalCfg.Sentinel.Log.UsePid
}

func MetricLogFlushIntervalSec() uint32 {
return globalCfg.Sentinel.Log.Metric.FlushIntervalSec
}

func MetricLogSingleFileMaxSize() uint64 {
return globalCfg.Sentinel.Log.Metric.SingleFileMaxSize
}

func MetricLogMaxFileAmount() uint32 {
return globalCfg.Sentinel.Log.Metric.MaxFileCount
}

func SystemStatCollectIntervalMs() uint32 {
return globalCfg.Sentinel.Stat.System.CollectIntervalMs
}
68 changes: 68 additions & 0 deletions core/config/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package config

import (
"github.com/alibaba/sentinel-golang/core/constant"
"os"
"testing"
)

func TestLoadFromYamlFile(t *testing.T) {
type args struct {
filePath string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "TestLoadFromYamlFile",
args: args{
filePath: "testdata/sentinel.yml",
},
wantErr: false,
},
{
name: "TestLoadFromYamlFile",
args: args{
filePath: "testdata/sentinel.yml.1",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := LoadFromYamlFile(tt.args.filePath); (err != nil) != tt.wantErr {
t.Errorf("LoadFromYamlFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestOverrideFromSystemEnv(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{
name: "TestOverrideFromSystemEnv",
wantErr: false,
},
}
err := LoadFromYamlFile("testdata/sentinel.yml")
if err != nil {
t.Errorf("Fail to initialize data.")
}
_ = os.Setenv(constant.AppNameEnvKey, "app-name")
_ = os.Setenv(constant.AppTypeEnvKey, "1")
_ = os.Setenv(constant.LogDirEnvKey, "testdata/sentinel.yml.2")
_ = os.Setenv(constant.LogNamePidEnvKey, "true")

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := OverrideFromSystemEnv(); (err != nil) != tt.wantErr {
t.Errorf("OverrideFromSystemEnv() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading

0 comments on commit 64106d0

Please sign in to comment.