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

Strip '://' suffix from remote_write scheme #439

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 16 additions & 8 deletions lib/charms/prometheus_k8s/v0/prometheus_remote_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

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


logger = logging.getLogger(__name__)
Expand All @@ -58,7 +58,11 @@
DEFAULT_ALERT_RULES_RELATIVE_PATH = "./src/prometheus_alert_rules"


class RelationNotFoundError(Exception):
class RemoteWriteError(Exception):
"""Base class remote-write errors."""


class RelationNotFoundError(RemoteWriteError):
"""Raised if there is no relation with the given name."""

def __init__(self, relation_name: str):
Expand All @@ -68,7 +72,7 @@ def __init__(self, relation_name: str):
super().__init__(self.message)


class RelationInterfaceMismatchError(Exception):
class RelationInterfaceMismatchError(RemoteWriteError):
"""Raised if the relation with the given name has a different interface."""

def __init__(
Expand All @@ -89,7 +93,7 @@ def __init__(
super().__init__(self.message)


class RelationRoleMismatchError(Exception):
class RelationRoleMismatchError(RemoteWriteError):
"""Raised if the relation with the given name has a different direction."""

def __init__(
Expand Down Expand Up @@ -445,7 +449,7 @@ def restore(self, snapshot):
self.relation_id = snapshot["relation_id"]


class InvalidAlertRulePathError(Exception):
class InvalidAlertRulePathError(RemoteWriteError):
"""Raised if the alert rules folder cannot be found or is otherwise invalid."""

def __init__(
Expand Down Expand Up @@ -764,7 +768,7 @@ def __init__(
self,
charm: CharmBase,
relation_name: str = DEFAULT_RELATION_NAME,
endpoint_schema: str = "http",
endpoint_schema: str = "http", # TODO: in v1, rename to 'scheme'
endpoint_address: str = "",
endpoint_port: Union[str, int] = 9090,
endpoint_path: str = "/api/v1/write",
Expand Down Expand Up @@ -802,7 +806,7 @@ def __init__(
self._charm = charm
self.tool = CosTool(self._charm)
self._relation_name = relation_name
self._endpoint_schema = endpoint_schema
self._endpoint_scheme = endpoint_schema.strip().rstrip("://")
self._endpoint_address = endpoint_address
self._endpoint_port = int(endpoint_port)
self._endpoint_path = endpoint_path
Expand Down Expand Up @@ -851,8 +855,12 @@ def _set_endpoint_on_relation(self, relation: Relation) -> None:
if path and not path.startswith("/"):
path = "/{}".format(path)

if not re.match(r"^\w+$", self._endpoint_scheme):
msg = f"Invalid scheme 'f{self._endpoint_scheme}'; did you mean 'http'?"
raise ValueError(msg)

endpoint_url = "{}://{}:{}{}".format(
self._endpoint_schema, address, str(self._endpoint_port), path
self._endpoint_scheme, address, str(self._endpoint_port), path
)

relation.data[self._charm.unit]["remote_write"] = json.dumps(
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ deps =
integration: pytest-operator==1.0.0b1
commands =
charm: mypy {[vars]src_path} {posargs}
lib: mypy --python-version 3.5 {[vars]lib_path} {posargs}
lib: mypy --python-version 3.8 {[vars]lib_path} {posargs}
unit: mypy {[vars]tst_path}/unit {posargs}
integration: mypy {[vars]tst_path}/integration {posargs}

Expand Down