go-zero/core/service/serviceconf.go

78 lines
1.8 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package service
import (
"github.com/zeromicro/go-zero/core/load"
"github.com/zeromicro/go-zero/core/logx"
2022-09-07 11:18:52 +08:00
"github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/prometheus"
"github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/core/trace"
"github.com/zeromicro/go-zero/internal/devserver"
2020-07-26 17:09:05 +08:00
)
const (
// DevMode means development mode.
DevMode = "dev"
// TestMode means test mode.
2020-07-26 17:09:05 +08:00
TestMode = "test"
2021-03-30 20:53:35 +08:00
// RtMode means regression test mode.
2021-03-30 20:45:55 +08:00
RtMode = "rt"
// PreMode means pre-release mode.
PreMode = "pre"
// ProMode means production mode.
ProMode = "pro"
2020-07-26 17:09:05 +08:00
)
// A ServiceConf is a service config.
2020-07-26 17:09:05 +08:00
type ServiceConf struct {
2021-09-05 22:43:15 +08:00
Name string
Log logx.LogConf
Mode string `json:",default=pro,options=dev|test|rt|pre|pro"`
MetricsUrl string `json:",optional"`
// Deprecated: please use DevServer
Prometheus prometheus.Config `json:",optional"`
Telemetry trace.Config `json:",optional"`
DevServer devserver.Config `json:",optional"`
2020-07-26 17:09:05 +08:00
}
// MustSetUp sets up the service, exits on error.
2020-07-26 17:09:05 +08:00
func (sc ServiceConf) MustSetUp() {
logx.Must(sc.SetUp())
2020-07-26 17:09:05 +08:00
}
// SetUp sets up the service.
2020-07-26 17:09:05 +08:00
func (sc ServiceConf) SetUp() error {
if len(sc.Log.ServiceName) == 0 {
sc.Log.ServiceName = sc.Name
}
if err := logx.SetUp(sc.Log); err != nil {
return err
}
sc.initMode()
prometheus.StartAgent(sc.Prometheus)
if len(sc.Telemetry.Name) == 0 {
sc.Telemetry.Name = sc.Name
}
trace.StartAgent(sc.Telemetry)
proc.AddShutdownListener(func() {
trace.StopAgent()
})
2020-07-26 17:09:05 +08:00
if len(sc.MetricsUrl) > 0 {
stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl))
}
devserver.StartAgent(sc.DevServer)
2020-07-26 17:09:05 +08:00
return nil
}
func (sc ServiceConf) initMode() {
switch sc.Mode {
2021-03-30 20:45:55 +08:00
case DevMode, TestMode, RtMode, PreMode:
2020-07-26 17:09:05 +08:00
load.Disable()
stat.SetReporter(nil)
}
}