flags.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package flags
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/coroot/coroot-node-agent/utils"
  6. "github.com/coroot/coroot-node-agent/utils/modelse"
  7. "github.com/jedib0t/go-pretty/v6/table"
  8. "gopkg.in/alecthomas/kingpin.v2"
  9. "os"
  10. "path"
  11. "strings"
  12. )
  13. var (
  14. ListenAddress = kingpin.Flag("listen", "Listen address - ip:port or :port").Default("0.0.0.0:80").Envar("LISTEN").String()
  15. CgroupRoot = kingpin.Flag("cgroupfs-root", "The mount point of the host cgroupfs root").Default("/sys/fs/cgroup").Envar("CGROUPFS_ROOT").String()
  16. DisableLogParsing = kingpin.Flag("disable-log-parsing", "Disable container log parsing").Default("false").Envar("DISABLE_LOG_PARSING").Bool()
  17. DisablePinger = kingpin.Flag("disable-pinger", "Don't ping upstreams").Default("false").Envar("DISABLE_PINGER").Bool()
  18. DisableL7Tracing = kingpin.Flag("disable-l7-tracing", "Disable L7 tracing").Default("false").Envar("DISABLE_L7_TRACING").Bool()
  19. DisableStackTracing = kingpin.Flag("disable-stack-tracing", "Disable stack tracing").Default("false").Envar("DISABLE_STACK_TRACING").Bool()
  20. DisableE2ETracing = kingpin.Flag("disable-e2e-tracing", "Disable e2e tracing").Default("false").Envar("DISABLE_E2E_TRACING").Bool()
  21. ExternalNetworksWhitelist = kingpin.Flag("track-public-network", "Allow track connections to the specified IP networks, all private networks are allowed by default (e.g., Y.Y.Y.Y/mask)").Envar("TRACK_PUBLIC_NETWORK").Strings()
  22. EphemeralPortRange = kingpin.Flag("ephemeral-port-range", "Destination and Listen TCP ports from this range will be skipped").Default("32768-60999").Envar("EPHEMERAL_PORT_RANGE").String()
  23. Provider = kingpin.Flag("provider", "`provider` label for `node_cloud_info` metric").Envar("PROVIDER").String()
  24. Region = kingpin.Flag("region", "`region` label for `node_cloud_info` metric").Envar("REGION").String()
  25. AvailabilityZone = kingpin.Flag("availability-zone", "`availability_zone` label for `node_cloud_info` metric").Envar("AVAILABILITY_ZONE").String()
  26. InstanceType = kingpin.Flag("instance-type", "`instance_type` label for `node_cloud_info` metric").Envar("INSTANCE_TYPE").String()
  27. InstanceLifeCycle = kingpin.Flag("instance-life-cycle", "`instance_life_cycle` label for `node_cloud_info` metric").Envar("INSTANCE_LIFE_CYCLE").String()
  28. LogPerSecond = kingpin.Flag("log-per-second", "The number of logs per second").Default("10.0").Envar("LOG_PER_SECOND").Float64()
  29. LogBurst = kingpin.Flag("log-burst", "The maximum number of tokens that can be consumed in a single call to allow").Default("100").Envar("LOG_BURST").Int()
  30. CollectorEndpoint = kingpin.Flag("collector-endpoint", "A base endpoint URL for metrics, traces, logs, and profiles").Envar("COLLECTOR_ENDPOINT").URL()
  31. ApiKey = kingpin.Flag("api-key", "Coroot API key").Envar("API_KEY").String()
  32. MetricsEndpoint = kingpin.Flag("metrics-endpoint", "The URL of the endpoint to send metrics to").Envar("METRICS_ENDPOINT").URL()
  33. TracesEndpoint = kingpin.Flag("traces-endpoint", "The URL of the endpoint to send traces to").Envar("TRACES_ENDPOINT").URL()
  34. ConfigEndpoint = kingpin.Flag("config-endpoint", "The URL of the endpoint to send traces to").Envar("CONFIG_ENDPOINT").Default("10.0.16.250:18080").String()
  35. LogsEndpoint = kingpin.Flag("logs-endpoint", "The URL of the endpoint to send logs to").Envar("LOGS_ENDPOINT").URL()
  36. ProfilesEndpoint = kingpin.Flag("profiles-endpoint", "The URL of the endpoint to send profiles to").Envar("PROFILES_ENDPOINT").URL()
  37. ScrapeInterval = kingpin.Flag("scrape-interval", "How often to gather metrics from the agent").Default("15s").Envar("SCRAPE_INTERVAL").Duration()
  38. WalDir = kingpin.Flag("wal-dir", "Path to where the agent stores data (e.g. the metrics Write-Ahead Log)").Default("/tmp/coroot-node-agent").Envar("WAL_DIR").String()
  39. DumpApps = kingpin.Flag("dump", "Dump app snap").Default("false").Bool()
  40. LogLevel = kingpin.Flag("log-level", "Log level").Envar("LOG_LEVEL").Default("info").String()
  41. )
  42. func GetString(fl *string) string {
  43. if fl == nil {
  44. return ""
  45. }
  46. return *fl
  47. }
  48. func init() {
  49. if strings.HasSuffix(os.Args[0], ".test") {
  50. return
  51. }
  52. kingpin.HelpFlag.Short('h').Hidden()
  53. kingpin.Parse()
  54. if *DumpApps {
  55. DumpTableFeatures()
  56. }
  57. if *CollectorEndpoint != nil {
  58. u := *CollectorEndpoint
  59. if *MetricsEndpoint == nil {
  60. *MetricsEndpoint = u.JoinPath("/v1/metrics")
  61. }
  62. if *TracesEndpoint == nil {
  63. *TracesEndpoint = u.JoinPath("/v1/traces")
  64. }
  65. if *LogsEndpoint == nil {
  66. *LogsEndpoint = u.JoinPath("/v1/logs")
  67. }
  68. if *ProfilesEndpoint == nil {
  69. *ProfilesEndpoint = u.JoinPath("/v1/profiles")
  70. }
  71. }
  72. if *MetricsEndpoint != nil {
  73. *ListenAddress = "127.0.0.1:10300"
  74. }
  75. }
  76. func DumpTableFeatures() {
  77. dumpPath := path.Join(utils.GetDefaultLogPath(), "memdump")
  78. fileName := path.Join(dumpPath, "app.snap")
  79. content, err := os.ReadFile(fileName)
  80. if err != nil {
  81. fmt.Println(err.Error())
  82. }
  83. s := make(map[uint32]modelse.AppStatusInfo)
  84. err = json.Unmarshal(content, &s)
  85. if err != nil {
  86. fmt.Println(err.Error())
  87. }
  88. t := table.NewWriter()
  89. for pid, info := range s {
  90. service := fmt.Sprintf("%s:%d", info.Sn, info.Sport)
  91. t.AppendRow(table.Row{
  92. pid,
  93. info.ProcName,
  94. info.AppName,
  95. info.Language,
  96. service,
  97. info.AppID,
  98. //info.AgentID,
  99. info.RegisterAt,
  100. info.UpdateAt,
  101. })
  102. }
  103. t.SetAutoIndex(true)
  104. t.AppendHeader(table.Row{
  105. "pid",
  106. "process",
  107. "app name",
  108. "code",
  109. "service",
  110. "app id",
  111. //"agent id",
  112. "register at",
  113. "update at",
  114. })
  115. fmt.Println(t.Render())
  116. os.Exit(0)
  117. }