Skip to content

Commit

Permalink
use as_dict() because label_matcher_dict excludes unit (#67)
Browse files Browse the repository at this point in the history
* use as_dict() because label_matcher_dict excludes unit

* add juju_ prefix again

* rename and extent itest

* remove old itest

* update libs

* increase LIBPATCH

* add link to issue
  • Loading branch information
Abuelodelanada authored Mar 8, 2024
1 parent 58f7ae4 commit 31d1295
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _on_certificate_removed(self, event: CertificateRemovedEvent):

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

PYDEPS = ["jsonschema"]

Expand Down Expand Up @@ -379,7 +379,7 @@ def _on_relation_changed(self, event: RelationChangedEvent) -> None:
)

def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
"""Handler triggered on relation broken event.
"""Handle relation broken event.
Args:
event: Juju event
Expand Down
12 changes: 10 additions & 2 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 @@ class _MetricsEndpointDict(TypedDict):

LIBID = "dc15fa84cef84ce58155fb84f6c6213a"
LIBAPI = 0
LIBPATCH = 7
LIBPATCH = 8

PYDEPS = ["cosl", "pydantic < 2"]

Expand Down Expand Up @@ -700,10 +700,18 @@ def metrics_jobs(self) -> List[Dict]:

# Apply labels to the scrape jobs
for static_config in job.get("static_configs", []):
topo_as_dict = topology.as_dict(excluded_keys=["charm_name"])
static_config["labels"] = {
# Be sure to keep labels from static_config
**static_config.get("labels", {}),
**topology.label_matcher_dict,
# TODO: We should add a new method in juju_topology.py
# that like `as_dict` method, returns the keys with juju_ prefix
# https://github.com/canonical/cos-lib/issues/18
**{
"juju_{}".format(key): value
for key, value in topo_as_dict.items()
if value
},
}

scrape_jobs.append(job)
Expand Down
29 changes: 18 additions & 11 deletions lib/charms/loki_k8s/v1/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def _alert_rules_error(self, event):

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

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -799,7 +799,11 @@ def _from_file(self, root_path: Path, file_path: Path) -> List[dict]:
alert_rule["labels"] = {}

if self.topology:
alert_rule["labels"].update(self.topology.label_matcher_dict)
# only insert labels that do not already exist
for label, val in self.topology.label_matcher_dict.items():
if label not in alert_rule["labels"]:
alert_rule["labels"][label] = val

# insert juju topology filters into a prometheus alert rule
# logql doesn't like empty matchers, so add a job matcher which hits
# any string as a "wildcard" which the topology labels will
Expand Down Expand Up @@ -2090,15 +2094,18 @@ def _download_and_push_promtail_to_workload(
container: container into which promtail is to be uploaded.
"""
# Check for Juju proxy variables and fall back to standard ones if not set
proxies: Optional[Dict[str, str]] = {}
if proxies and os.environ.get("JUJU_CHARM_HTTP_PROXY"):
proxies.update({"http": os.environ["JUJU_CHARM_HTTP_PROXY"]})
if proxies and os.environ.get("JUJU_CHARM_HTTPS_PROXY"):
proxies.update({"https": os.environ["JUJU_CHARM_HTTPS_PROXY"]})
if proxies and os.environ.get("JUJU_CHARM_NO_PROXY"):
proxies.update({"no_proxy": os.environ["JUJU_CHARM_NO_PROXY"]})
else:
proxies = None
# If no Juju proxy variable was set, we set proxies to None to let the ProxyHandler get
# the proxy env variables from the environment
proxies = {
# The ProxyHandler uses only the protocol names as keys
# https://docs.python.org/3/library/urllib.request.html#urllib.request.ProxyHandler
"https": os.environ.get("JUJU_CHARM_HTTPS_PROXY", ""),
"http": os.environ.get("JUJU_CHARM_HTTP_PROXY", ""),
# The ProxyHandler uses `no` for the no_proxy key
# https://github.com/python/cpython/blob/3.12/Lib/urllib/request.py#L2553
"no": os.environ.get("JUJU_CHARM_NO_PROXY", ""),
}
proxies = {k: v for k, v in proxies.items() if v != ""} or None

proxy_handler = request.ProxyHandler(proxies)
opener = request.build_opener(proxy_handler)
Expand Down
Loading

0 comments on commit 31d1295

Please sign in to comment.