conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package conf
  2. import (
  3. "errors"
  4. "time"
  5. "go-common/app/service/ops/log-agent/pkg/flowmonitor"
  6. "go-common/library/log"
  7. "go-common/app/service/ops/log-agent/pkg/limit"
  8. "go-common/app/service/ops/log-agent/conf/configcenter"
  9. "go-common/app/service/ops/log-agent/pkg/httpstream"
  10. "go-common/app/service/ops/log-agent/pkg/lancermonitor"
  11. "go-common/app/service/ops/log-agent/pipeline/hostlogcollector"
  12. "go-common/app/service/ops/log-agent/pipeline/dockerlogcollector"
  13. "go-common/library/naming/discovery"
  14. "github.com/BurntSushi/toml"
  15. )
  16. const (
  17. config = "agent.toml"
  18. )
  19. var (
  20. // Conf conf
  21. Conf = &Config{}
  22. )
  23. type Config struct {
  24. // discovery
  25. Discovery *discovery.Config `toml:"discovery"`
  26. // log
  27. Log *log.Config `toml:"log"`
  28. // flow monitor
  29. Flowmonitor *flowmonitor.Config `toml:"flowmonitor"`
  30. // limit
  31. Limit *limit.LimitConf `toml:"limit"`
  32. // debug
  33. DebugAddr string `toml:"debugAddr"`
  34. // httpstream
  35. HttpStream *httpstream.Config `toml:"httpstream"`
  36. // lancermonitor
  37. LancerMonitor *lancermonitor.Config `toml:"lancermonitor"`
  38. // hostlogcollector
  39. HostLogCollector *hostlogcollector.Config `toml:"hostlogcollector"`
  40. // docker log collector
  41. DockerLogCollector *dockerlogcollector.Config `toml:"dockerLogCollector"`
  42. }
  43. func (c *Config) ConfigValidate() (error) {
  44. if c == nil {
  45. return errors.New("config of log agent can't be nil")
  46. }
  47. if c.DockerLogCollector == nil {
  48. c.DockerLogCollector = new(dockerlogcollector.Config)
  49. }
  50. if c.HostLogCollector == nil {
  51. c.HostLogCollector = new(hostlogcollector.Config)
  52. }
  53. return nil
  54. }
  55. // initConfig init config
  56. func Init() (err error) {
  57. configcenter.InitConfigCenter()
  58. if err = readConfig(); err != nil {
  59. return
  60. }
  61. go func() {
  62. currentVersion := configcenter.Version
  63. for {
  64. if currentVersion != configcenter.Version {
  65. log.Info("lancer route config reload")
  66. if err := readConfig(); err != nil {
  67. log.Error("lancer route config reload error (%v", err)
  68. }
  69. currentVersion = configcenter.Version
  70. }
  71. time.Sleep(time.Second)
  72. }
  73. }()
  74. return Conf.ConfigValidate()
  75. }
  76. //// readConfig read config from config center
  77. func readConfig() (err error) {
  78. var (
  79. ok bool
  80. value string
  81. tmpConfig *Config
  82. )
  83. //config
  84. if value, ok = configcenter.Client.Value(config); !ok {
  85. return errors.New("failed to get agent.toml")
  86. }
  87. if _, err = toml.Decode(value, &tmpConfig); err != nil {
  88. return err
  89. }
  90. Conf = tmpConfig
  91. return
  92. }