-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
384 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.