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

Add tag with charm name to dashboard #256

Merged
merged 6 commits into from
Feb 14, 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
9 changes: 8 additions & 1 deletion lib/charms/grafana_agent/v0/cos_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class _MetricsEndpointDict(TypedDict):

LIBID = "dc15fa84cef84ce58155fb84f6c6213a"
LIBAPI = 0
LIBPATCH = 19
LIBPATCH = 20

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

Expand Down Expand Up @@ -758,6 +758,13 @@ def _dashboards(self) -> List[str]:
# 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)

# Add tags
tags: List[str] = dashboard.get("tags", [])
if not any(tag.startswith("charm: ") for tag in tags):
tags.append(f"charm: {self._charm.meta.name}")
dashboard["tags"] = tags
sed-i marked this conversation as resolved.
Show resolved Hide resolved

dashboards.append(LZMABase64.compress(json.dumps(dashboard)))
return dashboards

Expand Down
21 changes: 16 additions & 5 deletions lib/charms/grafana_k8s/v0/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def __init__(self, *args):
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version

LIBPATCH = 39
LIBPATCH = 42

PYDEPS = ["cosl >= 0.0.50"]

Expand Down Expand Up @@ -417,8 +417,7 @@ def __init__(
self.expected_relation_interface = expected_relation_interface
self.actual_relation_interface = actual_relation_interface
self.message = (
"The '{}' relation has '{}' as "
"interface rather than the expected '{}'".format(
"The '{}' relation has '{}' as " "interface rather than the expected '{}'".format(
relation_name, actual_relation_interface, expected_relation_interface
)
)
Expand Down Expand Up @@ -634,7 +633,10 @@ def _replace_template_fields( # noqa: C901
deletions = []
for tmpl in dict_content["templating"]["list"]:
if tmpl["name"] and tmpl["name"] in used_replacements:
deletions.append(tmpl)
# it might happen that existing template var name is the same as the one we insert (i.e prometheusds or lokids)
# in that case, we want to pop the existing one only.
if tmpl not in DATASOURCE_TEMPLATE_DROPDOWNS:
deletions.append(tmpl)

for d in deletions:
dict_content["templating"]["list"].remove(d)
Expand Down Expand Up @@ -962,6 +964,13 @@ def _replace_uid(
"Processed dashboard '%s': kept original uid '%s'", dashboard_path, original_uid
)

@classmethod
def _add_tags(cls, dashboard_dict: dict, charm_name: str):
tags: List[str] = dashboard_dict.get("tags", [])
if not any(tag.startswith("charm: ") for tag in tags):
tags.append(f"charm: {charm_name}")
dashboard_dict["tags"] = tags

@classmethod
def load_dashboards_from_dir(
cls,
Expand Down Expand Up @@ -1004,6 +1013,8 @@ def _is_dashboard(p: Path) -> bool:
charm_name=charm_name,
)

cls._add_tags(dashboard_dict=dashboard_dict, charm_name=charm_name)

id = "file:{}".format(path.stem)
dashboard_templates[id] = cls._content_to_dashboard_object(
charm_name=charm_name,
Expand Down Expand Up @@ -1601,7 +1612,7 @@ def _render_dashboards_and_signal_changed(self, relation: Relation) -> bool: #

if not coerced_data == stored_data:
stored_dashboards = self.get_peer_data("dashboards")
stored_dashboards[relation.id] = stored_data
stored_dashboards[str(relation.id)] = stored_data
self.set_peer_data("dashboards", stored_dashboards)
return True
return None # type: ignore
Expand Down
Loading