Skip to content

Commit

Permalink
Merge pull request #625 from signal18/log-management
Browse files Browse the repository at this point in the history
manage graphite log level
  • Loading branch information
svaroqui committed Jun 6, 2024
2 parents c9d5ed3 + 9621294 commit 3abd2df
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 63 deletions.
21 changes: 21 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ type Config struct {
LogTopologyLevel int `mapstructure:"log-topology-level" toml:"log-topology-level" json:"logTopologyLevel"`
LogProxy bool `mapstructure:"log-proxy" toml:"log-proxy" json:"logProxy"`
LogProxyLevel int `mapstructure:"log-proxy-level" toml:"log-proxy-level" json:"logProxyLevel"`
LogGraphite bool `mapstructure:"log-graphite" toml:"log-graphite" json:"logGraphite"`
LogGraphiteLevel int `mapstructure:"log-graphite-level" toml:"log-graphite-level" json:"logGraphiteLevel"`
User string `mapstructure:"db-servers-credential" toml:"db-servers-credential" json:"dbServersCredential"`
Hosts string `mapstructure:"db-servers-hosts" toml:"db-servers-hosts" json:"dbServersHosts"`
HostsDelayed string `mapstructure:"replication-delayed-hosts" toml:"replication-delayed-hosts" json:"replicationDelayedHosts"`
Expand Down Expand Up @@ -901,6 +903,7 @@ const (
ConstLogModHAProxy = 12
ConstLogModProxyJanitor = 13
ConstLogModMaxscale = 14
ConstLogModGraphite = 15
)

func (conf *Config) GetSecrets() map[string]Secret {
Expand Down Expand Up @@ -1880,6 +1883,11 @@ func (conf *Config) IsEligibleForPrinting(module int, level string) bool {
return conf.MxsLogLevel >= lvl
}
break
case module == ConstLogModGraphite:
if conf.LogGraphite {
return conf.LogGraphiteLevel >= lvl
}
break
}
}

Expand All @@ -1889,3 +1897,16 @@ func (conf *Config) IsEligibleForPrinting(module int, level string) bool {
func (conf *Config) SetLogOutput(out io.Writer) {
log.SetOutput(out)
}

func (conf *Config) ToLogrusLevel(l int) log.Level {
switch l {
case 2:
return log.WarnLevel
case 3:
return log.InfoLevel
case 4:
return log.DebugLevel
}
//Always return at least error level to make sure Logger not exit
return log.ErrorLevel
}
8 changes: 5 additions & 3 deletions graphite/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package cache

/*
Expand All @@ -14,6 +13,7 @@ import (

"github.com/signal18/replication-manager/graphite/helper"
"github.com/signal18/replication-manager/graphite/points"
"github.com/sirupsen/logrus"
)

type WriteStrategy int
Expand All @@ -40,7 +40,8 @@ type Cache struct {

writeoutQueue *WriteoutQueue

xlog atomic.Value // io.Writer
xlog atomic.Value // io.Writer
logger *logrus.Logger

stat struct {
size int32 // changing via atomic
Expand All @@ -61,9 +62,10 @@ type Shard struct {
}

// Creates a new cache instance
func New() *Cache {
func New(logger *logrus.Logger) *Cache {
c := &Cache{
data: make([]*Shard, shardCount),
logger: logger,
writeStrategy: Noop,
maxSize: 1000000,
}
Expand Down
6 changes: 2 additions & 4 deletions graphite/cache/writeout_queue.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

package cache

import (
"sync"
"time"

"github.com/sirupsen/logrus"
"github.com/signal18/replication-manager/graphite/points"
)

Expand Down Expand Up @@ -37,10 +35,10 @@ func (q *WriteoutQueue) makeRebuildCallback(nextRebuildTime time.Time) func(chan
// next rebuild
nextRebuildOnce.Do(func() {
now := time.Now()
logrus.Debugf("nextRebuildOnce.Do: %#v %#v", now.String(), nextRebuildTime.String())
q.cache.logger.Debugf("nextRebuildOnce.Do: %#v %#v", now.String(), nextRebuildTime.String())
if now.Before(nextRebuildTime) {
sleepTime := nextRebuildTime.Sub(now)
logrus.Debugf("sleep %s before rebuild", sleepTime.String())
q.cache.logger.Debugf("sleep %s before rebuild", sleepTime.String())

select {
case <-time.After(sleepTime):
Expand Down
27 changes: 15 additions & 12 deletions graphite/carbon/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ type App struct {
Persister *persister.Whisper
Carbonserver *carbonserver.CarbonserverListener
Collector *Collector // (!!!) Should be re-created on every change config/modules
Logger *logrus.Logger
exit chan bool
}

// New App instance
func New(configFilename string) *App {
func New(configFilename string, logger *logrus.Logger) *App {
app := &App{
ConfigFilename: configFilename,
Config: NewConfig(),
Logger: logger,
exit: make(chan bool),
}
return app
Expand Down Expand Up @@ -143,31 +145,31 @@ func (app *App) stopListeners() {
if app.TCP != nil {
app.TCP.Stop()
app.TCP = nil
Log.Debug("[tcp] finished")
app.Logger.Debug("[tcp] finished")
}

if app.Pickle != nil {
app.Pickle.Stop()
app.Pickle = nil
Log.Debug("[pickle] finished")
app.Logger.Debug("[pickle] finished")
}

if app.UDP != nil {
app.UDP.Stop()
app.UDP = nil
Log.Debug("[udp] finished")
app.Logger.Debug("[udp] finished")
}

if app.CarbonLink != nil {
app.CarbonLink.Stop()
app.CarbonLink = nil
Log.Debug("[carbonlink] finished")
app.Logger.Debug("[carbonlink] finished")
}

if app.Carbonserver != nil {
app.Carbonserver.Stop()
app.Carbonserver = nil
Log.Debug("[carbonserver] finished")
app.Logger.Debug("[carbonserver] finished")
}
}

Expand All @@ -177,25 +179,25 @@ func (app *App) stopAll() {
if app.Persister != nil {
app.Persister.Stop()
app.Persister = nil
Log.Debug("[persister] finished")
app.Logger.Debug("[persister] finished")
}

if app.Cache != nil {
app.Cache.Stop()
app.Cache = nil
Log.Debug("[cache] finished")
app.Logger.Debug("[cache] finished")
}

if app.Collector != nil {
app.Collector.Stop()
app.Collector = nil
Log.Debug("[stat] finished")
app.Logger.Debug("[stat] finished")
}

if app.exit != nil {
close(app.exit)
app.exit = nil
Log.Debug("[app] close(exit)")
app.Logger.Debug("[app] close(exit)")
}
}

Expand All @@ -214,6 +216,7 @@ func (app *App) startPersister() {
app.Config.Whisper.Aggregation,
app.Cache.WriteoutQueue().GetNotConfirmed,
app.Cache.Confirm,
app.Logger,
)
p.SetMaxUpdatesPerSecond(app.Config.Whisper.MaxUpdatesPerSecond)
p.SetSparse(app.Config.Whisper.Sparse)
Expand All @@ -240,7 +243,7 @@ func (app *App) Start() (err error) {

runtime.GOMAXPROCS(conf.Common.MaxCPU)

core := cache.New()
core := cache.New(app.Logger)
core.SetMaxSize(conf.Cache.MaxSize)
core.SetWriteStrategy(conf.Cache.WriteStrategy)

Expand Down Expand Up @@ -300,7 +303,7 @@ func (app *App) Start() (err error) {
return
}

carbonserver.Log = Log
carbonserver.Log = app.Logger
carbonserver := carbonserver.NewCarbonserverListener(core.Get)
carbonserver.SetWhisperData(conf.Whisper.DataDir)
carbonserver.SetMaxGlobs(conf.Carbonserver.MaxGlobs)
Expand Down
13 changes: 11 additions & 2 deletions graphite/carbonapi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package graphite

import (
Expand All @@ -17,6 +16,7 @@ import (
"strings"
"time"

"github.com/signal18/replication-manager/config"
pb "github.com/signal18/replication-manager/graphite/carbonzipper/carbonzipperpb"
"github.com/signal18/replication-manager/graphite/carbonzipper/mlog"
"github.com/signal18/replication-manager/graphite/carbonzipper/mstats"
Expand Down Expand Up @@ -658,7 +658,16 @@ func usageHandler(w http.ResponseWriter, r *http.Request) {
w.Write(usageMsg)
}

func RunCarbonApi(z string, port int, l int, cacheType string, mc string, memsize int, cpus int, tz string, logdir string) {
func RunCarbonApi(conf *config.Config) {
var z string = "http://0.0.0.0:" + strconv.Itoa(conf.GraphiteCarbonServerPort)
var port int = conf.GraphiteCarbonApiPort
var l int = 20
var cacheType string = "mem"
var mc string = ""
var memsize int = 200
var cpus int = 0
var tz string = ""
var logdir string = conf.WorkingDir

interval := 60 * time.Second
graphiteHost := ""
Expand Down
2 changes: 1 addition & 1 deletion graphite/carbonserver/carbonserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2013-2016 Fabian Groffen, Damian Gryski, Vladimir Smirnov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down
61 changes: 36 additions & 25 deletions graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"syscall"
"time"

"github.com/signal18/replication-manager/config"
"github.com/signal18/replication-manager/graphite/carbon"
logging "github.com/signal18/replication-manager/graphite/logging"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -208,73 +209,83 @@ func httpServe(addr string) (func(), error) {
return func() { listener.Close() }, nil
}

func RunCarbon(ShareDir string, DataDir string, GraphiteCarbonPort int, GraphiteCarbonLinkPort int, GraphiteCarbonPicklePort int, GraphiteCarbonPprofPort int, GraphiteCarbonServerPort int) error {
func RunCarbon(conf *config.Config) error {
var err error
var loglevel logrus.Level

if conf.LogGraphite {
//Log based on repman config
loglevel = conf.ToLogrusLevel(conf.LogGraphiteLevel)
} else {
//Only log errors
loglevel = logrus.ErrorLevel
}

log.SetLevel(loglevel)
logging.Log = log

input, err := ioutil.ReadFile(ShareDir + "/carbon.conf.template")
input, err := ioutil.ReadFile(conf.ShareDir + "/carbon.conf.template")
if err != nil {
return err
}

output := bytes.Replace(input, []byte("{{.schemas}}"), []byte(ShareDir+"/schemas.conf"), -1)
fullpath, err := filepath.Abs(DataDir + "/graphite")
output := bytes.Replace(input, []byte("{{.schemas}}"), []byte(conf.ShareDir+"/schemas.conf"), -1)
fullpath, err := filepath.Abs(conf.WorkingDir + "/graphite")

output2 := bytes.Replace(output, []byte("{{.datadir}}"), []byte(fullpath), -1)
output3 := bytes.Replace(output2, []byte("{{.graphitecarbonport}}"), []byte(strconv.Itoa(GraphiteCarbonPort)), -1)
output4 := bytes.Replace(output3, []byte("{{.graphitecarbonlinkport}}"), []byte(strconv.Itoa(GraphiteCarbonLinkPort)), -1)
output5 := bytes.Replace(output4, []byte("{{.graphitecarbonpickleport}}"), []byte(strconv.Itoa(GraphiteCarbonPicklePort)), -1)
output6 := bytes.Replace(output5, []byte("{{.graphitecarbonpprofport}}"), []byte(strconv.Itoa(GraphiteCarbonPprofPort)), -1)
output7 := bytes.Replace(output6, []byte("{{.graphitecarbonserverport}}"), []byte(strconv.Itoa(GraphiteCarbonServerPort)), -1)
output3 := bytes.Replace(output2, []byte("{{.graphitecarbonport}}"), []byte(strconv.Itoa(conf.GraphiteCarbonPort)), -1)
output4 := bytes.Replace(output3, []byte("{{.graphitecarbonlinkport}}"), []byte(strconv.Itoa(conf.GraphiteCarbonLinkPort)), -1)
output5 := bytes.Replace(output4, []byte("{{.graphitecarbonpickleport}}"), []byte(strconv.Itoa(conf.GraphiteCarbonPicklePort)), -1)
output6 := bytes.Replace(output5, []byte("{{.graphitecarbonpprofport}}"), []byte(strconv.Itoa(conf.GraphiteCarbonPprofPort)), -1)
output7 := bytes.Replace(output6, []byte("{{.graphitecarbonserverport}}"), []byte(strconv.Itoa(conf.GraphiteCarbonServerPort)), -1)

if err = ioutil.WriteFile(DataDir+"/carbon.conf", output7, 0666); err != nil {
if err = ioutil.WriteFile(conf.WorkingDir+"/carbon.conf", output7, 0666); err != nil {
fmt.Println(err)
os.Exit(1)
}

carbon.Log = log
app := carbon.New(DataDir + "/carbon.conf")
// carbon.Log = log
app := carbon.New(conf.WorkingDir+"/carbon.conf", log)

if err = app.ParseConfig(); err != nil {
return err
}

app.Config.Common.Logfile = DataDir + "/carbon.log"
// log.Fatal(app.Config.Whisper.SchemasFilename)
app.Config.Common.Logfile = conf.WorkingDir + "/carbon.log"
// graphite.Log.Fatal(app.Config.Whisper.SchemasFilename)
cfg := app.Config

var runAsUser *user.User
if cfg.Common.User != "" {
runAsUser, err = user.Lookup(cfg.Common.User)
if err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
}

if err := logging.SetLevel(cfg.Common.LogLevel); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}

if err := logging.PrepareFile(cfg.Common.Logfile, runAsUser); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}

if err := logging.SetFile(cfg.Common.Logfile); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}

if cfg.Pprof.Enabled {
_, err = httpServe(cfg.Pprof.Listen)
if err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
}

if err = app.Start(); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
} else {
log.Info("started")
logging.Log.Info("started")
}

go func() {
Expand All @@ -291,17 +302,17 @@ func RunCarbon(ShareDir string, DataDir string, GraphiteCarbonPort int, Graphite
signal.Notify(c, syscall.SIGHUP)
for {
<-c
log.Info("HUP received. Reload config")
logging.Log.Info("HUP received. Reload config")
if err := app.ReloadConfig(); err != nil {
log.Errorf("Config reload failed: %s", err.Error())
logging.Log.Errorf("Config reload failed: %s", err.Error())
} else {
log.Info("Config successfully reloaded")
logging.Log.Info("Config successfully reloaded")
}
}
}()

app.Loop()

log.Info("stopped")
logging.Log.Info("stopped")
return nil
}
Loading

0 comments on commit 3abd2df

Please sign in to comment.