config.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package trace
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/pkg/errors"
  8. "go-common/library/conf/dsn"
  9. xtime "go-common/library/time"
  10. )
  11. var _traceDSN = "unixgram:///var/run/dapper-collect/dapper-collect.sock"
  12. func init() {
  13. if v := os.Getenv("TRACE"); v != "" {
  14. _traceDSN = v
  15. }
  16. flag.StringVar(&_traceDSN, "trace", _traceDSN, "trace report dsn, or use TRACE env.")
  17. }
  18. // Config config.
  19. type Config struct {
  20. // Report network e.g. unixgram, tcp, udp
  21. Network string `dsn:"network"`
  22. // For TCP and UDP networks, the addr has the form "host:port".
  23. // For Unix networks, the address must be a file system path.
  24. Addr string `dsn:"address"`
  25. // DEPRECATED
  26. Proto string `dsn:"network"`
  27. // DEPRECATED
  28. Chan int `dsn:"query.chan,"`
  29. // Report timeout
  30. Timeout xtime.Duration `dsn:"query.timeout,200ms"`
  31. // DisableSample
  32. DisableSample bool `dsn:"query.disable_sample"`
  33. // probabilitySampling
  34. Probability float32 `dsn:"-"`
  35. // ProtocolVersion
  36. ProtocolVersion int32 `dsn:"query.protocol_version,2"`
  37. }
  38. func parseDSN(rawdsn string) (*Config, error) {
  39. d, err := dsn.Parse(rawdsn)
  40. if err != nil {
  41. return nil, errors.Wrapf(err, "trace: invalid dsn: %s", rawdsn)
  42. }
  43. cfg := new(Config)
  44. if _, err = d.Bind(cfg); err != nil {
  45. return nil, errors.Wrapf(err, "trace: invalid dsn: %s", rawdsn)
  46. }
  47. return cfg, nil
  48. }
  49. // TracerFromEnvFlag new tracer from env and flag
  50. func TracerFromEnvFlag() (Tracer, error) {
  51. cfg, err := parseDSN(_traceDSN)
  52. if err != nil {
  53. return nil, err
  54. }
  55. report := newReport(cfg.Network, cfg.Addr, time.Duration(cfg.Timeout), cfg.ProtocolVersion)
  56. serviceName := serviceNameFromEnv()
  57. return newTracer(serviceName, report, cfg), nil
  58. }
  59. // Init 兼容以前的 Init 写法
  60. func Init(cfg *Config) {
  61. serviceName := serviceNameFromEnv()
  62. if cfg != nil {
  63. // NOTE compatible proto field
  64. cfg.Network = cfg.Proto
  65. fmt.Fprintf(os.Stderr, "[deprecated] trace.Init() with conf is Deprecated, argument will be ignored. please use flag -trace or env TRACE to configure trace.\n")
  66. report := newReport(cfg.Network, cfg.Addr, time.Duration(cfg.Timeout), cfg.ProtocolVersion)
  67. SetGlobalTracer(newTracer(serviceName, report, cfg))
  68. return
  69. }
  70. // paser config from env
  71. cfg, err := parseDSN(_traceDSN)
  72. if err != nil {
  73. panic(fmt.Errorf("parse trace dsn error: %s", err))
  74. }
  75. report := newReport(cfg.Network, cfg.Addr, time.Duration(cfg.Timeout), cfg.ProtocolVersion)
  76. // disable sample if uat env
  77. cfg.DisableSample = isUATEnv()
  78. SetGlobalTracer(newTracer(serviceName, report, cfg))
  79. }