Skip to content

Commit f200016

Browse files
authored
feat(go): add request response size metric (#833)
1 parent 2d1ba90 commit f200016

File tree

3 files changed

+156
-30
lines changed

3 files changed

+156
-30
lines changed

pkg/metrics/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func NewRegistry() *prometheus.Registry {
3030
service.MetricBrowserGetVersionDuration,
3131
service.MetricBrowserRenderCSVDuration,
3232
service.MetricBrowserRenderDuration,
33+
service.MetricBrowserRequestSize,
3334
service.MetricBrowserInstancesActive,
3435
service.MetricProcessMaxMemory,
3536
service.MetricProcessPeakMemoryAverage,

pkg/service/browser.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ var (
6767
},
6868
Buckets: []float64{0.01, 0.03, 0.05, 0.1, 0.3, 0.5, 1, 3, 5, 7, 10, 15, 20, 30, 50, 70, 100, 150, 300},
6969
}, []string{"action"})
70+
MetricBrowserRequestSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
71+
Name: "browser_request_size",
72+
Help: "How large is the average response from a browser request? This is a best-effort measure, and may not be entirely accurate.",
73+
ConstLabels: prometheus.Labels{
74+
"unit": "bytes",
75+
},
76+
Buckets: []float64{1, 1024, 4 * 1024, 16 * 1024, 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024, 64 * 1024 * 1024, 256 * 1024 * 1024},
77+
}, []string{"mime_type"})
7078
)
7179

7280
var ErrInvalidBrowserOption = errors.New("invalid browser option")
@@ -400,7 +408,11 @@ func (s *BrowserService) createAllocatorOptions(cfg config.BrowserConfig) ([]chr
400408

401409
func (s *BrowserService) handleNetworkEvents(browserCtx context.Context) {
402410
requests := make(map[network.RequestID]trace.Span)
403-
mu := &sync.Mutex{}
411+
requestsMutex := &sync.Mutex{}
412+
413+
requestMimes := make(map[network.RequestID]string)
414+
requestMimesMutex := &sync.Mutex{}
415+
404416
tracer := tracer(browserCtx)
405417

406418
chromedp.ListenTarget(browserCtx, func(ev any) {
@@ -437,9 +449,6 @@ func (s *BrowserService) handleNetworkEvents(browserCtx context.Context) {
437449
}()
438450

439451
case *network.EventRequestWillBeSent:
440-
mu.Lock()
441-
defer mu.Unlock()
442-
443452
_, span := tracer.Start(browserCtx, "Browser HTTP request",
444453
trace.WithTimestamp(e.Timestamp.Time()),
445454
trace.WithAttributes(
@@ -448,13 +457,20 @@ func (s *BrowserService) handleNetworkEvents(browserCtx context.Context) {
448457
attribute.String("method", e.Request.Method),
449458
attribute.String("type", string(e.Type)),
450459
))
460+
461+
requestsMutex.Lock()
451462
requests[e.RequestID] = span
463+
requestsMutex.Unlock()
452464

453465
case *network.EventResponseReceived:
454-
mu.Lock()
455-
defer mu.Unlock()
466+
requestMimesMutex.Lock()
467+
requestMimes[e.RequestID] = e.Response.MimeType
468+
requestMimesMutex.Unlock()
456469

470+
requestsMutex.Lock()
457471
span, ok := requests[e.RequestID]
472+
delete(requests, e.RequestID) // no point keeping it around anymore.
473+
requestsMutex.Unlock()
458474
if !ok {
459475
return
460476
}
@@ -474,7 +490,16 @@ func (s *BrowserService) handleNetworkEvents(browserCtx context.Context) {
474490
span.SetStatus(codes.Ok, fmt.Sprintf("%d %s", e.Response.Status, statusText))
475491
}
476492
span.End(trace.WithTimestamp(e.Timestamp.Time()))
477-
delete(requests, e.RequestID) // no point keeping it around anymore.
493+
494+
case *network.EventLoadingFinished:
495+
requestMimesMutex.Lock()
496+
mime, ok := requestMimes[e.RequestID]
497+
delete(requestMimes, e.RequestID)
498+
requestMimesMutex.Unlock()
499+
if !ok {
500+
return
501+
}
502+
MetricBrowserRequestSize.WithLabelValues(mime).Observe(float64(e.EncodedDataLength))
478503
}
479504
})
480505
}

tests/acceptance/fixtures/dashboards/grafana-image-renderer.json

Lines changed: 123 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"sort": "none"
118118
}
119119
},
120-
"pluginVersion": "12.3.0-18638286877",
120+
"pluginVersion": "12.3.0-18893060694",
121121
"targets": [
122122
{
123123
"datasource": {
@@ -217,7 +217,7 @@
217217
"sort": "none"
218218
}
219219
},
220-
"pluginVersion": "12.3.0-18638286877",
220+
"pluginVersion": "12.3.0-18893060694",
221221
"targets": [
222222
{
223223
"datasource": {
@@ -340,7 +340,7 @@
340340
"sort": "none"
341341
}
342342
},
343-
"pluginVersion": "12.3.0-18638286877",
343+
"pluginVersion": "12.3.0-18893060694",
344344
"targets": [
345345
{
346346
"datasource": {
@@ -422,7 +422,7 @@
422422
"reverse": false
423423
}
424424
},
425-
"pluginVersion": "12.3.0-18638286877",
425+
"pluginVersion": "12.3.0-18893060694",
426426
"targets": [
427427
{
428428
"datasource": {
@@ -504,7 +504,7 @@
504504
"reverse": false
505505
}
506506
},
507-
"pluginVersion": "12.3.0-18638286877",
507+
"pluginVersion": "12.3.0-18893060694",
508508
"targets": [
509509
{
510510
"datasource": {
@@ -604,7 +604,7 @@
604604
"sort": "none"
605605
}
606606
},
607-
"pluginVersion": "12.3.0-18638286877",
607+
"pluginVersion": "12.3.0-18893060694",
608608
"targets": [
609609
{
610610
"datasource": {
@@ -704,7 +704,7 @@
704704
"sort": "none"
705705
}
706706
},
707-
"pluginVersion": "12.3.0-18638286877",
707+
"pluginVersion": "12.3.0-18893060694",
708708
"targets": [
709709
{
710710
"datasource": {
@@ -804,7 +804,7 @@
804804
"sort": "none"
805805
}
806806
},
807-
"pluginVersion": "12.3.0-18638286877",
807+
"pluginVersion": "12.3.0-18893060694",
808808
"targets": [
809809
{
810810
"datasource": {
@@ -899,7 +899,7 @@
899899
"reverse": false
900900
}
901901
},
902-
"pluginVersion": "12.3.0-18638286877",
902+
"pluginVersion": "12.3.0-18893060694",
903903
"targets": [
904904
{
905905
"datasource": {
@@ -1005,7 +1005,7 @@
10051005
"reverse": false
10061006
}
10071007
},
1008-
"pluginVersion": "12.3.0-18638286877",
1008+
"pluginVersion": "12.3.0-18893060694",
10091009
"targets": [
10101010
{
10111011
"datasource": {
@@ -1105,7 +1105,7 @@
11051105
"sort": "none"
11061106
}
11071107
},
1108-
"pluginVersion": "12.3.0-18638286877",
1108+
"pluginVersion": "12.3.0-18893060694",
11091109
"targets": [
11101110
{
11111111
"datasource": {
@@ -1206,7 +1206,7 @@
12061206
"sort": "none"
12071207
}
12081208
},
1209-
"pluginVersion": "12.3.0-18638286877",
1209+
"pluginVersion": "12.3.0-18893060694",
12101210
"targets": [
12111211
{
12121212
"datasource": {
@@ -1342,7 +1342,7 @@
13421342
"sort": "none"
13431343
}
13441344
},
1345-
"pluginVersion": "12.3.0-18638286877",
1345+
"pluginVersion": "12.3.0-18893060694",
13461346
"targets": [
13471347
{
13481348
"datasource": {
@@ -1372,13 +1372,113 @@
13721372
"title": "Chromium memory usage over time",
13731373
"type": "timeseries"
13741374
},
1375+
{
1376+
"datasource": {
1377+
"type": "prometheus",
1378+
"uid": "${prom}"
1379+
},
1380+
"description": "How large is a response from each MIME type?",
1381+
"fieldConfig": {
1382+
"defaults": {
1383+
"color": {
1384+
"mode": "palette-classic"
1385+
},
1386+
"custom": {
1387+
"axisBorderShow": false,
1388+
"axisCenteredZero": false,
1389+
"axisColorMode": "text",
1390+
"axisLabel": "",
1391+
"axisPlacement": "auto",
1392+
"barAlignment": 0,
1393+
"barWidthFactor": 0.6,
1394+
"drawStyle": "line",
1395+
"fillOpacity": 0,
1396+
"gradientMode": "none",
1397+
"hideFrom": {
1398+
"legend": false,
1399+
"tooltip": false,
1400+
"viz": false
1401+
},
1402+
"insertNulls": false,
1403+
"lineInterpolation": "linear",
1404+
"lineWidth": 1,
1405+
"pointSize": 5,
1406+
"scaleDistribution": {
1407+
"type": "linear"
1408+
},
1409+
"showPoints": "auto",
1410+
"showValues": false,
1411+
"spanNulls": false,
1412+
"stacking": {
1413+
"group": "A",
1414+
"mode": "none"
1415+
},
1416+
"thresholdsStyle": {
1417+
"mode": "off"
1418+
}
1419+
},
1420+
"mappings": [],
1421+
"thresholds": {
1422+
"mode": "absolute",
1423+
"steps": [
1424+
{
1425+
"color": "green",
1426+
"value": 0
1427+
},
1428+
{
1429+
"color": "red",
1430+
"value": 80
1431+
}
1432+
]
1433+
},
1434+
"unit": "bytes"
1435+
},
1436+
"overrides": []
1437+
},
1438+
"gridPos": {
1439+
"h": 8,
1440+
"w": 12,
1441+
"x": 0,
1442+
"y": 58
1443+
},
1444+
"id": 22,
1445+
"options": {
1446+
"legend": {
1447+
"calcs": [],
1448+
"displayMode": "list",
1449+
"placement": "bottom",
1450+
"showLegend": true
1451+
},
1452+
"tooltip": {
1453+
"hideZeros": false,
1454+
"mode": "multi",
1455+
"sort": "none"
1456+
}
1457+
},
1458+
"pluginVersion": "12.3.0-18893060694",
1459+
"targets": [
1460+
{
1461+
"datasource": {
1462+
"type": "prometheus",
1463+
"uid": "${prom}"
1464+
},
1465+
"editorMode": "code",
1466+
"expr": "avg by(mime_type) (histogram_quantile(0.99, sum by(le, mime_type) (browser_request_size_bucket{unit=\"bytes\", job=\"image-renderer\"})))",
1467+
"legendFormat": "__auto",
1468+
"range": true,
1469+
"refId": "A"
1470+
}
1471+
],
1472+
"title": "Browser response sizes (p99)",
1473+
"type": "timeseries"
1474+
},
13751475
{
13761476
"collapsed": false,
13771477
"gridPos": {
13781478
"h": 1,
13791479
"w": 24,
13801480
"x": 0,
1381-
"y": 58
1481+
"y": 66
13821482
},
13831483
"id": 19,
13841484
"panels": [],
@@ -1426,14 +1526,14 @@
14261526
"h": 13,
14271527
"w": 12,
14281528
"x": 0,
1429-
"y": 59
1529+
"y": 67
14301530
},
14311531
"id": 1,
14321532
"options": {
14331533
"cellHeight": "sm",
14341534
"showHeader": true
14351535
},
1436-
"pluginVersion": "12.3.0-18638286877",
1536+
"pluginVersion": "12.3.0-18893060694",
14371537
"targets": [
14381538
{
14391539
"datasource": {
@@ -1527,14 +1627,14 @@
15271627
"h": 13,
15281628
"w": 12,
15291629
"x": 12,
1530-
"y": 59
1630+
"y": 67
15311631
},
15321632
"id": 21,
15331633
"options": {
15341634
"cellHeight": "sm",
15351635
"showHeader": true
15361636
},
1537-
"pluginVersion": "12.3.0-18638286877",
1637+
"pluginVersion": "12.3.0-18893060694",
15381638
"targets": [
15391639
{
15401640
"datasource": {
@@ -1610,7 +1710,7 @@
16101710
"h": 16,
16111711
"w": 24,
16121712
"x": 0,
1613-
"y": 72
1713+
"y": 80
16141714
},
16151715
"id": 14,
16161716
"options": {
@@ -1624,7 +1724,7 @@
16241724
"sortOrder": "Descending",
16251725
"wrapLogMessage": false
16261726
},
1627-
"pluginVersion": "12.3.0-18638286877",
1727+
"pluginVersion": "12.3.0-18893060694",
16281728
"targets": [
16291729
{
16301730
"datasource": {
@@ -1654,7 +1754,7 @@
16541754
"h": 16,
16551755
"w": 24,
16561756
"x": 0,
1657-
"y": 88
1757+
"y": 96
16581758
},
16591759
"id": 15,
16601760
"options": {
@@ -1668,7 +1768,7 @@
16681768
"sortOrder": "Descending",
16691769
"wrapLogMessage": false
16701770
},
1671-
"pluginVersion": "12.3.0-18638286877",
1771+
"pluginVersion": "12.3.0-18893060694",
16721772
"targets": [
16731773
{
16741774
"datasource": {
@@ -1744,5 +1844,5 @@
17441844
"timezone": "browser",
17451845
"title": "grafana-image-renderer",
17461846
"uid": "grafana-image-renderer",
1747-
"version": 11
1847+
"version": 12
17481848
}

0 commit comments

Comments
 (0)