agentconfig.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package conf
  2. import (
  3. "fmt"
  4. "github.com/BurntSushi/toml"
  5. "go-common/library/conf"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _agentConfigKey = "dapper-agent.toml"
  10. )
  11. // LoadAgentConfig LoadAgentConfig
  12. func LoadAgentConfig() (*AgentConfig, error) {
  13. if confPath != "" {
  14. cfg := new(AgentConfig)
  15. _, err := toml.DecodeFile(confPath, cfg)
  16. return cfg, err
  17. }
  18. return remoteAgentConfig()
  19. }
  20. // AgentConfig config for dapper agent
  21. type AgentConfig struct {
  22. Servers []string `toml:"servers"`
  23. Log *log.Config `toml:"log"`
  24. Queue *QueueConfig `toml:"queue"`
  25. UDPCollect UDPCollectConfig `toml:"udp_collect"`
  26. }
  27. // QueueConfig internal queue config
  28. type QueueConfig struct {
  29. // queue local stroage path
  30. MemBuckets int `toml:"mem_buckets"`
  31. BucketBytes int `toml:"bucket_bytes"`
  32. CacheDir string `toml:"cache_dir"`
  33. }
  34. // UDPCollectConfig collect config
  35. type UDPCollectConfig struct {
  36. Workers int `toml:"workers"`
  37. Addr string `toml:"addr"`
  38. }
  39. func remoteAgentConfig() (*AgentConfig, error) {
  40. client, err := conf.New()
  41. if err != nil {
  42. return nil, fmt.Errorf("new config center client error: %s", err)
  43. }
  44. data, ok := client.Value2(_agentConfigKey)
  45. if !ok {
  46. return nil, fmt.Errorf("load config center error key %s not found", _agentConfigKey)
  47. }
  48. cfg := new(AgentConfig)
  49. _, err = toml.Decode(data, cfg)
  50. if err != nil {
  51. return nil, fmt.Errorf("could not decode config file %s, error: %s", _agentConfigKey, err)
  52. }
  53. go func() {
  54. for range client.Event() {
  55. // ignore config change event
  56. }
  57. }()
  58. return cfg, nil
  59. }