Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
41 changes: 40 additions & 1 deletion lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def _on_subject_requested(self, event: SubjectRequestedEvent):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 56
LIBPATCH = 58

PYDEPS = ["ops>=2.0.0"]

Expand Down Expand Up @@ -842,6 +842,11 @@ def _legacy_compat_find_secret_by_old_label(self) -> None:
self._secret_meta = self._model.get_secret(label=label)
except SecretNotFoundError:
pass
except ModelError as e:
# Permission denied can be raised if the secret exists but is not yet granted to us.
if "permission denied" in str(e):
return
raise
else:
if label != self.label:
self.current_label = label
Expand Down Expand Up @@ -876,6 +881,8 @@ def _legacy_migration_to_new_label_if_needed(self) -> None:
except ModelError as err:
if MODEL_ERRORS["not_leader"] not in str(err):
raise
if "permission denied" not in str(err):
raise
self.current_label = None

##########################################################################
Expand Down Expand Up @@ -4268,6 +4275,14 @@ def _on_secret_changed_event(self, event: SecretChangedEvent):
if relation.app == self.charm.app:
logging.info("Secret changed event ignored for Secret Owner")

if relation.name != self.relation_data.relation_name:
logger.debug(
"Ignoring secret-changed from endpoint %s (expected %s)",
relation.name,
self.relation_data.relation_name,
)
return

remote_unit = None
for unit in relation.units:
if unit.app != self.charm.app:
Expand Down Expand Up @@ -5294,6 +5309,14 @@ def _on_secret_changed_event(self, event: SecretChangedEvent):
)
return

if relation.name != self.relation_data.relation_name:
logger.debug(
"Ignoring secret-changed from endpoint %s (expected %s)",
relation.name,
self.relation_data.relation_name,
)
return

if relation.app == self.charm.app:
logging.info("Secret changed event ignored for Secret Owner")

Expand Down Expand Up @@ -5556,6 +5579,14 @@ def _on_secret_changed_event(self, event: SecretChangedEvent):
)
return

if relation.name != self.relation_data.relation_name:
logger.debug(
"Ignoring secret-changed from endpoint %s (expected %s)",
relation.name,
self.relation_data.relation_name,
)
return

if relation.app == self.charm.app:
logging.info("Secret changed event ignored for Secret Owner")

Expand Down Expand Up @@ -5701,6 +5732,14 @@ def _on_secret_changed_event(self, event: SecretChangedEvent):
if relation.app == self.charm.app:
logging.info("Secret changed event ignored for Secret Owner")

if relation.name != self.relation_data.relation_name:
logger.debug(
"Ignoring secret-changed from endpoint %s (expected %s)",
relation.name,
self.relation_data.relation_name,
)
return

remote_unit = None
for unit in relation.units:
if unit.app != self.charm.app:
Expand Down
36 changes: 33 additions & 3 deletions lib/charms/grafana_agent/v0/cos_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ def __init__(self, *args):
```
"""

import copy
import enum
import hashlib
import json
import logging
import socket
Expand Down Expand Up @@ -254,7 +256,7 @@ class _MetricsEndpointDict(TypedDict):

LIBID = "dc15fa84cef84ce58155fb84f6c6213a"
LIBAPI = 0
LIBPATCH = 23
LIBPATCH = 25

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

Expand Down Expand Up @@ -308,6 +310,13 @@ def _dedupe_list(items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
return unique_items


def _dict_hash_except_key(scrape_config: Dict[str, Any], key: Optional[str]):
"""Get a hash of the scrape_config dict, except for the specified key."""
cfg_for_hash = {k: v for k, v in scrape_config.items() if k != key}
serialized = json.dumps(cfg_for_hash, sort_keys=True)
return hashlib.blake2b(serialized.encode(), digest_size=4).hexdigest()


class TracingError(Exception):
"""Base class for custom errors raised by tracing."""

Expand Down Expand Up @@ -697,6 +706,27 @@ def _on_refresh(self, event):
) as e:
logger.error("Invalid relation data provided: %s", e)

def _deterministic_scrape_configs(
self, scrape_configs: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
"""Get deterministic scrape_configs with stable job names.

For stability across serializations, compute a short per-config hash
and append it to the existing job name (or 'default'). Keep the app
name as a prefix: <app>_<job_or_default>_<8hex-hash>.

Hash the whole scrape_config (except any existing job_name) so the
suffix is sensitive to all stable fields. Use deterministic JSON
serialization.
"""
local_scrape_configs = copy.deepcopy(scrape_configs)
for scrape_config in local_scrape_configs:
name = scrape_config.get("job_name", "default")
short_id = _dict_hash_except_key(scrape_config, "job_name")
scrape_config["job_name"] = f"{self._charm.app.name}_{name}_{short_id}"

return sorted(local_scrape_configs, key=lambda c: c.get("job_name", ""))

@property
def _scrape_jobs(self) -> List[Dict]:
"""Return a list of scrape_configs.
Expand All @@ -721,7 +751,7 @@ def _scrape_jobs(self) -> List[Dict]:

scrape_configs = scrape_configs or []

return scrape_configs
return self._deterministic_scrape_configs(scrape_configs)

@property
def _metrics_alert_rules(self) -> Dict:
Expand All @@ -737,7 +767,7 @@ def _metrics_alert_rules(self) -> Dict:
)
alert_rules.add_path(self._metrics_rules, recursive=self._recursive)
alert_rules.add(
generic_alert_groups.application_rules,
copy.deepcopy(generic_alert_groups.application_rules),
group_name_prefix=JujuTopology.from_charm(self._charm).identifier,
)

Expand Down
17 changes: 15 additions & 2 deletions lib/charms/operator_libs_linux/v1/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@
# limitations under the License.


"""Abstractions for stopping, starting and managing system services via systemd.
"""Legacy Charmhub-hosted systemd library, deprecated in favour of ``charmlibs.systemd``.

WARNING: This library is deprecated and will no longer receive feature updates or bugfixes.
``charmlibs.systemd`` version 1.0 is a bug-for-bug compatible migration of this library.
Add 'charmlibs-systemd~=1.0' to your charm's dependencies, and remove this Charmhub-hosted library.
Then replace `from charms.operator_libs_linux.v0 import systemd` with
`from charmlibs import systemd`.
Read more:
- https://documentation.ubuntu.com/charmlibs
- https://pypi.org/project/charmlibs-systemd

---

Abstractions for stopping, starting and managing system services via systemd.

This library assumes that your charm is running on a platform that uses systemd. E.g.,
Centos 7 or later, Ubuntu Xenial (16.04) or later.
Expand Down Expand Up @@ -64,7 +77,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 4
LIBPATCH = 5


class SystemdError(Exception):
Expand Down