Skip to content

Commit e10a8bf

Browse files
authored
dydt panels & remake clark dashboard w/ wrapper (#27)
1 parent d2e1005 commit e10a8bf

2 files changed

Lines changed: 84 additions & 126 deletions

File tree

Lines changed: 52 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,54 @@
1-
from grafanalib.core import (
2-
Dashboard,
3-
Templating,
4-
Template,
5-
TimeSeries,
6-
Target,
7-
GridPos,
8-
Row,
9-
)
10-
from grafanalib.formatunits import PERCENT_UNIT, SECONDS, NUMBER_FORMAT, TRUE_FALSE
11-
12-
from common import PROMETHEUS_DATASOURCE_NAME
13-
14-
15-
def get_office_card_row():
16-
return Row(
17-
title="Office Access Card",
18-
panels=[
19-
TimeSeries(
20-
title="Office Access Card - 200 Responses",
21-
targets=[
22-
Target(
23-
datasource=PROMETHEUS_DATASOURCE_NAME,
24-
expr='sum(endpoint_hits{route=~"/api/OfficeAccessCard/verify.*", statusCode="200"})',
25-
legendFormat="total",
26-
refId="A",
27-
),
28-
Target(
29-
datasource=PROMETHEUS_DATASOURCE_NAME,
30-
expr='sum(rate(endpoint_hits{route=~"/api/OfficeAccessCard/verify.*", statusCode="200"}[1h])) * 3600',
31-
legendFormat="dY/dt [hourly]",
32-
refId="B",
33-
),
34-
],
35-
),
36-
TimeSeries(
37-
title="Office Access Card - Non 200 Responses",
38-
targets=[
39-
Target(
40-
datasource=PROMETHEUS_DATASOURCE_NAME,
41-
expr='sum(endpoint_hits{route=~"/api/OfficeAccessCard/verify.*", statusCode!="200"}) by (statusCode)',
42-
legendFormat="{{statusCode}} total",
43-
refId="A",
44-
),
45-
Target(
46-
datasource=PROMETHEUS_DATASOURCE_NAME,
47-
expr='sum(rate(endpoint_hits{route=~"/api/OfficeAccessCard/verify.*", statusCode!="200"}[1h])) by (statusCode) * 3600',
48-
legendFormat="{{statusCode}} dY/dt [hourly]",
49-
refId="B",
50-
),
51-
],
52-
),
53-
],
54-
)
55-
56-
57-
def get_other_row():
58-
return Row(
59-
title="Endpoint Requests",
60-
panels=[
61-
TimeSeries(
62-
title="All main-endpoints traffic",
63-
unit=NUMBER_FORMAT,
64-
gridPos=GridPos(h=8, w=12, x=0, y=0),
65-
lineWidth=2,
66-
stacking={"group": "A", "mode": "none"},
67-
tooltipMode="all",
68-
tooltipSort="desc",
69-
targets=[
70-
Target(
71-
datasource=PROMETHEUS_DATASOURCE_NAME,
72-
expr='endpoint_hits{route!="/metrics"}',
73-
legendFormat="{{route}} {{method}} {{statusCode}}",
74-
refId="A",
75-
),
76-
],
77-
),
78-
TimeSeries(
79-
title="Account access",
80-
unit=NUMBER_FORMAT,
81-
gridPos=GridPos(h=8, w=12, x=12, y=0),
82-
lineWidth=2,
83-
stacking={"group": "A", "mode": "none"},
84-
tooltipMode="all",
85-
tooltipSort="desc",
86-
targets=[
87-
Target(
88-
datasource=PROMETHEUS_DATASOURCE_NAME,
89-
expr='endpoint_hits{route=~"/api/Auth.*"}',
90-
legendFormat="{{route}} {{method}} {{statusCode}}",
91-
refId="A",
92-
),
93-
],
94-
),
95-
TimeSeries(
96-
title="Messaging",
97-
unit=NUMBER_FORMAT,
98-
gridPos=GridPos(h=8, w=12, x=12, y=8),
99-
lineWidth=2,
100-
stacking={"group": "A", "mode": "none"},
101-
tooltipMode="all",
102-
tooltipSort="desc",
103-
targets=[
104-
Target(
105-
datasource=PROMETHEUS_DATASOURCE_NAME,
106-
expr='endpoint_hits{route=~"/api/messages/.*"}',
107-
legendFormat="{{route}} {{method}} {{statusCode}}",
108-
refId="A",
109-
),
110-
],
111-
),
112-
],
113-
)
1+
from wrapper import SceGrafanalibWrapper, ExpressionAndLegendPair
1142

3+
from grafanalib.formatunits import NUMBER_FORMAT
1154

116-
dashboard = Dashboard(
117-
title="Clark",
118-
uid="clark",
119-
description="sce club website",
120-
timezone="browser",
121-
rows=[get_office_card_row(), get_other_row()],
122-
).auto_panel_ids()
5+
wrapper = SceGrafanalibWrapper("Clark")
6+
wrapper.AddPanel(
7+
title="Office Access Card - 200 Responses",
8+
queries=[
9+
ExpressionAndLegendPair(
10+
'endpoint_hits{route=~"/api/OfficeAccessCard/verify.*", statusCode="200"}',
11+
"",
12+
)
13+
],
14+
dydt=True,
15+
)
16+
wrapper.AddPanel(
17+
title="Office Access Card - Non 200 Responses",
18+
queries=[
19+
ExpressionAndLegendPair(
20+
'endpoint_hits{route=~"/api/OfficeAccessCard/verify.*", statusCode!="200"}',
21+
"{{statusCode}}",
22+
)
23+
],
24+
dydt=True,
25+
)
26+
wrapper.AddPanel(
27+
title="All main-endpoints traffic",
28+
queries=[
29+
ExpressionAndLegendPair(
30+
'endpoint_hits{route!="/metrics"}', "{{route}} {{method}} {{statusCode}}"
31+
)
32+
],
33+
unit=NUMBER_FORMAT,
34+
)
35+
wrapper.AddPanel(
36+
title="Account access",
37+
queries=[
38+
ExpressionAndLegendPair(
39+
'endpoint_hits{route=~"/api/Auth.*"}', "{{route}} {{method}} {{statusCode}}"
40+
)
41+
],
42+
unit=NUMBER_FORMAT,
43+
)
44+
wrapper.AddPanel(
45+
title="Messaging",
46+
queries=[
47+
ExpressionAndLegendPair(
48+
'endpoint_hits{route=~"/api/messages/.*"}',
49+
"{{route}} {{method}} {{statusCode}}",
50+
)
51+
],
52+
unit=NUMBER_FORMAT,
53+
)
54+
dashboard = wrapper.Render()

grafana/provisioning/dashboards/wrapper.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,48 @@ def __init__(self, title, panel_width=12, panel_height=8):
3636
self.panel_width = min(panel_width, self.MAX_WIDTH)
3737
self.panel_height = panel_height
3838

39-
def AddPanel(self, title, queries: list[ExpressionAndLegendPair], unit):
39+
def AddPanel(self, title, queries: list[ExpressionAndLegendPair], unit='', dydt=False):
4040
targets = []
4141
iterator = RefIdGenerator()
4242
for query in queries:
4343
query_text = query.expression
4444
query_label = query.legend
45-
refId = iterator.next()
45+
if not dydt:
46+
targets.append(
47+
Target(
48+
expr=query_text,
49+
legendFormat=query_label,
50+
refId=iterator.next(),
51+
datasource=PROMETHEUS_DATASOURCE_NAME,
52+
)
53+
)
54+
continue
55+
total_query = f"sum({query_text})"
56+
rate_query = f"sum(rate({query_text}[1h]))"
57+
total_label = "total"
58+
rate_label = "dY/dt [hourly]"
59+
if query_label:
60+
total_query += f' by ({query_label.strip("{}")})'
61+
rate_query += f' by ({query_label.strip("{}")})'
62+
total_label = f"{query_label} " + total_label
63+
rate_label = f"{query_label} " + rate_label
64+
targets.append(
65+
Target(
66+
expr=total_query,
67+
legendFormat=total_label,
68+
refId=iterator.next(),
69+
datasource=PROMETHEUS_DATASOURCE_NAME,
70+
)
71+
)
4672
targets.append(
4773
Target(
48-
expr=query_text,
49-
legendFormat=query_label,
50-
refId=refId,
74+
expr=rate_query + " * 3600",
75+
legendFormat=rate_label,
76+
refId=iterator.next(),
5177
datasource=PROMETHEUS_DATASOURCE_NAME,
5278
)
5379
)
54-
self.panels.append(
80+
self.panels.append(
5581
TimeSeries(
5682
title=title,
5783
targets=targets,

0 commit comments

Comments
 (0)