Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/charms/loki_k8s/v1/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def __init__(self, ...):

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

PYDEPS = ["cosl"]

Expand Down Expand Up @@ -1601,28 +1601,46 @@ def loki_endpoints(self) -> List[dict]:
"""Fetch Loki Push API endpoints sent from LokiPushApiProvider through relation data.

Returns:
A list of dictionaries with Loki Push API endpoints, for instance:
A list of unique dictionaries with Loki Push API endpoints, for instance:
[
{"url": "http://loki1:3100/loki/api/v1/push"},
{"url": "http://loki2:3100/loki/api/v1/push"},
]
"""
endpoints = [] # type: list
endpoints = []
seen_urls = set()

for relation in self._charm.model.relations[self._relation_name]:
for unit in relation.units:
if unit.app == self._charm.app:
# This is a peer unit
continue

endpoint = relation.data[unit].get("endpoint")
if endpoint:
deserialized_endpoint = json.loads(endpoint)
endpoints.append(deserialized_endpoint)
if not (endpoint := relation.data[unit].get("endpoint")):
continue

deserialized_endpoint = json.loads(endpoint)
url = deserialized_endpoint.get("url")

# Deduplicate by URL.
# With loki-k8s we have ingress-per-unit, so in that case
# we do want to collect the URLs of all the units.
# With loki-coordinator-k8s, even when the coordinator
# is scaled, we want to advertise only one URL.
# Without deduplication, we'd end up with the same
# tls config section in the promtail config file, in which
# case promtail immediately exits with the following error:
# [promtail] level=error ts=<timestamp> msg="error creating promtail" error="failed to create client manager: duplicate client configs are not allowed, found duplicate for name: "

if not url or url in seen_urls:
continue

seen_urls.add(url)
endpoints.append(deserialized_endpoint)

return endpoints



class LokiPushApiConsumer(ConsumerBase):
"""Loki Consumer class."""

Expand Down
Loading