You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/service/serviceconf.go

73 lines
1.6 KiB
Go

4 years ago
package service
import (
"log"
"github.com/tal-tech/go-zero/core/load"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/opentelemetry"
"github.com/tal-tech/go-zero/core/prometheus"
"github.com/tal-tech/go-zero/core/stat"
4 years ago
)
const (
// DevMode means development mode.
DevMode = "dev"
// TestMode means test mode.
4 years ago
TestMode = "test"
// RtMode means regression test mode.
RtMode = "rt"
// PreMode means pre-release mode.
PreMode = "pre"
// ProMode means production mode.
ProMode = "pro"
4 years ago
)
// A ServiceConf is a service config.
4 years ago
type ServiceConf struct {
Name string
Log logx.LogConf
Mode string `json:",default=pro,options=dev|test|rt|pre|pro"`
MetricsUrl string `json:",optional"`
Prometheus prometheus.Config `json:",optional"`
OpenTelemetry opentelemetry.Config `json:",optional"`
4 years ago
}
// MustSetUp sets up the service, exits on error.
4 years ago
func (sc ServiceConf) MustSetUp() {
if err := sc.SetUp(); err != nil {
log.Fatal(err)
}
}
// SetUp sets up the service.
4 years ago
func (sc ServiceConf) SetUp() error {
if len(sc.Log.ServiceName) == 0 {
sc.Log.ServiceName = sc.Name
}
if len(sc.OpenTelemetry.Name) == 0 {
sc.OpenTelemetry.Name = sc.Name
}
4 years ago
if err := logx.SetUp(sc.Log); err != nil {
return err
}
sc.initMode()
prometheus.StartAgent(sc.Prometheus)
opentelemetry.StartAgent(sc.OpenTelemetry)
4 years ago
if len(sc.MetricsUrl) > 0 {
stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl))
}
return nil
}
func (sc ServiceConf) initMode() {
switch sc.Mode {
case DevMode, TestMode, RtMode, PreMode:
4 years ago
load.Disable()
stat.SetReporter(nil)
}
}