config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v3"
  5. "os"
  6. "time"
  7. )
  8. type AggregateConfig struct {
  9. ApdexGoodMS int `yaml:"apdexGoodMS"`
  10. ApdexFairMS int `yaml:"apdexFairMS"`
  11. LoadTimeoutMS int `yaml:"loadTimeoutMS"`
  12. }
  13. func (c AggregateConfig) LoadTimeout() time.Duration {
  14. return time.Duration(c.LoadTimeoutMS) * time.Millisecond
  15. }
  16. type ClickhouseConfig struct {
  17. Servers []string `yaml:"servers"`
  18. Database string `yaml:"database"`
  19. User string `yaml:"user"`
  20. Pwd string `yaml:"pwd"`
  21. }
  22. type JobConfig struct {
  23. TraceIDChannelSize int `yaml:"traceIDChannelSize"`
  24. FlatTraceChannelSize int `yaml:"flatTraceChannelSize"`
  25. TimeRangeChannelSize int `yaml:"timeRangeChannelSize"`
  26. }
  27. type KafkaSinkConfig struct {
  28. Parallelism int `yaml:"parallelism"`
  29. Brokers []string `yaml:"brokers"`
  30. Topic string
  31. }
  32. type TraceLoaderConfig struct {
  33. Parallelism int `yaml:"parallelism"`
  34. }
  35. type PushTaskConfig struct {
  36. RunIntervalSeconds int `yaml:"runIntervalSeconds"`
  37. LatencyMinutes int `yaml:"latencyMinutes"`
  38. DebugQueryDurationMinutes int `yaml:"debugQueryDurationMinutes"`
  39. }
  40. func (c PushTaskConfig) RunInterval() time.Duration {
  41. return time.Duration(c.RunIntervalSeconds) * time.Second
  42. }
  43. func (c PushTaskConfig) LatencyDuration() time.Duration {
  44. return time.Duration(c.LatencyMinutes) * time.Minute
  45. }
  46. func (c PushTaskConfig) GetDebugQueryDuration() time.Duration {
  47. return time.Duration(c.DebugQueryDurationMinutes) * time.Minute
  48. }
  49. type RocketMQConfig struct {
  50. Parallelism int `yaml:"parallelism"`
  51. TopoTopic string `yaml:"topoTopic"`
  52. MetricTopic string `yaml:"metricTopic"`
  53. TopoGroupID string `yaml:"topoGroupID"`
  54. MetricGroupID string `yaml:"metricGroupID"`
  55. }
  56. type Config struct {
  57. Aggregate AggregateConfig `yaml:"aggregate"`
  58. TraceLoader TraceLoaderConfig `yaml:"traceLoader"`
  59. Job JobConfig `yaml:"job"`
  60. KafkaSink KafkaSinkConfig `yaml:"kafkaSink"`
  61. Clickhouse ClickhouseConfig `yaml:"clickhouse"`
  62. AppAlias2CmdbID map[string]string `yaml:"appAlias2CmdbID"`
  63. PushTaskConfig PushTaskConfig `yaml:"pushTaskConfig"`
  64. IncludeApps []string `yaml:"includeApps"`
  65. RocketMq RocketMQConfig `yaml:"rocketMq"`
  66. }
  67. func (c Config) findServiceCmdbID(serviceName string, serviceName2AppAlias map[string]string) string {
  68. alias, ok := serviceName2AppAlias[serviceName]
  69. if !ok {
  70. return ""
  71. }
  72. cmdbID, findCmdb := c.AppAlias2CmdbID[alias]
  73. if !findCmdb {
  74. return ""
  75. }
  76. return cmdbID
  77. }
  78. func (c Config) GetSNRelation() *ServiceNameAppAliasCmdbIDRelation {
  79. return &ServiceNameAppAliasCmdbIDRelation{
  80. AppAlias2CmdbID: c.AppAlias2CmdbID,
  81. }
  82. }
  83. func parseConfig(cp string) (Config, error) {
  84. var c Config
  85. configContents, err := os.ReadFile(cp)
  86. if err != nil {
  87. return c, fmt.Errorf("read %s: %w", cp, err)
  88. }
  89. if errUn := yaml.Unmarshal(configContents, &c); errUn != nil {
  90. return c, fmt.Errorf("unmarshal %s: %w", string(configContents), errUn)
  91. }
  92. return c, nil
  93. }
  94. type ServiceNameAppAliasCmdbIDRelation struct {
  95. AppAlias2CmdbID map[string]string
  96. sn2AA map[string]string
  97. }
  98. func (sn ServiceNameAppAliasCmdbIDRelation) GetCmdbIDFromAppAlias(appAlias string) (string, bool) {
  99. aa, findAA := sn.AppAlias2CmdbID[appAlias]
  100. return aa, findAA
  101. }