Skip to content

Commit

Permalink
fix #130 - wrong url provided to Prometheus (#150)
Browse files Browse the repository at this point in the history
* fix #130 - wrong url provided to Prometheus

* Update src/charm.py

Co-authored-by: Leon <82407168+sed-i@users.noreply.github.com>

* update grafana_dashboard.py lib

* remove comment

---------

Co-authored-by: Leon <82407168+sed-i@users.noreply.github.com>
  • Loading branch information
Abuelodelanada and sed-i authored Apr 19, 2023
1 parent 42c1429 commit 39c1b67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ def __init__(self, *args):
self,
relation_name="self-metrics-endpoint",
jobs=self.self_scraping_job,
external_url=self._external_url,
refresh_event=[
self.on.update_status,
self.ingress.on.ready,
self.ingress.on.revoked,
self.on["ingress"].relation_changed,
self.on["ingress"].relation_departed,
],
)

self.catalog = CatalogueConsumer(
Expand Down Expand Up @@ -192,10 +198,11 @@ def __init__(self, *args):
@property
def self_scraping_job(self):
"""The self-monitoring scrape job."""
port = urlparse(self._external_url).port or 80
external_url = urlparse(self._external_url)
return [
{
"static_configs": [{"targets": [f"*:{port}"]}],
"metrics_path": f"{external_url.path}/metrics",
"static_configs": [{"targets": [f"{external_url.hostname}:{external_url.port}"]}],
}
]

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_external_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ def test_config_option_overrides_fqdn(self):
# GIVEN a charm with the fqdn as its external URL
self.assertEqual(self.get_url_cli_arg(), self.fqdn_url)
self.assertTrue(self.is_service_running())
self.assertEqual(self.fqdn_url, self.harness.charm._external_url)

# WHEN the web_external_url config option is set
external_url = "http://foo.bar:8080/path/to/alertmanager"
self.harness.update_config({"web_external_url": external_url})
self.assertEqual(external_url, self.harness.charm._external_url)

# THEN it is used as the cli arg instead of the fqdn
self.assertEqual(self.get_url_cli_arg(), external_url)
Expand All @@ -95,6 +97,7 @@ def test_config_option_overrides_traefik(self):
# GIVEN a charm with the fqdn as its external URL
self.assertEqual(self.get_url_cli_arg(), self.fqdn_url)
self.assertTrue(self.is_service_running())
self.assertEqual(self.harness.charm._external_url, self.fqdn_url)

# WHEN a relation with traefik is formed but ingress isn't ready
rel_id = self.harness.add_relation("ingress", "traefik-app")
Expand All @@ -103,6 +106,7 @@ def test_config_option_overrides_traefik(self):
# THEN there is no change to the cli arg
self.assertEqual(self.get_url_cli_arg(), self.fqdn_url)
self.assertTrue(self.is_service_running())
self.assertEqual(self.harness.charm._external_url, self.fqdn_url)

# WHEN ingress becomes available
external_url_ingress = "http://foo.bar.ingress:80/path/to/mdl-alertmanager-k8s"
Expand Down

0 comments on commit 39c1b67

Please sign in to comment.