server.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "time"
  10. "golang.org/x/sync/errgroup"
  11. "github.com/grafana/grafana/pkg/api"
  12. "github.com/grafana/grafana/pkg/log"
  13. "github.com/grafana/grafana/pkg/login"
  14. "github.com/grafana/grafana/pkg/metrics"
  15. "github.com/grafana/grafana/pkg/models"
  16. "github.com/grafana/grafana/pkg/plugins"
  17. "github.com/grafana/grafana/pkg/services/alerting"
  18. "github.com/grafana/grafana/pkg/services/cleanup"
  19. "github.com/grafana/grafana/pkg/services/eventpublisher"
  20. "github.com/grafana/grafana/pkg/services/notifications"
  21. "github.com/grafana/grafana/pkg/services/search"
  22. "github.com/grafana/grafana/pkg/setting"
  23. "github.com/grafana/grafana/pkg/social"
  24. opentracing "github.com/opentracing/opentracing-go"
  25. jaeger "github.com/uber/jaeger-client-go"
  26. jaegercfg "github.com/uber/jaeger-client-go/config"
  27. jaegerlog "github.com/uber/jaeger-client-go/log"
  28. )
  29. func NewGrafanaServer() models.GrafanaServer {
  30. rootCtx, shutdownFn := context.WithCancel(context.Background())
  31. childRoutines, childCtx := errgroup.WithContext(rootCtx)
  32. return &GrafanaServerImpl{
  33. context: childCtx,
  34. shutdownFn: shutdownFn,
  35. childRoutines: childRoutines,
  36. log: log.New("server"),
  37. }
  38. }
  39. type GrafanaServerImpl struct {
  40. context context.Context
  41. shutdownFn context.CancelFunc
  42. childRoutines *errgroup.Group
  43. log log.Logger
  44. httpServer *api.HttpServer
  45. }
  46. func (g *GrafanaServerImpl) Start() {
  47. go listenToSystemSignals(g)
  48. g.initLogging()
  49. g.writePIDFile()
  50. initSql()
  51. metrics.Init(setting.Cfg)
  52. search.Init()
  53. login.Init()
  54. social.NewOAuthService()
  55. eventpublisher.Init()
  56. plugins.Init()
  57. //localhost:5775
  58. cfg := jaegercfg.Configuration{
  59. Disabled: false,
  60. Sampler: &jaegercfg.SamplerConfig{
  61. Type: jaeger.SamplerTypeConst,
  62. Param: 1,
  63. },
  64. Reporter: &jaegercfg.ReporterConfig{
  65. LogSpans: false,
  66. LocalAgentHostPort: "localhost:5775",
  67. },
  68. }
  69. jLogger := jaegerlog.StdLogger
  70. tracer, closer, err := cfg.New(
  71. "grafana",
  72. jaegercfg.Logger(jLogger),
  73. )
  74. if err != nil {
  75. g.log.Error("tracing", "error", err)
  76. g.Shutdown(1, "Startup failed")
  77. return
  78. }
  79. opentracing.InitGlobalTracer(tracer)
  80. defer closer.Close()
  81. // init alerting
  82. if setting.AlertingEnabled && setting.ExecuteAlerts {
  83. engine := alerting.NewEngine()
  84. g.childRoutines.Go(func() error { return engine.Run(g.context) })
  85. }
  86. // cleanup service
  87. cleanUpService := cleanup.NewCleanUpService()
  88. g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
  89. if err := notifications.Init(); err != nil {
  90. g.log.Error("Notification service failed to initialize", "error", err)
  91. g.Shutdown(1, "Startup failed")
  92. return
  93. }
  94. g.startHttpServer()
  95. }
  96. func (g *GrafanaServerImpl) initLogging() {
  97. err := setting.NewConfigContext(&setting.CommandLineArgs{
  98. Config: *configFile,
  99. HomePath: *homePath,
  100. Args: flag.Args(),
  101. })
  102. if err != nil {
  103. g.log.Error(err.Error())
  104. os.Exit(1)
  105. }
  106. g.log.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  107. setting.LogConfigurationInfo()
  108. }
  109. func (g *GrafanaServerImpl) startHttpServer() {
  110. g.httpServer = api.NewHttpServer()
  111. err := g.httpServer.Start(g.context)
  112. if err != nil {
  113. g.log.Error("Fail to start server", "error", err)
  114. g.Shutdown(1, "Startup failed")
  115. return
  116. }
  117. }
  118. func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
  119. g.log.Info("Shutdown started", "code", code, "reason", reason)
  120. err := g.httpServer.Shutdown(g.context)
  121. if err != nil {
  122. g.log.Error("Failed to shutdown server", "error", err)
  123. }
  124. g.shutdownFn()
  125. err = g.childRoutines.Wait()
  126. g.log.Info("Shutdown completed", "reason", err)
  127. log.Close()
  128. os.Exit(code)
  129. }
  130. func (g *GrafanaServerImpl) writePIDFile() {
  131. if *pidFile == "" {
  132. return
  133. }
  134. // Ensure the required directory structure exists.
  135. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  136. if err != nil {
  137. g.log.Error("Failed to verify pid directory", "error", err)
  138. os.Exit(1)
  139. }
  140. // Retrieve the PID and write it.
  141. pid := strconv.Itoa(os.Getpid())
  142. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  143. g.log.Error("Failed to write pidfile", "error", err)
  144. os.Exit(1)
  145. }
  146. g.log.Info("Writing PID file", "path", *pidFile, "pid", pid)
  147. }