-
Notifications
You must be signed in to change notification settings - Fork 485
/
sourcemaps.go
357 lines (313 loc) · 11.2 KB
/
sourcemaps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package app_agent_receiver
import (
"bytes"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"text/template"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/go-sourcemap/sourcemap"
"github.com/prometheus/client_golang/prometheus"
"github.com/vincent-petithory/dataurl"
)
// SourceMapStore is interface for a sourcemap service capable of transforming
// minified source locations to original source location
type SourceMapStore interface {
GetSourceMap(sourceURL string, release string) (*SourceMap, error)
}
type httpClient interface {
Get(url string) (resp *http.Response, err error)
}
// FileService is interface for a service that can be used to load source maps
// from file system
type fileService interface {
Stat(name string) (fs.FileInfo, error)
ReadFile(name string) ([]byte, error)
}
type osFileService struct{}
func (s *osFileService) Stat(name string) (fs.FileInfo, error) {
return os.Stat(name)
}
func (s *osFileService) ReadFile(name string) ([]byte, error) {
return os.ReadFile(name)
}
var reSourceMap = "//[#@]\\s(source(?:Mapping)?URL)=\\s*(?P<url>\\S+)\r?\n?$"
// SourceMap is a wrapper for go-sourcemap consumer
type SourceMap struct {
consumer *sourcemap.Consumer
}
type sourceMapMetrics struct {
cacheSize *prometheus.CounterVec
downloads *prometheus.CounterVec
fileReads *prometheus.CounterVec
}
type sourcemapFileLocation struct {
SourceMapFileLocation
pathTemplate *template.Template
}
// RealSourceMapStore is an implementation of SourceMapStore
// that can download source maps or read them from file system
type RealSourceMapStore struct {
sync.Mutex
l log.Logger
httpClient httpClient
fileService fileService
config SourceMapConfig
cache map[string]*SourceMap
fileLocations []*sourcemapFileLocation
metrics *sourceMapMetrics
}
// NewSourceMapStore creates an instance of SourceMapStore.
// httpClient and fileService will be instantiated to defaults if nil is provided
func NewSourceMapStore(l log.Logger, config SourceMapConfig, reg prometheus.Registerer, httpClient httpClient, fileService fileService) SourceMapStore {
if httpClient == nil {
httpClient = &http.Client{
Timeout: config.DownloadTimeout,
}
}
if fileService == nil {
fileService = &osFileService{}
}
metrics := &sourceMapMetrics{
cacheSize: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "app_agent_receiver_sourcemap_cache_size",
Help: "number of items in source map cache, per origin",
}, []string{"origin"}),
downloads: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "app_agent_receiver_sourcemap_downloads_total",
Help: "downloads by the source map service",
}, []string{"origin", "http_status"}),
fileReads: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "app_agent_receiver_sourcemap_file_reads_total",
Help: "source map file reads from file system, by origin and status",
}, []string{"origin", "status"}),
}
reg.MustRegister(metrics.cacheSize, metrics.downloads, metrics.fileReads)
fileLocations := []*sourcemapFileLocation{}
for _, configLocation := range config.FileSystem {
tpl, err := template.New(configLocation.Path).Parse(configLocation.Path)
if err != nil {
panic(err)
}
fileLocations = append(fileLocations, &sourcemapFileLocation{
SourceMapFileLocation: configLocation,
pathTemplate: tpl,
})
}
return &RealSourceMapStore{
l: l,
httpClient: httpClient,
fileService: fileService,
config: config,
cache: make(map[string]*SourceMap),
metrics: metrics,
fileLocations: fileLocations,
}
}
func (store *RealSourceMapStore) downloadFileContents(url string) ([]byte, error) {
resp, err := store.httpClient.Get(url)
if err != nil {
store.metrics.downloads.WithLabelValues(getOrigin(url), "?").Inc()
return nil, err
}
defer resp.Body.Close()
store.metrics.downloads.WithLabelValues(getOrigin(url), fmt.Sprint(resp.StatusCode)).Inc()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status %v", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (store *RealSourceMapStore) downloadSourceMapContent(sourceURL string) (content []byte, resolvedSourceMapURL string, err error) {
level.Debug(store.l).Log("msg", "attempting to download source file", "url", sourceURL)
result, err := store.downloadFileContents(sourceURL)
if err != nil {
level.Debug(store.l).Log("msg", "failed to download source file", "url", sourceURL, "err", err)
return nil, "", err
}
r := regexp.MustCompile(reSourceMap)
match := r.FindAllStringSubmatch(string(result), -1)
if len(match) == 0 {
level.Debug(store.l).Log("msg", "no source map url found in source", "url", sourceURL)
return nil, "", nil
}
sourceMapURL := match[len(match)-1][2]
// inline sourcemap
if strings.HasPrefix(sourceMapURL, "data:") {
dataURL, err := dataurl.DecodeString(sourceMapURL)
if err != nil {
level.Debug(store.l).Log("msg", "failed to parse inline source map data url", "url", sourceURL, "err", err)
return nil, "", err
}
level.Info(store.l).Log("msg", "successfully parsed inline source map data url", "url", sourceURL)
return dataURL.Data, sourceURL + ".map", nil
}
// remote sourcemap
resolvedSourceMapURL = sourceMapURL
// if url is relative, attempt to resolve absolute
if !strings.HasPrefix(resolvedSourceMapURL, "http") {
base, err := url.Parse(sourceURL)
if err != nil {
level.Debug(store.l).Log("msg", "failed to parse source url", "url", sourceURL, "err", err)
return nil, "", err
}
relative, err := url.Parse(sourceMapURL)
if err != nil {
level.Debug(store.l).Log("msg", "failed to parse source map url", "url", sourceURL, "sourceMapURL", sourceMapURL, "err", err)
return nil, "", err
}
resolvedSourceMapURL = base.ResolveReference(relative).String()
level.Debug(store.l).Log("msg", "resolved absolute source map url", "url", sourceURL, "sourceMapURL", resolvedSourceMapURL)
}
level.Debug(store.l).Log("msg", "attempting to download source map file", "url", resolvedSourceMapURL)
result, err = store.downloadFileContents(resolvedSourceMapURL)
if err != nil {
level.Debug(store.l).Log("failed to download source map file", "url", resolvedSourceMapURL, "err", err)
return nil, "", err
}
return result, resolvedSourceMapURL, nil
}
func (store *RealSourceMapStore) getSourceMapFromFileSystem(sourceURL string, release string, fileconf *sourcemapFileLocation) (content []byte, sourceMapURL string, err error) {
if len(sourceURL) == 0 || !strings.HasPrefix(sourceURL, fileconf.MinifiedPathPrefix) || strings.HasSuffix(sourceURL, "/") {
return nil, "", nil
}
var rootPath bytes.Buffer
err = fileconf.pathTemplate.Execute(&rootPath, struct{ Release string }{Release: cleanFilePathPart(release)})
if err != nil {
return nil, "", err
}
pathParts := []string{rootPath.String()}
for _, part := range strings.Split(strings.TrimPrefix(strings.Split(sourceURL, "?")[0], fileconf.MinifiedPathPrefix), "/") {
if len(part) > 0 && part != "." && part != ".." {
pathParts = append(pathParts, part)
}
}
mapFilePath := filepath.Join(pathParts...) + ".map"
if _, err := store.fileService.Stat(mapFilePath); err != nil {
store.metrics.fileReads.WithLabelValues(getOrigin(sourceURL), "not_found").Inc()
level.Debug(store.l).Log("msg", "source map not found on filesystem", "url", sourceURL, "file_path", mapFilePath)
return nil, "", nil
}
level.Debug(store.l).Log("msg", "source map found on filesystem", "url", mapFilePath, "file_path", mapFilePath)
content, err = store.fileService.ReadFile(mapFilePath)
if err != nil {
store.metrics.fileReads.WithLabelValues(getOrigin(sourceURL), "error").Inc()
} else {
store.metrics.fileReads.WithLabelValues(getOrigin(sourceURL), "ok").Inc()
}
return content, sourceURL, err
}
func (store *RealSourceMapStore) getSourceMapContent(sourceURL string, release string) (content []byte, sourceMapURL string, err error) {
//attempt to find in fs
for _, fileconf := range store.fileLocations {
content, sourceMapURL, err = store.getSourceMapFromFileSystem(sourceURL, release, fileconf)
if content != nil || err != nil {
return content, sourceMapURL, err
}
}
//attempt to download
if strings.HasPrefix(sourceURL, "http") && urlMatchesOrigins(sourceURL, store.config.DownloadFromOrigins) {
return store.downloadSourceMapContent(sourceURL)
}
return nil, "", nil
}
// GetSourceMap returns sourcemap for a given source url
func (store *RealSourceMapStore) GetSourceMap(sourceURL string, release string) (*SourceMap, error) {
store.Lock()
defer store.Unlock()
cacheKey := fmt.Sprintf("%s__%s", sourceURL, release)
if smap, ok := store.cache[cacheKey]; ok {
return smap, nil
}
content, sourceMapURL, err := store.getSourceMapContent(sourceURL, release)
if err != nil || content == nil {
store.cache[cacheKey] = nil
return nil, err
}
if content != nil {
consumer, err := sourcemap.Parse(sourceMapURL, content)
if err != nil {
store.cache[cacheKey] = nil
level.Debug(store.l).Log("msg", "failed to parse source map", "url", sourceMapURL, "release", release, "err", err)
return nil, err
}
level.Info(store.l).Log("msg", "successfully parsed source map", "url", sourceMapURL, "release", release)
smap := &SourceMap{
consumer: consumer,
}
store.cache[cacheKey] = smap
store.metrics.cacheSize.WithLabelValues(getOrigin(sourceURL)).Inc()
return smap, nil
}
return nil, nil
}
// ResolveSourceLocation resolves minified source location to original source location
func ResolveSourceLocation(store SourceMapStore, frame *Frame, release string) (*Frame, error) {
smap, err := store.GetSourceMap(frame.Filename, release)
if err != nil {
return nil, err
}
if smap == nil {
return nil, nil
}
file, function, line, col, ok := smap.consumer.Source(frame.Lineno, frame.Colno)
if !ok {
return nil, nil
}
// unfortunately in many cases go-sourcemap fails to determine the original function name.
// not a big issue as long as file, line and column are correct
if len(function) == 0 {
function = "?"
}
return &Frame{
Filename: file,
Lineno: line,
Colno: col,
Function: function,
}, nil
}
// TransformException will attempt to resolve all minified source locations in the stacktrace with original source locations
func TransformException(store SourceMapStore, log log.Logger, ex *Exception, release string) *Exception {
if ex.Stacktrace == nil {
return ex
}
frames := []Frame{}
for _, frame := range ex.Stacktrace.Frames {
mappedFrame, err := ResolveSourceLocation(store, &frame, release)
if err != nil {
level.Error(log).Log("msg", "Error resolving stack trace frame source location", "err", err)
frames = append(frames, frame)
} else if mappedFrame != nil {
frames = append(frames, *mappedFrame)
} else {
frames = append(frames, frame)
}
}
return &Exception{
Type: ex.Type,
Value: ex.Value,
Stacktrace: &Stacktrace{Frames: frames},
Timestamp: ex.Timestamp,
}
}
func cleanFilePathPart(x string) string {
return strings.TrimLeft(strings.ReplaceAll(strings.ReplaceAll(x, "\\", ""), "/", ""), ".")
}
func getOrigin(URL string) string {
parsed, err := url.Parse(URL)
if err != nil {
return "?"
}
return fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host)
}