github.com/shuguocloud/go-zero@v1.3.0/core/service/serviceconf.go (about) 1 package service 2 3 import ( 4 "log" 5 6 "github.com/shuguocloud/go-zero/core/load" 7 "github.com/shuguocloud/go-zero/core/logx" 8 "github.com/shuguocloud/go-zero/core/prometheus" 9 "github.com/shuguocloud/go-zero/core/stat" 10 "github.com/shuguocloud/go-zero/core/trace" 11 ) 12 13 const ( 14 // DevMode means development mode. 15 DevMode = "dev" 16 // TestMode means test mode. 17 TestMode = "test" 18 // RtMode means regression test mode. 19 RtMode = "rt" 20 // PreMode means pre-release mode. 21 PreMode = "pre" 22 // ProMode means production mode. 23 ProMode = "pro" 24 ) 25 26 // A ServiceConf is a service config. 27 type ServiceConf struct { 28 Name string 29 Log logx.LogConf 30 Mode string `json:",default=pro,options=dev|test|rt|pre|pro"` 31 MetricsUrl string `json:",optional"` 32 Prometheus prometheus.Config `json:",optional"` 33 Telemetry trace.Config `json:",optional"` 34 } 35 36 // MustSetUp sets up the service, exits on error. 37 func (sc ServiceConf) MustSetUp() { 38 if err := sc.SetUp(); err != nil { 39 log.Fatal(err) 40 } 41 } 42 43 // SetUp sets up the service. 44 func (sc ServiceConf) SetUp() error { 45 if len(sc.Log.ServiceName) == 0 { 46 sc.Log.ServiceName = sc.Name 47 } 48 if err := logx.SetUp(sc.Log); err != nil { 49 return err 50 } 51 52 sc.initMode() 53 prometheus.StartAgent(sc.Prometheus) 54 55 if len(sc.Telemetry.Name) == 0 { 56 sc.Telemetry.Name = sc.Name 57 } 58 trace.StartAgent(sc.Telemetry) 59 60 if len(sc.MetricsUrl) > 0 { 61 stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl)) 62 } 63 64 return nil 65 } 66 67 func (sc ServiceConf) initMode() { 68 switch sc.Mode { 69 case DevMode, TestMode, RtMode, PreMode: 70 load.Disable() 71 stat.SetReporter(nil) 72 } 73 }