Skip to content

Commit

Permalink
chore(metrics): add ability to set quantile for heatmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdeafbeef committed Jul 15, 2024
1 parent c429be6 commit 901d1d0
Showing 1 changed file with 78 additions and 10 deletions.
88 changes: 78 additions & 10 deletions scripts/gen-dashboard.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Union, List
from typing import Union, List, Literal

from grafanalib import formatunits as UNITS, _gen
from grafanalib.core import (
Expand Down Expand Up @@ -84,6 +84,24 @@ def create_counter_panel(
legend_format: str | None = None,
by_labels: list[str] = ["instance"],
) -> Panel:
"""
Create a counter panel for visualization.
Args:
expr (Union[str, List[Union[str, Expr]]]): Expression or list of expressions to visualize.
title (str): Title of the panel.
unit_format (str, optional): Format for the unit display. Defaults to UNITS.NUMBER_FORMAT.
labels_selectors (List[str], optional): List of label selectors. Defaults to an empty list.
legend_format (str | None, optional): Format for the legend. If None, it's generated automatically. Defaults to None.
by_labels (list[str], optional): Labels to group by. Defaults to ["instance"].
Returns:
Panel: A timeseries panel object.
Raises:
ValueError: If the list elements in expr are not all strings or all Expr objects.
TypeError: If expr is not a string, a list of strings, or a list of Expr objects.
"""
if legend_format is None:
legend_format = generate_legend_format(labels_selectors)

Expand Down Expand Up @@ -137,17 +155,46 @@ def create_heatmap_panel(
)


# Type alias for accepted quantiles
AcceptedQuantile = Literal[0, 50, 90, 95, 99, 999, 100]


def create_heatmap_quantile_panel(
metric_name: str,
title: str,
unit_format=UNITS.NUMBER_FORMAT,
unit_format: str = UNITS.NUMBER_FORMAT,
quantile: AcceptedQuantile = 95,
) -> Panel:
"""
Create a heatmap quantile panel for the given metric.
Args:
metric_name (str): Name of the metric to visualize.
title (str): Title of the panel.
unit_format (str, optional): Unit format for the panel. Defaults to UNITS.NUMBER_FORMAT.
quantile (AcceptedQuantile, optional): Quantile to use (as an integer 0-100). Defaults to 95.
Returns:
Panel: A configured grafanalib Panel object.
Raises:
ValueError: If the quantile is not one of the accepted values.
"""
accepted_quantiles = {0, 50, 90, 95, 99, 999, 100}

if quantile not in accepted_quantiles:
raise ValueError(f"Quantile must be one of {accepted_quantiles}")

quantile_float = quantile / 100 if quantile != 0 else 0
legend_format = f"{{{{instance}}}} p{quantile_float:.3f}"
quantile_expr = f'quantile="{quantile_float}"'

return timeseries_panel(
title=title,
targets=[
target(
Expr(metric_name, label_selectors=['quantile="0.95"']),
legend_format="{{instance}}",
expr=Expr(metric_name, label_selectors=[quantile_expr]),
legend_format=legend_format,
)
],
unit=unit_format,
Expand Down Expand Up @@ -176,20 +223,27 @@ def core_bc() -> RowPanel:
"tycho_bc_contract_delete_total", "Number of contract deletions over time"
),
create_heatmap_quantile_panel(
"tycho_bc_total_gas_used", "Total gas used per block"
"tycho_bc_total_gas_used", "Total gas used per block", quantile=100
),
create_heatmap_quantile_panel(
"tycho_bc_in_msg_count", "Number of inbound messages per block"
"tycho_bc_in_msg_count",
"Number of inbound messages per block",
quantile=999,
),
create_heatmap_quantile_panel(
"tycho_bc_out_msg_count", "Number of outbound messages per block"
"tycho_bc_out_msg_count",
"Number of outbound messages per block",
quantile=999,
),
create_heatmap_quantile_panel(
"tycho_bc_out_in_msg_ratio", "Out/In message ratio per block"
"tycho_bc_out_in_msg_ratio",
"Out/In message ratio per block",
quantile=999,
),
create_heatmap_quantile_panel(
"tycho_bc_out_msg_acc_ratio",
"Out message/Account ratio per block",
quantile=999,
),
]
return create_row("Blockchain", metrics)
Expand Down Expand Up @@ -418,10 +472,12 @@ def core_block_strider() -> RowPanel:
"tycho_storage_get_cell_from_rocksdb_time", "Time to load cell from RocksDB"
),
create_heatmap_quantile_panel(
"tycho_storage_store_block_data_size", "Block data size", UNITS.BYTES
"tycho_storage_store_block_data_size", "Block data size", UNITS.BYTES, 999
),
create_heatmap_quantile_panel(
"tycho_storage_cell_count", "Number of new cells from merkle update"
"tycho_storage_cell_count",
"Number of new cells from merkle update",
quantile=999,
),
create_heatmap_panel(
"tycho_storage_state_update_time", "Time to write state update to rocksdb"
Expand Down Expand Up @@ -1039,6 +1095,7 @@ def mempool_components() -> RowPanel:
"tycho_mempool_verifier_verify",
"Verifier: verify() errors",
labels_selectors=['kind=~"$kind"'],
by_labels=["kind", "instance"],
),
create_heatmap_panel(
"tycho_mempool_verifier_verify_time",
Expand All @@ -1048,6 +1105,7 @@ def mempool_components() -> RowPanel:
"tycho_mempool_verifier_validate",
"Verifier: validate() errors and warnings",
labels_selectors=['kind=~"$kind"'],
by_labels=["kind", "instance"],
),
create_heatmap_panel(
"tycho_mempool_verifier_validate_time",
Expand Down Expand Up @@ -1170,6 +1228,16 @@ def templates() -> Templating:
include_all=True,
all_value=".*",
),
template(
name="kind",
query="label_values(tycho_mempool_verifier_verify,kind)",
data_source="${source}",
hide=0,
regex=None,
multi=True,
include_all=True,
all_value=".*",
),
]
)

Expand Down

0 comments on commit 901d1d0

Please sign in to comment.