From cfb061ddaba0d8bf38199b74eb0f25eaa33b27ad Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 26 Oct 2018 10:40:33 +0200 Subject: [PATCH 1/8] refactor datasource caching --- pkg/api/dataproxy.go | 50 ++++--------------------------- pkg/api/http_server.go | 18 ++++++----- pkg/api/metrics.go | 5 +++- pkg/cmd/grafana-server/server.go | 9 +++++- pkg/middleware/headers.go | 14 +++++++++ pkg/middleware/middleware.go | 1 + pkg/models/context.go | 1 + pkg/services/cache/cache.go | 17 +++++++++++ pkg/services/datasources/cache.go | 49 ++++++++++++++++++++++++++++++ 9 files changed, 109 insertions(+), 55 deletions(-) create mode 100644 pkg/middleware/headers.go create mode 100644 pkg/services/cache/cache.go create mode 100644 pkg/services/datasources/cache.go diff --git a/pkg/api/dataproxy.go b/pkg/api/dataproxy.go index 3bb2f236129..1bc97eb42ed 100644 --- a/pkg/api/dataproxy.go +++ b/pkg/api/dataproxy.go @@ -1,62 +1,22 @@ package api import ( - "fmt" - "github.com/pkg/errors" - "time" - "github.com/grafana/grafana/pkg/api/pluginproxy" - "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/metrics" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" ) -const HeaderNameNoBackendCache = "X-Grafana-NoCache" - -func (hs *HTTPServer) getDatasourceFromCache(id int64, c *m.ReqContext) (*m.DataSource, error) { - userPermissionsQuery := m.GetDataSourcePermissionsForUserQuery{ - User: c.SignedInUser, - } - if err := bus.Dispatch(&userPermissionsQuery); err != nil { - if err != bus.ErrHandlerNotFound { - return nil, err - } - } else { - permissionType, exists := userPermissionsQuery.Result[id] - if exists && permissionType != m.DsPermissionQuery { - return nil, errors.New("User not allowed to access datasource") - } - } - - nocache := c.Req.Header.Get(HeaderNameNoBackendCache) == "true" - cacheKey := fmt.Sprintf("ds-%d", id) - - if !nocache { - if cached, found := hs.cache.Get(cacheKey); found { - ds := cached.(*m.DataSource) - if ds.OrgId == c.OrgId { - return ds, nil - } - } - } - - query := m.GetDataSourceByIdQuery{Id: id, OrgId: c.OrgId} - if err := bus.Dispatch(&query); err != nil { - return nil, err - } - - hs.cache.Set(cacheKey, query.Result, time.Second*5) - return query.Result, nil -} - func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) { c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer) dsId := c.ParamsInt64(":id") - ds, err := hs.getDatasourceFromCache(dsId, c) - + ds, err := hs.DatasourceCache.GetDatasource(dsId, c.SignedInUser, c.SkipCache) if err != nil { + if err == m.ErrDataSourceAccessDenied { + c.JsonApiErr(403, "Access denied to datasource", nil) + return + } c.JsonApiErr(500, "Unable to load datasource meta data", err) return } diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 858b3c5a8c5..ce28e4716ee 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -16,7 +16,6 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" - gocache "github.com/patrickmn/go-cache" macaron "gopkg.in/macaron.v1" "github.com/grafana/grafana/pkg/api/live" @@ -28,6 +27,8 @@ import ( "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/registry" + "github.com/grafana/grafana/pkg/services/cache" + "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/hooks" "github.com/grafana/grafana/pkg/services/rendering" "github.com/grafana/grafana/pkg/setting" @@ -46,19 +47,19 @@ type HTTPServer struct { macaron *macaron.Macaron context context.Context streamManager *live.StreamManager - cache *gocache.Cache httpSrv *http.Server - RouteRegister routing.RouteRegister `inject:""` - Bus bus.Bus `inject:""` - RenderService rendering.Service `inject:""` - Cfg *setting.Cfg `inject:""` - HooksService *hooks.HooksService `inject:""` + RouteRegister routing.RouteRegister `inject:""` + Bus bus.Bus `inject:""` + RenderService rendering.Service `inject:""` + Cfg *setting.Cfg `inject:""` + HooksService *hooks.HooksService `inject:""` + CacheService *cache.CacheService `inject:""` + DatasourceCache datasources.CacheService `inject:""` } func (hs *HTTPServer) Init() error { hs.log = log.New("http.server") - hs.cache = gocache.New(5*time.Minute, 10*time.Minute) hs.streamManager = live.NewStreamManager() hs.macaron = hs.newMacaron() @@ -231,6 +232,7 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() { m.Use(middleware.ValidateHostHeader(setting.Domain)) } + m.Use(middleware.HandleNoCacheHeader()) m.Use(middleware.AddDefaultResponseHeaders()) } diff --git a/pkg/api/metrics.go b/pkg/api/metrics.go index cb80bd346b8..a6cfe4d09de 100644 --- a/pkg/api/metrics.go +++ b/pkg/api/metrics.go @@ -25,8 +25,11 @@ func (hs *HTTPServer) QueryMetrics(c *m.ReqContext, reqDto dtos.MetricRequest) R return Error(400, "Query missing datasourceId", nil) } - ds, err := hs.getDatasourceFromCache(datasourceId, c) + ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache) if err != nil { + if err == m.ErrDataSourceAccessDenied { + return Error(403, "Access denied to datasource", nil) + } return Error(500, "Unable to load datasource meta data", err) } diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index 765b8ddf993..a07cb692a68 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -15,9 +15,15 @@ import ( "github.com/grafana/grafana/pkg/api" "github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/bus" - _ "github.com/grafana/grafana/pkg/extensions" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/login" + "github.com/grafana/grafana/pkg/services/cache" + "github.com/grafana/grafana/pkg/setting" + + "github.com/grafana/grafana/pkg/social" + + // self registering services + _ "github.com/grafana/grafana/pkg/extensions" _ "github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/middleware" _ "github.com/grafana/grafana/pkg/plugins" @@ -72,6 +78,7 @@ func (g *GrafanaServerImpl) Run() error { serviceGraph.Provide(&inject.Object{Value: bus.GetBus()}) serviceGraph.Provide(&inject.Object{Value: g.cfg}) serviceGraph.Provide(&inject.Object{Value: routing.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)}) + serviceGraph.Provide(&inject.Object{Value: cache.New(5*time.Minute, 10*time.Minute)}) // self registered services services := registry.GetServices() diff --git a/pkg/middleware/headers.go b/pkg/middleware/headers.go new file mode 100644 index 00000000000..28c623d74b0 --- /dev/null +++ b/pkg/middleware/headers.go @@ -0,0 +1,14 @@ +package middleware + +import ( + m "github.com/grafana/grafana/pkg/models" + macaron "gopkg.in/macaron.v1" +) + +const HeaderNameNoBackendCache = "X-Grafana-NoCache" + +func HandleNoCacheHeader() macaron.Handler { + return func(ctx *m.ReqContext) { + ctx.SkipCache = ctx.Req.Header.Get(HeaderNameNoBackendCache) == "true" + } +} diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 7b29901c1a3..ace72d998eb 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -29,6 +29,7 @@ func GetContextHandler() macaron.Handler { Session: session.GetSession(), IsSignedIn: false, AllowAnonymous: false, + SkipCache: false, Logger: log.New("context"), } diff --git a/pkg/models/context.go b/pkg/models/context.go index 262f6550954..8ed7fa61a7d 100644 --- a/pkg/models/context.go +++ b/pkg/models/context.go @@ -20,6 +20,7 @@ type ReqContext struct { IsSignedIn bool IsRenderCall bool AllowAnonymous bool + SkipCache bool Logger log.Logger } diff --git a/pkg/services/cache/cache.go b/pkg/services/cache/cache.go new file mode 100644 index 00000000000..93b2cf76e26 --- /dev/null +++ b/pkg/services/cache/cache.go @@ -0,0 +1,17 @@ +package cache + +import ( + "time" + + gocache "github.com/patrickmn/go-cache" +) + +type CacheService struct { + *gocache.Cache +} + +func New(defaultExpiration, cleanupInterval time.Duration) *CacheService { + return &CacheService{ + Cache: gocache.New(defaultExpiration, cleanupInterval), + } +} diff --git a/pkg/services/datasources/cache.go b/pkg/services/datasources/cache.go new file mode 100644 index 00000000000..c984b4f8743 --- /dev/null +++ b/pkg/services/datasources/cache.go @@ -0,0 +1,49 @@ +package datasources + +import ( + "fmt" + "time" + + "github.com/grafana/grafana/pkg/bus" + m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/registry" + "github.com/grafana/grafana/pkg/services/cache" +) + +type CacheService interface { + GetDatasource(datasourceID int64, user *m.SignedInUser, skipCache bool) (*m.DataSource, error) +} + +type CacheServiceImpl struct { + Bus bus.Bus `inject:""` + CacheService *cache.CacheService `inject:""` +} + +func init() { + registry.RegisterService(&CacheServiceImpl{}) +} + +func (dc *CacheServiceImpl) Init() error { + return nil +} + +func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *m.SignedInUser, skipCache bool) (*m.DataSource, error) { + cacheKey := fmt.Sprintf("ds-%d", datasourceID) + + if !skipCache { + if cached, found := dc.CacheService.Get(cacheKey); found { + ds := cached.(*m.DataSource) + if ds.OrgId == user.OrgId { + return ds, nil + } + } + } + + query := m.GetDataSourceByIdQuery{Id: datasourceID, OrgId: user.OrgId} + if err := dc.Bus.Dispatch(&query); err != nil { + return nil, err + } + + dc.CacheService.Set(cacheKey, query.Result, time.Second*5) + return query.Result, nil +} From 9edaa3fa8c45f312bab0bb809b0b586318a647b8 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 26 Oct 2018 13:57:31 +0200 Subject: [PATCH 2/8] application lifecycle event support --- pkg/cmd/grafana-server/server.go | 18 ++++++++-------- pkg/lifecycle/lifecycle.go | 22 ++++++++++++++++++++ pkg/lifecycle/lifecycle_test.go | 35 ++++++++++++++++++++++++++++++++ pkg/login/auth.go | 10 ++++++--- pkg/social/social.go | 9 +++++++- 5 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 pkg/lifecycle/lifecycle.go create mode 100644 pkg/lifecycle/lifecycle_test.go diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index a07cb692a68..d8d021d502b 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -15,13 +15,17 @@ import ( "github.com/grafana/grafana/pkg/api" "github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/lifecycle" + "github.com/grafana/grafana/pkg/middleware" + "github.com/grafana/grafana/pkg/registry" + + "golang.org/x/sync/errgroup" + + "github.com/grafana/grafana/pkg/api" "github.com/grafana/grafana/pkg/log" - "github.com/grafana/grafana/pkg/login" "github.com/grafana/grafana/pkg/services/cache" "github.com/grafana/grafana/pkg/setting" - "github.com/grafana/grafana/pkg/social" - // self registering services _ "github.com/grafana/grafana/pkg/extensions" _ "github.com/grafana/grafana/pkg/metrics" @@ -35,8 +39,7 @@ import ( _ "github.com/grafana/grafana/pkg/services/rendering" _ "github.com/grafana/grafana/pkg/services/search" _ "github.com/grafana/grafana/pkg/services/sqlstore" - "github.com/grafana/grafana/pkg/setting" - "github.com/grafana/grafana/pkg/social" // self registering services + "github.com/grafana/grafana/pkg/setting" // self registering services _ "github.com/grafana/grafana/pkg/tracing" "golang.org/x/sync/errgroup" ) @@ -71,8 +74,7 @@ func (g *GrafanaServerImpl) Run() error { g.loadConfiguration() g.writePIDFile() - login.Init() - social.NewOAuthService() + lifecycle.Notify(lifecycle.ApplicationStarting) serviceGraph := inject.Graph{} serviceGraph.Provide(&inject.Object{Value: bus.GetBus()}) @@ -145,7 +147,7 @@ func (g *GrafanaServerImpl) Run() error { } sendSystemdNotification("READY=1") - + lifecycle.Notify(lifecycle.ApplicationStarted) return g.childRoutines.Wait() } diff --git a/pkg/lifecycle/lifecycle.go b/pkg/lifecycle/lifecycle.go new file mode 100644 index 00000000000..eea733a7da8 --- /dev/null +++ b/pkg/lifecycle/lifecycle.go @@ -0,0 +1,22 @@ +package lifecycle + +type Event int + +const ( + ApplicationStarting Event = iota + ApplicationStarted +) + +type EventHandlerFunc func() + +var listeners = map[int][]EventHandlerFunc{} + +func AddListener(evt Event, fn EventHandlerFunc) { + listeners[int(evt)] = append(listeners[int(evt)], fn) +} + +func Notify(evt Event) { + for _, handler := range listeners[int(evt)] { + handler() + } +} diff --git a/pkg/lifecycle/lifecycle_test.go b/pkg/lifecycle/lifecycle_test.go new file mode 100644 index 00000000000..e946cc2f4a8 --- /dev/null +++ b/pkg/lifecycle/lifecycle_test.go @@ -0,0 +1,35 @@ +package lifecycle + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestLifecycle(t *testing.T) { + Convey("TestLifecycle", t, func() { + Convey("Given listeners", func() { + applicationStartingCounter := 0 + AddListener(ApplicationStarting, func() { + applicationStartingCounter++ + }) + + applicationStartedCounter := 0 + AddListener(ApplicationStarted, func() { + applicationStartedCounter++ + }) + + Convey("When notify application starting should call listener", func() { + Notify(ApplicationStarting) + So(applicationStartingCounter, ShouldEqual, 1) + So(applicationStartedCounter, ShouldEqual, 0) + }) + + Convey("When notify application started should call listener", func() { + Notify(ApplicationStarted) + So(applicationStartingCounter, ShouldEqual, 0) + So(applicationStartedCounter, ShouldEqual, 1) + }) + }) + }) +} diff --git a/pkg/login/auth.go b/pkg/login/auth.go index 991fa72fd54..b195b8dd2e4 100644 --- a/pkg/login/auth.go +++ b/pkg/login/auth.go @@ -2,7 +2,9 @@ package login import ( "errors" + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/lifecycle" m "github.com/grafana/grafana/pkg/models" ) @@ -18,9 +20,11 @@ var ( ErrGettingUserQuota = errors.New("Error getting user quota") ) -func Init() { - bus.AddHandler("auth", AuthenticateUser) - loadLdapConfig() +func init() { + lifecycle.AddListener(lifecycle.ApplicationStarting, func() { + bus.AddHandler("auth", AuthenticateUser) + loadLdapConfig() + }) } func AuthenticateUser(query *m.LoginUserQuery) error { diff --git a/pkg/social/social.go b/pkg/social/social.go index 8918507f3b9..054420fd994 100644 --- a/pkg/social/social.go +++ b/pkg/social/social.go @@ -8,11 +8,18 @@ import ( "golang.org/x/oauth2" + "github.com/grafana/grafana/pkg/lifecycle" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" ) +func init() { + lifecycle.AddListener(lifecycle.ApplicationStarting, func() { + initOAuthService() + }) +} + type BasicUserInfo struct { Id string Name string @@ -56,7 +63,7 @@ var ( allOauthes = []string{"github", "gitlab", "google", "generic_oauth", "grafananet", grafanaCom} ) -func NewOAuthService() { +func initOAuthService() { setting.OAuthService = &setting.OAuther{} setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo) From 70ddf936887869d3991f3926f1864a83a967d12c Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Thu, 25 Oct 2018 15:20:01 +0200 Subject: [PATCH 3/8] include teams on signed in user --- pkg/models/user.go | 1 + pkg/services/sqlstore/user.go | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/pkg/models/user.go b/pkg/models/user.go index d5b912e0a9c..e3c7b556d35 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -165,6 +165,7 @@ type SignedInUser struct { IsAnonymous bool HelpFlags1 HelpFlags1 LastSeenAt time.Time + Teams []int64 } func (u *SignedInUser) ShouldUpdateLastSeenAt() bool { diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 72d5654a777..3744a04d88a 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -387,6 +387,17 @@ func GetSignedInUser(query *m.GetSignedInUserQuery) error { if user.OrgRole == "" { user.OrgId = -1 user.OrgName = "Org missing" + } else { + getTeamsByUserQuery := &m.GetTeamsByUserQuery{OrgId: user.OrgId, UserId: user.UserId} + err = GetTeamsByUser(getTeamsByUserQuery) + if err != nil { + return err + } + + user.Teams = make([]int64, len(getTeamsByUserQuery.Result)) + for i, t := range getTeamsByUserQuery.Result { + user.Teams[i] = t.Id + } } query.Result = &user From 52d825f5351ed6271bde1e2aee27ceb4888a6689 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 26 Oct 2018 18:34:10 +0200 Subject: [PATCH 4/8] log error on datasource access denied --- pkg/api/dataproxy.go | 2 +- pkg/api/metrics.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/api/dataproxy.go b/pkg/api/dataproxy.go index 1bc97eb42ed..5cde0efd0b4 100644 --- a/pkg/api/dataproxy.go +++ b/pkg/api/dataproxy.go @@ -14,7 +14,7 @@ func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) { ds, err := hs.DatasourceCache.GetDatasource(dsId, c.SignedInUser, c.SkipCache) if err != nil { if err == m.ErrDataSourceAccessDenied { - c.JsonApiErr(403, "Access denied to datasource", nil) + c.JsonApiErr(403, "Access denied to datasource", err) return } c.JsonApiErr(500, "Unable to load datasource meta data", err) diff --git a/pkg/api/metrics.go b/pkg/api/metrics.go index a6cfe4d09de..6e5ae0f8761 100644 --- a/pkg/api/metrics.go +++ b/pkg/api/metrics.go @@ -28,7 +28,7 @@ func (hs *HTTPServer) QueryMetrics(c *m.ReqContext, reqDto dtos.MetricRequest) R ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache) if err != nil { if err == m.ErrDataSourceAccessDenied { - return Error(403, "Access denied to datasource", nil) + return Error(403, "Access denied to datasource", err) } return Error(500, "Unable to load datasource meta data", err) } From 2332b3e20579de39c7bbc227f69c11d184d6722b Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 26 Oct 2018 18:55:16 +0200 Subject: [PATCH 5/8] remove unused code --- pkg/cmd/grafana-server/server.go | 5 ----- pkg/models/datasource.go | 5 ----- 2 files changed, 10 deletions(-) diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index d8d021d502b..d4223df19ed 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -21,7 +21,6 @@ import ( "golang.org/x/sync/errgroup" - "github.com/grafana/grafana/pkg/api" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/services/cache" "github.com/grafana/grafana/pkg/setting" @@ -29,9 +28,7 @@ import ( // self registering services _ "github.com/grafana/grafana/pkg/extensions" _ "github.com/grafana/grafana/pkg/metrics" - "github.com/grafana/grafana/pkg/middleware" _ "github.com/grafana/grafana/pkg/plugins" - "github.com/grafana/grafana/pkg/registry" _ "github.com/grafana/grafana/pkg/services/alerting" _ "github.com/grafana/grafana/pkg/services/cleanup" _ "github.com/grafana/grafana/pkg/services/notifications" @@ -39,9 +36,7 @@ import ( _ "github.com/grafana/grafana/pkg/services/rendering" _ "github.com/grafana/grafana/pkg/services/search" _ "github.com/grafana/grafana/pkg/services/sqlstore" - "github.com/grafana/grafana/pkg/setting" // self registering services _ "github.com/grafana/grafana/pkg/tracing" - "golang.org/x/sync/errgroup" ) func NewGrafanaServer() *GrafanaServerImpl { diff --git a/pkg/models/datasource.go b/pkg/models/datasource.go index b71d17ec0d1..89439420d7a 100644 --- a/pkg/models/datasource.go +++ b/pkg/models/datasource.go @@ -207,11 +207,6 @@ func (p DsPermissionType) String() string { return names[int(p)] } -type GetDataSourcePermissionsForUserQuery struct { - User *SignedInUser - Result map[int64]DsPermissionType -} - type DatasourcesPermissionFilterQuery struct { User *SignedInUser Datasources []*DataSource From 5d4dc18bbc7586de466feb0a935a12f575907e73 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Tue, 30 Oct 2018 12:31:28 +0100 Subject: [PATCH 6/8] revert application lifecycle event support --- pkg/cmd/grafana-server/server.go | 7 ++++--- pkg/lifecycle/lifecycle.go | 22 -------------------- pkg/lifecycle/lifecycle_test.go | 35 -------------------------------- pkg/login/auth.go | 9 +++----- pkg/social/social.go | 9 +------- 5 files changed, 8 insertions(+), 74 deletions(-) delete mode 100644 pkg/lifecycle/lifecycle.go delete mode 100644 pkg/lifecycle/lifecycle_test.go diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index d4223df19ed..2c67a06a843 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -15,9 +15,10 @@ import ( "github.com/grafana/grafana/pkg/api" "github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/bus" - "github.com/grafana/grafana/pkg/lifecycle" + "github.com/grafana/grafana/pkg/login" "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/registry" + "github.com/grafana/grafana/pkg/social" "golang.org/x/sync/errgroup" @@ -69,7 +70,8 @@ func (g *GrafanaServerImpl) Run() error { g.loadConfiguration() g.writePIDFile() - lifecycle.Notify(lifecycle.ApplicationStarting) + login.Init() + social.NewOAuthService() serviceGraph := inject.Graph{} serviceGraph.Provide(&inject.Object{Value: bus.GetBus()}) @@ -142,7 +144,6 @@ func (g *GrafanaServerImpl) Run() error { } sendSystemdNotification("READY=1") - lifecycle.Notify(lifecycle.ApplicationStarted) return g.childRoutines.Wait() } diff --git a/pkg/lifecycle/lifecycle.go b/pkg/lifecycle/lifecycle.go deleted file mode 100644 index eea733a7da8..00000000000 --- a/pkg/lifecycle/lifecycle.go +++ /dev/null @@ -1,22 +0,0 @@ -package lifecycle - -type Event int - -const ( - ApplicationStarting Event = iota - ApplicationStarted -) - -type EventHandlerFunc func() - -var listeners = map[int][]EventHandlerFunc{} - -func AddListener(evt Event, fn EventHandlerFunc) { - listeners[int(evt)] = append(listeners[int(evt)], fn) -} - -func Notify(evt Event) { - for _, handler := range listeners[int(evt)] { - handler() - } -} diff --git a/pkg/lifecycle/lifecycle_test.go b/pkg/lifecycle/lifecycle_test.go deleted file mode 100644 index e946cc2f4a8..00000000000 --- a/pkg/lifecycle/lifecycle_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package lifecycle - -import ( - "testing" - - . "github.com/smartystreets/goconvey/convey" -) - -func TestLifecycle(t *testing.T) { - Convey("TestLifecycle", t, func() { - Convey("Given listeners", func() { - applicationStartingCounter := 0 - AddListener(ApplicationStarting, func() { - applicationStartingCounter++ - }) - - applicationStartedCounter := 0 - AddListener(ApplicationStarted, func() { - applicationStartedCounter++ - }) - - Convey("When notify application starting should call listener", func() { - Notify(ApplicationStarting) - So(applicationStartingCounter, ShouldEqual, 1) - So(applicationStartedCounter, ShouldEqual, 0) - }) - - Convey("When notify application started should call listener", func() { - Notify(ApplicationStarted) - So(applicationStartingCounter, ShouldEqual, 0) - So(applicationStartedCounter, ShouldEqual, 1) - }) - }) - }) -} diff --git a/pkg/login/auth.go b/pkg/login/auth.go index b195b8dd2e4..b766d963328 100644 --- a/pkg/login/auth.go +++ b/pkg/login/auth.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/grafana/grafana/pkg/bus" - "github.com/grafana/grafana/pkg/lifecycle" m "github.com/grafana/grafana/pkg/models" ) @@ -20,11 +19,9 @@ var ( ErrGettingUserQuota = errors.New("Error getting user quota") ) -func init() { - lifecycle.AddListener(lifecycle.ApplicationStarting, func() { - bus.AddHandler("auth", AuthenticateUser) - loadLdapConfig() - }) +func Init() { + bus.AddHandler("auth", AuthenticateUser) + loadLdapConfig() } func AuthenticateUser(query *m.LoginUserQuery) error { diff --git a/pkg/social/social.go b/pkg/social/social.go index 054420fd994..8918507f3b9 100644 --- a/pkg/social/social.go +++ b/pkg/social/social.go @@ -8,18 +8,11 @@ import ( "golang.org/x/oauth2" - "github.com/grafana/grafana/pkg/lifecycle" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" ) -func init() { - lifecycle.AddListener(lifecycle.ApplicationStarting, func() { - initOAuthService() - }) -} - type BasicUserInfo struct { Id string Name string @@ -63,7 +56,7 @@ var ( allOauthes = []string{"github", "gitlab", "google", "generic_oauth", "grafananet", grafanaCom} ) -func initOAuthService() { +func NewOAuthService() { setting.OAuthService = &setting.OAuther{} setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo) From 6f9c0241afce94323f65e050a01d53357c4a1419 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Tue, 30 Oct 2018 12:32:14 +0100 Subject: [PATCH 7/8] register datasource cache service with proper name --- pkg/services/datasources/cache.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/services/datasources/cache.go b/pkg/services/datasources/cache.go index c984b4f8743..0cd2bae63b5 100644 --- a/pkg/services/datasources/cache.go +++ b/pkg/services/datasources/cache.go @@ -20,7 +20,11 @@ type CacheServiceImpl struct { } func init() { - registry.RegisterService(&CacheServiceImpl{}) + registry.Register(®istry.Descriptor{ + Name: "DatasourceCacheService", + Instance: &CacheServiceImpl{}, + InitPriority: registry.Low, + }) } func (dc *CacheServiceImpl) Init() error { From d0c00388e6193cd438257935dba25b88eb56216a Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Tue, 30 Oct 2018 13:37:30 +0100 Subject: [PATCH 8/8] add functionality to override service in registry --- pkg/registry/registry.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 87fca27f6c1..487a6db7927 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -29,11 +29,42 @@ func Register(descriptor *Descriptor) { } func GetServices() []*Descriptor { - sort.Slice(services, func(i, j int) bool { - return services[i].InitPriority > services[j].InitPriority + slice := getServicesWithOverrides() + + sort.Slice(slice, func(i, j int) bool { + return slice[i].InitPriority > slice[j].InitPriority }) - return services + return slice +} + +type OverrideServiceFunc func(descriptor Descriptor) (*Descriptor, bool) + +var overrides []OverrideServiceFunc + +func RegisterOverride(fn OverrideServiceFunc) { + overrides = append(overrides, fn) +} + +func getServicesWithOverrides() []*Descriptor { + slice := []*Descriptor{} + for _, s := range services { + var descriptor *Descriptor + for _, fn := range overrides { + if newDescriptor, override := fn(*s); override { + descriptor = newDescriptor + break + } + } + + if descriptor != nil { + slice = append(slice, descriptor) + } else { + slice = append(slice, s) + } + } + + return slice } // Service interface is the lowest common shape that services