Skip to content

Commit

Permalink
Use real null values, not a string named None
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkay committed Jan 8, 2025
1 parent fb9de0b commit 1e2169e
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/charms/grafana_agent/v0/cos_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ def get_all_endpoints(

def _get_tracing_endpoint(
self, relation: Optional[Relation], protocol: ReceiverProtocol
) -> Optional[str]:
) -> str:
"""Return a tracing endpoint URL if it is available or raise a ProtocolNotFoundError."""
unit_data = self.get_all_endpoints(relation)
if not unit_data:
Expand All @@ -816,19 +816,21 @@ def _get_tracing_endpoint(
if not receivers:
# we didn't find the protocol because grafana-agent didn't return us the protocol that we requested
# the caller might want to verify that we did indeed request this protocol
# TODO put this in the error message maybe?
raise ProtocolNotFoundError(protocol)
if len(receivers) > 1:
logger.warning(
f"too many receivers with protocol={protocol!r}; using first one. Found: {receivers}"
)

receiver = receivers[0]
if not receiver.url:
# grafana-agent isn't connected to the tracing backend yet
raise ProtocolNotFoundError(protocol)
return receiver.url

def get_tracing_endpoint(
self, protocol: ReceiverProtocol, relation: Optional[Relation] = None
) -> Optional[str]:
) -> str:
"""Receiver endpoint for the given protocol.
It could happen that this function gets called before the provider publishes the endpoints.
Expand All @@ -842,13 +844,7 @@ def get_tracing_endpoint(
If the charm attempts to obtain an endpoint when grafana-agent isn't related to a tracing backend.
"""
try:
endpoint = self._get_tracing_endpoint(relation or self._relation, protocol=protocol)
# because of the backwards compatibility requirements, the requirer is returning None
# (written as string to the databag) when tracing backend isn't connected.
# See more in the comment in `COSAgentRequirer#update_tracing_receivers` in this file.
if endpoint == "None":
return None
return endpoint
return self._get_tracing_endpoint(relation or self._relation, protocol=protocol)
except ProtocolNotFoundError:
# let's see if we didn't find it because we didn't request the endpoint
requested_protocols = set()
Expand Down Expand Up @@ -1019,9 +1015,12 @@ def update_tracing_receivers(self):
# however, because of the backwards compatibility requirements, we need to still provide
# the protocols list so that the charm with older cos_agent version doesn't error its hooks.
# before this change was added, the charm with old cos_agent version threw exceptions with
# connections to grafana-agent timing out, after that it will still throw exceptions, but
# faster: `MissingSchema: Invalid URL 'None/v1/traces': No scheme supplied.`
url=f"{self._get_tracing_receiver_url(protocol) if self._charm.tracing.is_ready() else None}", # type: ignore
# connections to grafana-agent timing out. After the change, the charm will fail validating
# databag contents (as it expects a string in URL) but that won't cause any errors as
# tracing endpoints are the only content in the grafana-agent's side of the databag.
url=f"{self._get_tracing_receiver_url(protocol)}"
if self._charm.tracing.is_ready() # type: ignore
else None,
protocol=ProtocolType(
name=protocol,
type=receiver_protocol_to_transport_protocol[protocol],
Expand Down

0 comments on commit 1e2169e

Please sign in to comment.