Chore: Remove context.TODO (#43458)

* Remove context.TODO() from services

* Fix live test
This commit is contained in:
idafurjes
2021-12-28 10:26:18 +01:00
committed by GitHub
parent 56b3dc5445
commit 56c3875bb9
24 changed files with 81 additions and 77 deletions
+21 -21
View File
@@ -158,7 +158,7 @@ func ProvideService(plugCtxProvider *plugincontext.Provider, cfg *setting.Cfg, r
redisClient := redis.NewClient(&redis.Options{
Addr: g.Cfg.LiveHAEngineAddress,
})
cmd := redisClient.Ping(context.TODO())
cmd := redisClient.Ping(context.Background())
if _, err := cmd.Result(); err != nil {
return nil, fmt.Errorf("error pinging Redis: %v", err)
}
@@ -206,7 +206,7 @@ func ProvideService(plugCtxProvider *plugincontext.Provider, cfg *setting.Cfg, r
// This can be unreasonable to have in production scenario with many
// organizations.
orgQuery := &models.SearchOrgsQuery{}
err := sqlstore.SearchOrgs(context.TODO(), orgQuery)
err := sqlstore.SearchOrgs(context.Background(), orgQuery)
if err != nil {
return nil, fmt.Errorf("can't get org list: %w", err)
}
@@ -278,7 +278,7 @@ func ProvideService(plugCtxProvider *plugincontext.Provider, cfg *setting.Cfg, r
// Called when client subscribes to the channel.
client.OnSubscribe(func(e centrifuge.SubscribeEvent, cb centrifuge.SubscribeCallback) {
err := runConcurrentlyIfNeeded(client.Context(), semaphore, func() {
cb(g.handleOnSubscribe(client, e))
cb(g.handleOnSubscribe(context.Background(), client, e))
})
if err != nil {
cb(centrifuge.SubscribeReply{}, err)
@@ -290,7 +290,7 @@ func ProvideService(plugCtxProvider *plugincontext.Provider, cfg *setting.Cfg, r
// allows some simple prototypes to work quickly.
client.OnPublish(func(e centrifuge.PublishEvent, cb centrifuge.PublishCallback) {
err := runConcurrentlyIfNeeded(client.Context(), semaphore, func() {
cb(g.handleOnPublish(client, e))
cb(g.handleOnPublish(context.Background(), client, e))
})
if err != nil {
cb(centrifuge.PublishReply{}, err)
@@ -429,8 +429,8 @@ type GrafanaLive struct {
usageStats usageStats
}
func (g *GrafanaLive) getStreamPlugin(pluginID string) (backend.StreamHandler, error) {
plugin, exists := g.pluginStore.Plugin(context.TODO(), pluginID)
func (g *GrafanaLive) getStreamPlugin(ctx context.Context, pluginID string) (backend.StreamHandler, error) {
plugin, exists := g.pluginStore.Plugin(ctx, pluginID)
if !exists {
return nil, fmt.Errorf("plugin not found: %s", pluginID)
}
@@ -604,7 +604,7 @@ func (g *GrafanaLive) handleOnRPC(client *centrifuge.Client, e centrifuge.RPCEve
}, nil
}
func (g *GrafanaLive) handleOnSubscribe(client *centrifuge.Client, e centrifuge.SubscribeEvent) (centrifuge.SubscribeReply, error) {
func (g *GrafanaLive) handleOnSubscribe(ctx context.Context, client *centrifuge.Client, e centrifuge.SubscribeEvent) (centrifuge.SubscribeReply, error) {
logger.Debug("Client wants to subscribe", "user", client.UserID(), "client", client.ID(), "channel", e.Channel)
user, ok := livecontext.GetContextSignedUser(client.Context())
@@ -668,7 +668,7 @@ func (g *GrafanaLive) handleOnSubscribe(client *centrifuge.Client, e centrifuge.
}
}
if !ruleFound {
handler, addr, err := g.GetChannelHandler(user, channel)
handler, addr, err := g.GetChannelHandler(ctx, user, channel)
if err != nil {
if errors.Is(err, live.ErrInvalidChannelID) {
logger.Info("Invalid channel ID", "user", client.UserID(), "client", client.ID(), "channel", e.Channel)
@@ -704,7 +704,7 @@ func (g *GrafanaLive) handleOnSubscribe(client *centrifuge.Client, e centrifuge.
}, nil
}
func (g *GrafanaLive) handleOnPublish(client *centrifuge.Client, e centrifuge.PublishEvent) (centrifuge.PublishReply, error) {
func (g *GrafanaLive) handleOnPublish(ctx context.Context, client *centrifuge.Client, e centrifuge.PublishEvent) (centrifuge.PublishReply, error) {
logger.Debug("Client wants to publish", "user", client.UserID(), "client", client.ID(), "channel", e.Channel)
user, ok := livecontext.GetContextSignedUser(client.Context())
@@ -761,7 +761,7 @@ func (g *GrafanaLive) handleOnPublish(client *centrifuge.Client, e centrifuge.Pu
}
}
handler, addr, err := g.GetChannelHandler(user, channel)
handler, addr, err := g.GetChannelHandler(ctx, user, channel)
if err != nil {
if errors.Is(err, live.ErrInvalidChannelID) {
logger.Info("Invalid channel ID", "user", client.UserID(), "client", client.ID(), "channel", e.Channel)
@@ -831,7 +831,7 @@ func publishStatusToHTTPError(status backend.PublishStreamStatus) (int, string)
}
// GetChannelHandler gives thread-safe access to the channel.
func (g *GrafanaLive) GetChannelHandler(user *models.SignedInUser, channel string) (models.ChannelHandler, live.Channel, error) {
func (g *GrafanaLive) GetChannelHandler(ctx context.Context, user *models.SignedInUser, channel string) (models.ChannelHandler, live.Channel, error) {
// Parse the identifier ${scope}/${namespace}/${path}
addr, err := live.ParseChannel(channel)
if err != nil {
@@ -854,7 +854,7 @@ func (g *GrafanaLive) GetChannelHandler(user *models.SignedInUser, channel strin
return c, addr, nil
}
getter, err := g.GetChannelHandlerFactory(user, addr.Scope, addr.Namespace)
getter, err := g.GetChannelHandlerFactory(ctx, user, addr.Scope, addr.Namespace)
if err != nil {
return nil, addr, fmt.Errorf("error getting channel handler factory: %w", err)
}
@@ -872,14 +872,14 @@ func (g *GrafanaLive) GetChannelHandler(user *models.SignedInUser, channel strin
// GetChannelHandlerFactory gets a ChannelHandlerFactory for a namespace.
// It gives thread-safe access to the channel.
func (g *GrafanaLive) GetChannelHandlerFactory(user *models.SignedInUser, scope string, namespace string) (models.ChannelHandlerFactory, error) {
func (g *GrafanaLive) GetChannelHandlerFactory(ctx context.Context, user *models.SignedInUser, scope string, namespace string) (models.ChannelHandlerFactory, error) {
switch scope {
case live.ScopeGrafana:
return g.handleGrafanaScope(user, namespace)
case live.ScopePlugin:
return g.handlePluginScope(user, namespace)
return g.handlePluginScope(ctx, user, namespace)
case live.ScopeDatasource:
return g.handleDatasourceScope(user, namespace)
return g.handleDatasourceScope(ctx, user, namespace)
case live.ScopeStream:
return g.handleStreamScope(user, namespace)
default:
@@ -894,7 +894,7 @@ func (g *GrafanaLive) handleGrafanaScope(_ *models.SignedInUser, namespace strin
return nil, fmt.Errorf("unknown feature: %q", namespace)
}
func (g *GrafanaLive) handlePluginScope(_ *models.SignedInUser, namespace string) (models.ChannelHandlerFactory, error) {
func (g *GrafanaLive) handlePluginScope(ctx context.Context, _ *models.SignedInUser, namespace string) (models.ChannelHandlerFactory, error) {
// Temporary hack until we have a more generic solution later on
if namespace == "cloudwatch" {
return &cloudwatch.LogQueryRunnerSupplier{
@@ -902,7 +902,7 @@ func (g *GrafanaLive) handlePluginScope(_ *models.SignedInUser, namespace string
Service: g.LogsService,
}, nil
}
streamHandler, err := g.getStreamPlugin(namespace)
streamHandler, err := g.getStreamPlugin(ctx, namespace)
if err != nil {
return nil, fmt.Errorf("can't find stream plugin: %s", namespace)
}
@@ -919,12 +919,12 @@ func (g *GrafanaLive) handleStreamScope(u *models.SignedInUser, namespace string
return g.ManagedStreamRunner.GetOrCreateStream(u.OrgId, live.ScopeStream, namespace)
}
func (g *GrafanaLive) handleDatasourceScope(user *models.SignedInUser, namespace string) (models.ChannelHandlerFactory, error) {
ds, err := g.DataSourceCache.GetDatasourceByUID(context.TODO(), namespace, user, false)
func (g *GrafanaLive) handleDatasourceScope(ctx context.Context, user *models.SignedInUser, namespace string) (models.ChannelHandlerFactory, error) {
ds, err := g.DataSourceCache.GetDatasourceByUID(ctx, namespace, user, false)
if err != nil {
return nil, fmt.Errorf("error getting datasource: %w", err)
}
streamHandler, err := g.getStreamPlugin(ds.Type)
streamHandler, err := g.getStreamPlugin(ctx, ds.Type)
if err != nil {
return nil, fmt.Errorf("can't find stream plugin: %s", ds.Type)
}
@@ -996,7 +996,7 @@ func (g *GrafanaLive) HandleHTTPPublish(ctx *models.ReqContext) response.Respons
}
}
channelHandler, addr, err := g.GetChannelHandler(ctx.SignedInUser, cmd.Channel)
channelHandler, addr, err := g.GetChannelHandler(ctx.Req.Context(), ctx.SignedInUser, cmd.Channel)
if err != nil {
logger.Error("Error getting channels handler", "error", err, "channel", cmd.Channel)
return response.Error(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), nil)
+3 -2
View File
@@ -1,6 +1,7 @@
package managedstream
import (
"context"
"encoding/json"
"github.com/grafana/grafana-plugin-sdk-go/data"
@@ -11,7 +12,7 @@ type FrameCache interface {
// GetActiveChannels returns active managed stream channels with JSON schema.
GetActiveChannels(orgID int64) (map[string]json.RawMessage, error)
// GetFrame returns full JSON frame for a channel in org.
GetFrame(orgID int64, channel string) (json.RawMessage, bool, error)
GetFrame(ctx context.Context, orgID int64, channel string) (json.RawMessage, bool, error)
// Update updates frame cache and returns true if schema changed.
Update(orgID int64, channel string, frameJson data.FrameJSONCache) (bool, error)
Update(ctx context.Context, orgID int64, channel string, frameJson data.FrameJSONCache) (bool, error)
}
@@ -1,6 +1,7 @@
package managedstream
import (
"context"
"encoding/json"
"sync"
@@ -34,14 +35,14 @@ func (c *MemoryFrameCache) GetActiveChannels(orgID int64) (map[string]json.RawMe
return info, nil
}
func (c *MemoryFrameCache) GetFrame(orgID int64, channel string) (json.RawMessage, bool, error) {
func (c *MemoryFrameCache) GetFrame(ctx context.Context, orgID int64, channel string) (json.RawMessage, bool, error) {
c.mu.RLock()
defer c.mu.RUnlock()
cachedFrame, ok := c.frames[orgID][channel]
return cachedFrame.Bytes(data.IncludeAll), ok, nil
}
func (c *MemoryFrameCache) Update(orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
func (c *MemoryFrameCache) Update(ctx context.Context, orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.frames[orgID]; !ok {
@@ -1,6 +1,7 @@
package managedstream
import (
"context"
"encoding/json"
"testing"
@@ -15,7 +16,7 @@ func testFrameCache(t *testing.T, c FrameCache) {
frameJsonCache, err := data.FrameToJSONCache(frame)
require.NoError(t, err)
updated, err := c.Update(1, "test", frameJsonCache)
updated, err := c.Update(context.Background(), 1, "test", frameJsonCache)
require.NoError(t, err)
require.True(t, updated)
@@ -27,7 +28,7 @@ func testFrameCache(t *testing.T, c FrameCache) {
require.NotZero(t, schema)
// Make sure the same frame does not update schema.
updated, err = c.Update(1, "test", frameJsonCache)
updated, err = c.Update(context.Background(), 1, "test", frameJsonCache)
require.NoError(t, err)
require.False(t, updated)
@@ -37,17 +38,17 @@ func testFrameCache(t *testing.T, c FrameCache) {
require.NoError(t, err)
// Make sure schema updated.
updated, err = c.Update(1, "test", frameJsonCache)
updated, err = c.Update(context.Background(), 1, "test", frameJsonCache)
require.NoError(t, err)
require.True(t, updated)
// Add the same with another orgID and make sure schema updated.
updated, err = c.Update(2, "test", frameJsonCache)
updated, err = c.Update(context.Background(), 2, "test", frameJsonCache)
require.NoError(t, err)
require.True(t, updated)
// Make sure that the last frame successfully saved in cache.
frameJSON, ok, err := c.GetFrame(1, "test")
frameJSON, ok, err := c.GetFrame(context.Background(), 1, "test")
require.NoError(t, err)
require.True(t, ok)
@@ -42,9 +42,9 @@ func (c *RedisFrameCache) GetActiveChannels(orgID int64) (map[string]json.RawMes
return info, nil
}
func (c *RedisFrameCache) GetFrame(orgID int64, channel string) (json.RawMessage, bool, error) {
func (c *RedisFrameCache) GetFrame(ctx context.Context, orgID int64, channel string) (json.RawMessage, bool, error) {
key := getCacheKey(orgchannel.PrependOrgID(orgID, channel))
cmd := c.redisClient.HGetAll(context.TODO(), key)
cmd := c.redisClient.HGetAll(ctx, key)
result, err := cmd.Result()
if err != nil {
return nil, false, err
@@ -59,7 +59,7 @@ const (
frameCacheTTL = 7 * 24 * time.Hour
)
func (c *RedisFrameCache) Update(orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
func (c *RedisFrameCache) Update(ctx context.Context, orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
c.mu.Lock()
if _, ok := c.frames[orgID]; !ok {
c.frames[orgID] = map[string]data.FrameJSONCache{}
@@ -74,8 +74,6 @@ func (c *RedisFrameCache) Update(orgID int64, channel string, jsonFrame data.Fra
pipe := c.redisClient.TxPipeline()
defer func() { _ = pipe.Close() }()
ctx := context.TODO()
pipe.HGetAll(ctx, key)
pipe.HMSet(ctx, key, map[string]string{
"schema": stringSchema,
+4 -4
View File
@@ -170,7 +170,7 @@ func NewNamespaceStream(orgID int64, scope string, namespace string, publisher m
// Push sends frame to the stream and saves it for later retrieval by subscribers.
// * Saves the entire frame to cache.
// * If schema has been changed sends entire frame to channel, otherwise only data.
func (s *NamespaceStream) Push(path string, frame *data.Frame) error {
func (s *NamespaceStream) Push(ctx context.Context, path string, frame *data.Frame) error {
jsonFrameCache, err := data.FrameToJSONCache(frame)
if err != nil {
return err
@@ -179,7 +179,7 @@ func (s *NamespaceStream) Push(path string, frame *data.Frame) error {
// The channel this will be posted into.
channel := live.Channel{Scope: s.scope, Namespace: s.namespace, Path: path}.String()
isUpdated, err := s.frameCache.Update(s.orgID, channel, jsonFrameCache)
isUpdated, err := s.frameCache.Update(ctx, s.orgID, channel, jsonFrameCache)
if err != nil {
logger.Error("Error updating managed stream schema", "error", err)
return err
@@ -238,9 +238,9 @@ func (s *NamespaceStream) GetHandlerForPath(_ string) (models.ChannelHandler, er
return s, nil
}
func (s *NamespaceStream) OnSubscribe(_ context.Context, u *models.SignedInUser, e models.SubscribeEvent) (models.SubscribeReply, backend.SubscribeStreamStatus, error) {
func (s *NamespaceStream) OnSubscribe(ctx context.Context, u *models.SignedInUser, e models.SubscribeEvent) (models.SubscribeReply, backend.SubscribeStreamStatus, error) {
reply := models.SubscribeReply{}
frameJSON, ok, err := s.frameCache.GetFrame(u.OrgId, e.Channel)
frameJSON, ok, err := s.frameCache.GetFrame(ctx, u.OrgId, e.Channel)
if err != nil {
return reply, 0, err
}
@@ -1,6 +1,7 @@
package managedstream
import (
"context"
"testing"
"time"
@@ -57,13 +58,13 @@ func TestGetManagedStreams(t *testing.T) {
require.NoError(t, err)
require.Len(t, managedChannels, 4) // 4 hardcoded testdata streams.
err = s1.Push("cpu1", data.NewFrame("cpu1"))
err = s1.Push(context.Background(), "cpu1", data.NewFrame("cpu1"))
require.NoError(t, err)
err = s1.Push("cpu2", data.NewFrame("cpu2"))
err = s1.Push(context.Background(), "cpu2", data.NewFrame("cpu2"))
require.NoError(t, err)
err = s2.Push("cpu1", data.NewFrame("cpu1"))
err = s2.Push(context.Background(), "cpu1", data.NewFrame("cpu1"))
require.NoError(t, err)
managedChannels, err = runner.GetManagedChannels(1)
@@ -76,7 +77,7 @@ func TestGetManagedStreams(t *testing.T) {
// Different org.
s3, err := runner.GetOrCreateStream(2, "stream", "test1")
require.NoError(t, err)
err = s3.Push("cpu1", data.NewFrame("cpu1"))
err = s3.Push(context.Background(), "cpu1", data.NewFrame("cpu1"))
require.NoError(t, err)
managedChannels, err = runner.GetManagedChannels(1)
require.NoError(t, err)
@@ -29,7 +29,7 @@ func (s *BuiltinDataOutput) OutputData(ctx context.Context, vars Vars, data []by
if !ok {
return nil, errors.New("user not found in context")
}
handler, _, err := s.channelHandlerGetter.GetChannelHandler(u, vars.Channel)
handler, _, err := s.channelHandlerGetter.GetChannelHandler(ctx, u, vars.Channel)
if err != nil {
return nil, err
}
@@ -22,11 +22,11 @@ func (out *ManagedStreamFrameOutput) Type() string {
return FrameOutputTypeManagedStream
}
func (out *ManagedStreamFrameOutput) OutputFrame(_ context.Context, vars Vars, frame *data.Frame) ([]*ChannelFrame, error) {
func (out *ManagedStreamFrameOutput) OutputFrame(ctx context.Context, vars Vars, frame *data.Frame) ([]*ChannelFrame, error) {
stream, err := out.managedStream.GetOrCreateStream(vars.OrgID, vars.Scope, vars.Namespace)
if err != nil {
logger.Error("Error getting stream", "error", err)
return nil, err
}
return nil, stream.Push(vars.Path, frame)
return nil, stream.Push(ctx, vars.Path, frame)
}
@@ -15,7 +15,7 @@ type BuiltinSubscriber struct {
}
type ChannelHandlerGetter interface {
GetChannelHandler(user *models.SignedInUser, channel string) (models.ChannelHandler, live.Channel, error)
GetChannelHandler(ctx context.Context, user *models.SignedInUser, channel string) (models.ChannelHandler, live.Channel, error)
}
const SubscriberTypeBuiltin = "builtin"
@@ -33,7 +33,7 @@ func (s *BuiltinSubscriber) Subscribe(ctx context.Context, vars Vars, data []byt
if !ok {
return models.SubscribeReply{}, backend.SubscribeStreamStatusPermissionDenied, nil
}
handler, _, err := s.channelHandlerGetter.GetChannelHandler(u, vars.Channel)
handler, _, err := s.channelHandlerGetter.GetChannelHandler(ctx, u, vars.Channel)
if err != nil {
return models.SubscribeReply{}, 0, err
}
+1 -1
View File
@@ -87,7 +87,7 @@ func (g *Gateway) Handle(ctx *models.ReqContext) {
// interval = "1s" vs flush_interval = "5s"
for _, mf := range metricFrames {
err := stream.Push(mf.Key(), mf.Frame())
err := stream.Push(ctx.Req.Context(), mf.Key(), mf.Frame())
if err != nil {
logger.Error("Error pushing frame", "error", err, "data", string(body))
ctx.Resp.WriteHeader(http.StatusInternalServerError)
+1 -1
View File
@@ -91,7 +91,7 @@ func (s *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
}
for _, mf := range metricFrames {
err := stream.Push(mf.Key(), mf.Frame())
err := stream.Push(r.Context(), mf.Key(), mf.Frame())
if err != nil {
logger.Error("Error pushing frame", "error", err, "data", string(body))
return