From 5c9a10d423c40434717ecb74106612334f8eab3a Mon Sep 17 00:00:00 2001 From: Domas Date: Thu, 11 Feb 2021 11:00:55 +0200 Subject: [PATCH] Logging: sourcemap transform asset urls from CDN in logged stacktraces (#31115) --- pkg/api/frontend_logging_test.go | 18 ++++++++++++++++-- pkg/api/frontendlogging/source_maps.go | 17 ++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkg/api/frontend_logging_test.go b/pkg/api/frontend_logging_test.go index f04f35e42ce..ee78f694a86 100644 --- a/pkg/api/frontend_logging_test.go +++ b/pkg/api/frontend_logging_test.go @@ -4,6 +4,7 @@ import ( "errors" "io/ioutil" "net/http" + "net/url" "os" "strings" "testing" @@ -45,8 +46,12 @@ func logSentryEventScenario(t *testing.T, desc string, event frontendlogging.Fro sc := setupScenarioContext(t, "/log") + cdnRootURL, e := url.Parse("https://storage.googleapis.com/grafana-static-assets") + require.NoError(t, e) + cfg := &setting.Cfg{ StaticRootPath: "/staticroot", + CDNRootURL: cdnRootURL, } readSourceMap := func(dir string, path string) ([]byte, error) { @@ -252,6 +257,12 @@ func TestFrontendLoggingEndpoint(t *testing.T) { Lineno: 3, Colno: 10, }, + { + Function: "cdn", + Filename: "https://storage.googleapis.com/grafana-static-assets/grafana-oss/pre-releases/7.5.0-11925pre/public/build/foo.js", // source map found and mapped + Lineno: 3, + Colno: 10, + }, }, }, }, @@ -268,8 +279,9 @@ func TestFrontendLoggingEndpoint(t *testing.T) { at explode (http://localhost:3000/public/build/error.js:3:10) at wat (http://localhost:3000/public/build/bar.js:3:10) at nope (http://localhost:3000/baz.js:3:10) - at fake (http://localhost:3000/public/build/../../secrets.txt:3:10)`) - assert.Len(t, sourceMapReads, 5) + at fake (http://localhost:3000/public/build/../../secrets.txt:3:10) + at ? (core|webpack:///./some_source.ts:3:2)`) + assert.Len(t, sourceMapReads, 6) assert.Equal(t, "/staticroot", sourceMapReads[0].dir) assert.Equal(t, "build/moo/foo.js.map", sourceMapReads[0].path) assert.Equal(t, "/usr/local/telepathic-panel", sourceMapReads[1].dir) @@ -280,6 +292,8 @@ func TestFrontendLoggingEndpoint(t *testing.T) { assert.Equal(t, "build/bar.js.map", sourceMapReads[3].path) assert.Equal(t, "/staticroot", sourceMapReads[4].dir) assert.Equal(t, "secrets.txt.map", sourceMapReads[4].path) + assert.Equal(t, "/staticroot", sourceMapReads[5].dir) + assert.Equal(t, "build/foo.js.map", sourceMapReads[5].path) }) }) } diff --git a/pkg/api/frontendlogging/source_maps.go b/pkg/api/frontendlogging/source_maps.go index 151903662df..818bf7649cd 100644 --- a/pkg/api/frontendlogging/source_maps.go +++ b/pkg/api/frontendlogging/source_maps.go @@ -68,13 +68,16 @@ func (store *SourceMapStore) guessSourceMapLocation(sourceURL string) (*sourceMa return nil, err } - // determine if source comes from grafana core, look in public build dir - if strings.HasPrefix(u.Path, "/public/build/") { - return &sourceMapLocation{ - dir: store.cfg.StaticRootPath, - path: filepath.Join("build", u.Path[len("/public/build/"):]) + ".map", - pluginID: "", - }, nil + // determine if source comes from grafana core, locally or CDN, look in public build dir on fs + if strings.HasPrefix(u.Path, "/public/build/") || (store.cfg.CDNRootURL != nil && strings.HasPrefix(sourceURL, store.cfg.CDNRootURL.String()) && strings.Contains(u.Path, "/public/build/")) { + pathParts := strings.SplitN(u.Path, "/public/build/", 2) + if len(pathParts) == 2 { + return &sourceMapLocation{ + dir: store.cfg.StaticRootPath, + path: filepath.Join("build", pathParts[1]+".map"), + pluginID: "", + }, nil + } // if source comes from a plugin, look in plugin dir } else if strings.HasPrefix(u.Path, "/public/plugins/") { for _, route := range plugins.StaticRoutes {