Frontend logging: handle logging endpoints without expensive middleware (#54960) (#55019)

(cherry picked from commit b5c67fe0dd)
This commit is contained in:
Domas
2022-09-12 13:38:03 +03:00
committed by GitHub
parent a41e6ae453
commit 28997ed169
6 changed files with 99 additions and 142 deletions
-10
View File
@@ -31,9 +31,6 @@
package api
import (
"time"
"github.com/grafana/grafana/pkg/api/frontendlogging"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/middleware"
@@ -642,11 +639,4 @@ func (hs *HTTPServer) registerRoutes() {
r.Get("/api/snapshots/:key", routing.Wrap(hs.GetDashboardSnapshot))
r.Get("/api/snapshots-delete/:deleteKey", reqSnapshotPublicModeOrSignedIn, routing.Wrap(hs.DeleteDashboardSnapshotByDeleteKey))
r.Delete("/api/snapshots/:key", reqEditorRole, routing.Wrap(hs.DeleteDashboardSnapshot))
// Frontend logs
sourceMapStore := frontendlogging.NewSourceMapStore(hs.Cfg, hs.pluginStaticRouteResolver, frontendlogging.ReadSourceMapFromFS)
r.Post("/log", middleware.RateLimit(hs.Cfg.Sentry.EndpointRPS, hs.Cfg.Sentry.EndpointBurst, time.Now),
routing.Wrap(NewFrontendLogMessageHandler(sourceMapStore)))
r.Post("/log-grafana-javascript-agent", middleware.RateLimit(hs.Cfg.GrafanaJavascriptAgent.EndpointRPS, hs.Cfg.GrafanaJavascriptAgent.EndpointBurst, time.Now),
routing.Wrap(GrafanaJavascriptAgentLogMessageHandler(sourceMapStore)))
}
+84 -9
View File
@@ -2,24 +2,33 @@ package api
import (
"net/http"
"time"
"github.com/getsentry/sentry-go"
"golang.org/x/time/rate"
"github.com/grafana/grafana/pkg/api/frontendlogging"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/web"
)
var frontendLogger = log.New("frontend")
type frontendLogMessageHandler func(c *models.ReqContext) response.Response
type frontendLogMessageHandler func(hs *HTTPServer, c *web.Context)
const sentryLogEndpointPath = "/log"
const grafanaJavascriptAgentEndpointPath = "/log-grafana-javascript-agent"
func NewFrontendLogMessageHandler(store *frontendlogging.SourceMapStore) frontendLogMessageHandler {
return func(c *models.ReqContext) response.Response {
return func(hs *HTTPServer, c *web.Context) {
event := frontendlogging.FrontendSentryEvent{}
if err := web.Bind(c.Req, &event); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
c.Resp.WriteHeader(http.StatusBadRequest)
_, err = c.Resp.Write([]byte("bad request data"))
if err != nil {
hs.log.Error("could not write to response", "err", err)
}
return
}
var msg = "unknown"
@@ -43,15 +52,23 @@ func NewFrontendLogMessageHandler(store *frontendlogging.SourceMapStore) fronten
frontendLogger.Info(msg, ctx...)
}
return response.Success("ok")
c.Resp.WriteHeader(http.StatusAccepted)
_, err := c.Resp.Write([]byte("OK"))
if err != nil {
hs.log.Error("could not write to response", "err", err)
}
}
}
func GrafanaJavascriptAgentLogMessageHandler(store *frontendlogging.SourceMapStore) frontendLogMessageHandler {
return func(c *models.ReqContext) response.Response {
return func(hs *HTTPServer, c *web.Context) {
event := frontendlogging.FrontendGrafanaJavascriptAgentEvent{}
if err := web.Bind(c.Req, &event); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
c.Resp.WriteHeader(http.StatusBadRequest)
_, err = c.Resp.Write([]byte("bad request data"))
if err != nil {
hs.log.Error("could not write to response", "err", err)
}
}
// Meta object is standard across event types, adding it globally.
@@ -112,6 +129,64 @@ func GrafanaJavascriptAgentLogMessageHandler(store *frontendlogging.SourceMapSto
frontendLogger.Error(exception.Message(), ctx...)
}
}
return response.Success("ok")
c.Resp.WriteHeader(http.StatusAccepted)
_, err := c.Resp.Write([]byte("OK"))
if err != nil {
hs.log.Error("could not write to response", "err", err)
}
}
}
// setupFrontendLogHandlers will set up handlers for logs incoming from frontend.
// handlers are setup even if frontend logging is disabled, but in this case do nothing
// this is to avoid reporting errors in case config was changes but there are browser
// sessions still open with older config
func (hs *HTTPServer) frontendLogEndpoints() web.Handler {
if !(hs.Cfg.GrafanaJavascriptAgent.Enabled || hs.Cfg.Sentry.Enabled) {
return func(ctx *web.Context) {
if ctx.Req.Method == http.MethodPost && (ctx.Req.URL.Path == sentryLogEndpointPath || ctx.Req.URL.Path == grafanaJavascriptAgentEndpointPath) {
ctx.Resp.WriteHeader(http.StatusAccepted)
_, err := ctx.Resp.Write([]byte("OK"))
if err != nil {
hs.log.Error("could not write to response", "err", err)
}
}
}
}
sourceMapStore := frontendlogging.NewSourceMapStore(hs.Cfg, hs.pluginStaticRouteResolver, frontendlogging.ReadSourceMapFromFS)
var rateLimiter *rate.Limiter
var handler frontendLogMessageHandler
handlerEndpoint := ""
dummyEndpoint := ""
if hs.Cfg.GrafanaJavascriptAgent.Enabled {
rateLimiter = rate.NewLimiter(rate.Limit(hs.Cfg.GrafanaJavascriptAgent.EndpointRPS), hs.Cfg.GrafanaJavascriptAgent.EndpointBurst)
handler = GrafanaJavascriptAgentLogMessageHandler(sourceMapStore)
handlerEndpoint = grafanaJavascriptAgentEndpointPath
dummyEndpoint = sentryLogEndpointPath
} else {
rateLimiter = rate.NewLimiter(rate.Limit(hs.Cfg.Sentry.EndpointRPS), hs.Cfg.Sentry.EndpointBurst)
handler = NewFrontendLogMessageHandler(sourceMapStore)
handlerEndpoint = sentryLogEndpointPath
dummyEndpoint = grafanaJavascriptAgentEndpointPath
}
return func(ctx *web.Context) {
if ctx.Req.Method == http.MethodPost && ctx.Req.URL.Path == dummyEndpoint {
ctx.Resp.WriteHeader(http.StatusAccepted)
_, err := ctx.Resp.Write([]byte("OK"))
if err != nil {
hs.log.Error("could not write to response", "err", err)
}
}
if ctx.Req.Method == http.MethodPost && ctx.Req.URL.Path == handlerEndpoint {
if !rateLimiter.AllowN(time.Now(), 1) {
ctx.Resp.WriteHeader(http.StatusTooManyRequests)
return
}
handler(hs, ctx)
}
}
}
+14 -11
View File
@@ -3,6 +3,7 @@ package api
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
@@ -93,7 +94,8 @@ func logSentryEventScenario(t *testing.T, desc string, event frontendlogging.Fro
sc.context = c
c.Req.Body = mockRequestBody(event)
c.Req.Header.Add("Content-Type", "application/json")
return loggingHandler(c)
loggingHandler(nil, c.Context)
return response.Success("ok")
})
sc.m.Post(sc.url, handler)
@@ -165,7 +167,8 @@ func logGrafanaJavascriptAgentEventScenario(t *testing.T, desc string, event fro
sc.context = c
c.Req.Body = mockRequestBody(event)
c.Req.Header.Add("Content-Type", "application/json")
return loggingHandler(c)
loggingHandler(nil, c.Context)
return response.Success("OK")
})
sc.m.Post(sc.url, handler)
@@ -228,7 +231,7 @@ func TestFrontendLoggingEndpointSentry(t *testing.T) {
logSentryEventScenario(t, "Should log received error event", errorEvent,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assertContextContains(t, logs, "logger", "frontend")
assertContextContains(t, logs, "url", errorEvent.Request.URL)
assertContextContains(t, logs, "user_agent", errorEvent.Request.Headers["User-Agent"])
@@ -254,7 +257,7 @@ func TestFrontendLoggingEndpointSentry(t *testing.T) {
logSentryEventScenario(t, "Should log received message event", messageEvent,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assert.Len(t, logs, 10)
assertContextContains(t, logs, "logger", "frontend")
assertContextContains(t, logs, "msg", "hello world")
@@ -291,7 +294,7 @@ func TestFrontendLoggingEndpointSentry(t *testing.T) {
logSentryEventScenario(t, "Should log event context", eventWithContext,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assertContextContains(t, logs, "context_foo_one", "two")
assertContextContains(t, logs, "context_foo_three", "4")
assertContextContains(t, logs, "context_bar", "baz")
@@ -357,7 +360,7 @@ func TestFrontendLoggingEndpointSentry(t *testing.T) {
logSentryEventScenario(t, "Should load sourcemap and transform stacktrace line when possible",
errorEventForSourceMapping, func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assert.Len(t, logs, 9)
assertContextContains(t, logs, "stacktrace", `UserError: Please replace user and try again
at ? (core|webpack:///./some_source.ts:2:2)
@@ -421,7 +424,7 @@ func TestFrontendLoggingEndpointGrafanaJavascriptAgent(t *testing.T) {
logGrafanaJavascriptAgentEventScenario(t, "Should log received error event", errorEvent,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assertContextContains(t, logs, "logger", "frontend")
assertContextContains(t, logs, "page_url", errorEvent.Meta.Page.URL)
assertContextContains(t, logs, "user_email", errorEvent.Meta.User.Email)
@@ -444,7 +447,7 @@ func TestFrontendLoggingEndpointGrafanaJavascriptAgent(t *testing.T) {
logGrafanaJavascriptAgentEventScenario(t, "Should log received log event", logEvent,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assert.Len(t, logs, 11)
assertContextContains(t, logs, "logger", "frontend")
assertContextContains(t, logs, "msg", "This is a test log message")
@@ -469,7 +472,7 @@ func TestFrontendLoggingEndpointGrafanaJavascriptAgent(t *testing.T) {
logGrafanaJavascriptAgentEventScenario(t, "Should log received log context", logEventWithContext,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assertContextContains(t, logs, "context_one", "two")
assertContextContains(t, logs, "context_bar", "baz")
})
@@ -532,7 +535,7 @@ func TestFrontendLoggingEndpointGrafanaJavascriptAgent(t *testing.T) {
logGrafanaJavascriptAgentEventScenario(t, "Should load sourcemap and transform stacktrace line when possible", errorEventForSourceMapping,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assertContextContains(t, logs, "stacktrace", `UserError: Please replace user and try again
at ? (webpack:///./some_source.ts:2:2)
at ? (webpack:///./some_source.ts:3:2)
@@ -568,7 +571,7 @@ func TestFrontendLoggingEndpointGrafanaJavascriptAgent(t *testing.T) {
logGrafanaJavascriptAgentEventScenario(t, "Should log web vitals as context", logWebVitals,
func(sc *scenarioContext, logs map[string]interface{}, sourceMapReads []SourceMapReadRecord) {
assert.Equal(t, 200, sc.resp.Code)
assert.Equal(t, http.StatusAccepted, sc.resp.Code)
assertContextContains(t, logs, "CLS", float64(1))
})
})
+1
View File
@@ -550,6 +550,7 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() {
m.Use(hs.apiHealthHandler)
m.Use(hs.metricsEndpoint)
m.Use(hs.pluginMetricsEndpoint)
m.Use(hs.frontendLogEndpoints())
m.Use(hs.ContextHandler.Middleware)
m.Use(middleware.OrgRedirect(hs.Cfg, hs.SQLStore))