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 charm topology to snap logging job #81

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/charms/grafana_agent/v0/cos_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,18 @@ def metrics_jobs(self) -> List[Dict]:
@property
def snap_log_endpoints(self) -> List[SnapEndpoint]:
"""Fetch logging endpoints exposed by related snaps."""
endpoints = []
endpoints_with_topology = self.snap_log_endpoints_with_topology
for endpoint, _ in endpoints_with_topology:
endpoints.append(endpoint)

return endpoints

@property
def snap_log_endpoints_with_topology(self) -> List[Tuple[SnapEndpoint, JujuTopology]]:
"""Fetch logging endpoints and charm topology for each related snap."""
plugs = []
for data, _ in self._remote_data:
for data, topology in self._remote_data:
targets = data.log_slots
if targets:
for target in targets:
Expand All @@ -733,15 +743,16 @@ def snap_log_endpoints(self) -> List[SnapEndpoint]:
"endpoints; this should not happen."
)
else:
plugs.append(target)
plugs.append((target, topology))

endpoints = []
for plug in plugs:
for plug, topology in plugs:
if ":" not in plug:
logger.error(f"invalid plug definition received: {plug}. Ignoring...")
else:
endpoint = SnapEndpoint(*plug.split(":"))
endpoints.append(endpoint)
endpoints.append((endpoint, topology))

return endpoints

@property
Expand Down
12 changes: 9 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,12 @@ def _snap_plugs_logging_configs(self) -> List[Dict[str, Any]]:
agent_fstab = SnapFstab(Path("/var/lib/snapd/mount/snap.grafana-agent.fstab"))

shared_logs_configs = []
endpoint_owners = {endpoint.owner for endpoint in self._cos.snap_log_endpoints}
endpoint_owners = {
endpoint.owner: {"juju_application": topology.application, "juju_unit": topology.unit}
for endpoint, topology in self._cos.snap_log_endpoints_with_topology
}
for fstab_entry in agent_fstab.entries:
if fstab_entry.owner not in endpoint_owners:
if fstab_entry.owner not in endpoint_owners.keys():
continue

target_path = (
Expand All @@ -488,11 +491,14 @@ def _snap_plugs_logging_configs(self) -> List[Dict[str, Any]]:
"labels": {
"job": job_name,
"__path__": target_path,
**{
**{ # from grafana-agent's topology
k: v
for k, v in self._instance_topology.items()
if k not in ["juju_unit", "juju_application"]
},
# from the topology of the charm owning the snap
**endpoint_owners[fstab_entry.owner],
"snap_name": fstab_entry.owner,
},
}
],
Expand Down