Skip to content

Commit

Permalink
chore: update charm libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Github Actions committed Jul 22, 2023
1 parent 3b44d3e commit 44c88dd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
4 changes: 2 additions & 2 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 = 31
LIBPATCH = 32

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -525,7 +525,7 @@ def _validate_relation_by_interface_and_direction(
relation = charm.meta.relations[relation_name]

actual_relation_interface = relation.interface_name
if actual_relation_interface != expected_relation_interface:
if actual_relation_interface and actual_relation_interface != expected_relation_interface:
raise RelationInterfaceMismatchError(
relation_name, expected_relation_interface, actual_relation_interface
)
Expand Down
50 changes: 34 additions & 16 deletions lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def _on_scrape_targets_changed(self, event):

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

logger = logging.getLogger(__name__)

Expand All @@ -391,6 +391,7 @@ def _on_scrape_targets_changed(self, event):
"scheme",
"basic_auth",
"tls_config",
"authorization",
}
DEFAULT_JOB = {
"metrics_path": "/metrics",
Expand Down Expand Up @@ -601,15 +602,22 @@ def render_alertmanager_static_configs(alertmanagers: List[str]):
# Create a mapping from paths to netlocs
# Group alertmanager targets into a dictionary of lists:
# {path: [netloc1, netloc2]}
paths = defaultdict(list) # type: Dict[str, List[str]]
paths = defaultdict(list) # type: Dict[Tuple[str, str], List[str]]
for parsed in map(urlparse, sanitized):
path = parsed.path or "/"
paths[path].append(parsed.netloc)
paths[(parsed.scheme, path)].append(parsed.netloc)

return {
"alertmanagers": [
{"path_prefix": path_prefix, "static_configs": [{"targets": netlocs}]}
for path_prefix, netlocs in paths.items()
{
"scheme": scheme,
"path_prefix": path_prefix,
"static_configs": [{"targets": netlocs}],
# FIXME figure out how to get alertmanager's ca_file into here
# Without this, prom errors: "x509: certificate signed by unknown authority"
"tls_config": {"insecure_skip_verify": True},
}
for (scheme, path_prefix), netlocs in paths.items()
]
}

Expand Down Expand Up @@ -1352,29 +1360,39 @@ def _static_scrape_config(self, relation) -> list:
if not relation.units:
return []

scrape_jobs = json.loads(relation.data[relation.app].get("scrape_jobs", "[]"))
scrape_configs = json.loads(relation.data[relation.app].get("scrape_jobs", "[]"))

if not scrape_jobs:
if not scrape_configs:
return []

scrape_metadata = json.loads(relation.data[relation.app].get("scrape_metadata", "{}"))

if not scrape_metadata:
return scrape_jobs
return scrape_configs

topology = JujuTopology.from_dict(scrape_metadata)

job_name_prefix = "juju_{}_prometheus_scrape".format(topology.identifier)
scrape_jobs = PrometheusConfig.prefix_job_names(scrape_jobs, job_name_prefix)
scrape_jobs = PrometheusConfig.sanitize_scrape_configs(scrape_jobs)
scrape_configs = PrometheusConfig.prefix_job_names(scrape_configs, job_name_prefix)
scrape_configs = PrometheusConfig.sanitize_scrape_configs(scrape_configs)

hosts = self._relation_hosts(relation)

scrape_jobs = PrometheusConfig.expand_wildcard_targets_into_individual_jobs(
scrape_jobs, hosts, topology
scrape_configs = PrometheusConfig.expand_wildcard_targets_into_individual_jobs(
scrape_configs, hosts, topology
)

return scrape_jobs
# If scheme is https but no ca section present, then auto add "insecure_skip_verify",
# otherwise scraping errors out with "x509: certificate signed by unknown authority".
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#tls_config
for scrape_config in scrape_configs:
tls_config = scrape_config.get("tls_config", {})
ca_present = "ca" in tls_config or "ca_file" in tls_config
if scrape_config.get("scheme") == "https" and not ca_present:
tls_config["insecure_skip_verify"] = True
scrape_config["tls_config"] = tls_config

return scrape_configs

def _relation_hosts(self, relation: Relation) -> Dict[str, Tuple[str, str]]:
"""Returns a mapping from unit names to (address, path) tuples, for the given relation."""
Expand Down Expand Up @@ -1792,10 +1810,10 @@ def _scrape_jobs(self) -> list:
A list of dictionaries, where each dictionary specifies a
single scrape job for Prometheus.
"""
jobs = self._jobs if self._jobs else [DEFAULT_JOB]
jobs = self._jobs or []
if callable(self._lookaside_jobs):
return jobs + PrometheusConfig.sanitize_scrape_configs(self._lookaside_jobs())
return jobs
jobs.extend(PrometheusConfig.sanitize_scrape_configs(self._lookaside_jobs()))
return jobs or [DEFAULT_JOB]

@property
def _scrape_metadata(self) -> dict:
Expand Down

0 comments on commit 44c88dd

Please sign in to comment.