Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically overwrite dashboard UIDs with a prescribed naming pattern #224

Merged
merged 6 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions lib/charms/grafana_agent/v0/cos_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def __init__(self, *args):
)

import pydantic
from cosl import GrafanaDashboard, JujuTopology
from cosl import DashboardPath40UID, JujuTopology, LZMABase64
from cosl.rules import AlertRules
from ops.charm import RelationChangedEvent
from ops.framework import EventBase, EventSource, Object, ObjectEvents
Expand All @@ -254,9 +254,9 @@ class _MetricsEndpointDict(TypedDict):

LIBID = "dc15fa84cef84ce58155fb84f6c6213a"
LIBAPI = 0
LIBPATCH = 16
LIBPATCH = 17

PYDEPS = ["cosl", "pydantic"]
PYDEPS = ["cosl >= 0.0.50", "pydantic"]

DEFAULT_RELATION_NAME = "cos-agent"
DEFAULT_PEER_RELATION_NAME = "peers"
Expand Down Expand Up @@ -481,7 +481,7 @@ class CosAgentProviderUnitData(DatabagModel):
# this needs to make its way to the gagent leader
metrics_alert_rules: dict
log_alert_rules: dict
dashboards: List[GrafanaDashboard]
dashboards: List[str]
# subordinate is no longer used but we should keep it until we bump the library to ensure
# we don't break compatibility.
subordinate: Optional[bool] = None
Expand Down Expand Up @@ -514,7 +514,7 @@ class CosAgentPeersUnitData(DatabagModel):
# of the outgoing o11y relations.
metrics_alert_rules: Optional[dict]
log_alert_rules: Optional[dict]
dashboards: Optional[List[GrafanaDashboard]]
dashboards: Optional[List[str]]

# when this whole datastructure is dumped into a databag, it will be nested under this key.
# while not strictly necessary (we could have it 'flattened out' into the databag),
Expand Down Expand Up @@ -742,12 +742,20 @@ def _log_alert_rules(self) -> Dict:
return alert_rules.as_dict()

@property
def _dashboards(self) -> List[GrafanaDashboard]:
dashboards: List[GrafanaDashboard] = []
def _dashboards(self) -> List[str]:
dashboards: List[str] = []
for d in self._dashboard_dirs:
for path in Path(d).glob("*"):
dashboard = GrafanaDashboard._serialize(path.read_bytes())
dashboards.append(dashboard)
with open(path, "rt") as fp:
dashboard = json.load(fp)
rel_path = str(
path.relative_to(self._charm.charm_dir) if path.is_absolute() else path
)
# COSAgentProvider is somewhat analogous to GrafanaDashboardProvider. We need to overwrite the uid here
# because there is currently no other way to communicate the dashboard path separately.
# https://github.com/canonical/grafana-k8s-operator/pull/363
dashboard["uid"] = DashboardPath40UID.generate(self._charm.meta.name, rel_path)
dashboards.append(LZMABase64.compress(json.dumps(dashboard)))
return dashboards

@property
Expand Down Expand Up @@ -1318,7 +1326,7 @@ def dashboards(self) -> List[Dict[str, str]]:
seen_apps.append(app_name)

for encoded_dashboard in data.dashboards or ():
content = GrafanaDashboard(encoded_dashboard)._deserialize()
content = json.loads(LZMABase64.decompress(encoded_dashboard))

title = content.get("title", "no_title")

Expand Down
Loading
Loading