mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Add context to StaticRouteResolver and ErrorResolver interfaces (#73121)
* add ctx * fix tests
This commit is contained in:
@@ -48,6 +48,6 @@ type fakePluginStaticRouteResolver struct {
|
||||
routes []*plugins.StaticRoute
|
||||
}
|
||||
|
||||
func (psrr *fakePluginStaticRouteResolver) Routes() []*plugins.StaticRoute {
|
||||
func (psrr *fakePluginStaticRouteResolver) Routes(_ context.Context) []*plugins.StaticRoute {
|
||||
return psrr.routes
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ func GrafanaJavascriptAgentLogMessageHandler(store *frontendlogging.SourceMapSto
|
||||
var ctx = frontendlogging.CtxVector{}
|
||||
ctx = event.AddMetaToContext(ctx)
|
||||
exception := exception
|
||||
transformedException := frontendlogging.TransformException(&exception, store)
|
||||
transformedException := frontendlogging.TransformException(c.Req.Context(), &exception, store)
|
||||
ctx = append(ctx, "kind", "exception", "type", transformedException.Type, "value", transformedException.Value, "stacktrace", transformedException.String())
|
||||
ctx = append(ctx, "original_timestamp", exception.Timestamp)
|
||||
frontendLogger.Error(exception.Message(), ctx...)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package frontendlogging
|
||||
|
||||
// TransformException will attempt to resolved all monified source locations in the stacktrace with original source locations
|
||||
func TransformException(ex *Exception, store *SourceMapStore) *Exception {
|
||||
import "context"
|
||||
|
||||
// TransformException will attempt to resolve all modified source locations in the stacktrace with original source locations
|
||||
func TransformException(ctx context.Context, ex *Exception, store *SourceMapStore) *Exception {
|
||||
if ex.Stacktrace == nil {
|
||||
return ex
|
||||
}
|
||||
@@ -9,7 +11,7 @@ func TransformException(ex *Exception, store *SourceMapStore) *Exception {
|
||||
|
||||
for _, frame := range ex.Stacktrace.Frames {
|
||||
frame := frame
|
||||
mappedFrame, err := store.resolveSourceLocation(frame)
|
||||
mappedFrame, err := store.resolveSourceLocation(ctx, frame)
|
||||
if err != nil {
|
||||
frames = append(frames, frame)
|
||||
} else if mappedFrame != nil {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package frontendlogging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -65,7 +66,7 @@ func NewSourceMapStore(cfg *setting.Cfg, routeResolver plugins.StaticRouteResolv
|
||||
* just assumes that a [source filename].map file might exist in the same dir as the source file
|
||||
* and only considers sources coming from grafana core or plugins`
|
||||
*/
|
||||
func (store *SourceMapStore) guessSourceMapLocation(sourceURL string) (*sourceMapLocation, error) {
|
||||
func (store *SourceMapStore) guessSourceMapLocation(ctx context.Context, sourceURL string) (*sourceMapLocation, error) {
|
||||
u, err := url.Parse(sourceURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -84,7 +85,7 @@ func (store *SourceMapStore) guessSourceMapLocation(sourceURL string) (*sourceMa
|
||||
}
|
||||
// if source comes from a plugin, look in plugin dir
|
||||
} else if strings.HasPrefix(u.Path, "/public/plugins/") {
|
||||
for _, route := range store.routeResolver.Routes() {
|
||||
for _, route := range store.routeResolver.Routes(ctx) {
|
||||
pluginPrefix := filepath.Join("/public/plugins/", route.PluginID)
|
||||
if strings.HasPrefix(u.Path, pluginPrefix) {
|
||||
return &sourceMapLocation{
|
||||
@@ -98,14 +99,14 @@ func (store *SourceMapStore) guessSourceMapLocation(sourceURL string) (*sourceMa
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (store *SourceMapStore) getSourceMap(sourceURL string) (*sourceMap, error) {
|
||||
func (store *SourceMapStore) getSourceMap(ctx context.Context, sourceURL string) (*sourceMap, error) {
|
||||
store.Lock()
|
||||
defer store.Unlock()
|
||||
|
||||
if smap, ok := store.cache[sourceURL]; ok {
|
||||
return smap, nil
|
||||
}
|
||||
sourceMapLocation, err := store.guessSourceMapLocation(sourceURL)
|
||||
sourceMapLocation, err := store.guessSourceMapLocation(ctx, sourceURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,8 +138,8 @@ func (store *SourceMapStore) getSourceMap(sourceURL string) (*sourceMap, error)
|
||||
return smap, nil
|
||||
}
|
||||
|
||||
func (store *SourceMapStore) resolveSourceLocation(frame Frame) (*Frame, error) {
|
||||
smap, err := store.getSourceMap(frame.Filename)
|
||||
func (store *SourceMapStore) resolveSourceLocation(ctx context.Context, frame Frame) (*Frame, error) {
|
||||
smap, err := store.getSourceMap(ctx, frame.Filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -431,8 +431,8 @@ func (hs *HTTPServer) CheckHealth(c *contextmodel.ReqContext) response.Response
|
||||
return response.JSON(http.StatusOK, payload)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) GetPluginErrorsList(_ *contextmodel.ReqContext) response.Response {
|
||||
return response.JSON(http.StatusOK, hs.pluginErrorResolver.PluginErrors())
|
||||
func (hs *HTTPServer) GetPluginErrorsList(c *contextmodel.ReqContext) response.Response {
|
||||
return response.JSON(http.StatusOK, hs.pluginErrorResolver.PluginErrors(c.Req.Context()))
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) InstallPlugin(c *contextmodel.ReqContext) response.Response {
|
||||
|
||||
Reference in New Issue
Block a user