Skip to content

Commit

Permalink
chore: update charm libraries (#288)
Browse files Browse the repository at this point in the history
Co-authored-by: Github Actions <[email protected]>
  • Loading branch information
observability-noctua-bot and Github Actions authored Apr 22, 2024
1 parent a45524a commit 75982a8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
4 changes: 3 additions & 1 deletion lib/charms/loki_k8s/v1/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,9 @@ def _alert_rules_error(self, event):

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

PYDEPS = ["cosl"]

logger = logging.getLogger(__name__)

Expand Down
74 changes: 49 additions & 25 deletions lib/charms/tempo_k8s/v2/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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 = 2
LIBPATCH = 3

PYDEPS = ["pydantic"]

Expand All @@ -117,15 +117,13 @@ def __init__(self, *args):
"zipkin",
"kafka",
"opencensus",
"tempo", # legacy, renamed to tempo_http
"tempo_http",
"tempo_grpc",
"otlp_grpc",
"otlp_http",
"jaeger_grpc",
# "jaeger_grpc",
"jaeger_thrift_compact",
"jaeger_thrift_http",
"jaeger_http_thrift", # legacy, renamed to jaeger_thrift_http
"jaeger_thrift_binary",
]

Expand Down Expand Up @@ -302,11 +300,14 @@ class TracingProviderAppData(DatabagModel): # noqa: D101
"""Application databag model for the tracing provider."""

host: str
"""Server hostname."""
"""Server hostname (local fqdn)."""

receivers: List[Receiver]
"""Enabled receivers and ports at which they are listening."""

external_url: Optional[str] = None
"""Server url. If an ingress is present, it will be the ingress address."""


class TracingRequirerAppData(DatabagModel): # noqa: D101
"""Application databag model for the tracing requirer."""
Expand Down Expand Up @@ -492,13 +493,16 @@ def __init__(
self,
charm: CharmBase,
host: str,
external_url: Optional[str] = None,
relation_name: str = DEFAULT_RELATION_NAME,
):
"""Initialize.
Args:
charm: a `CharmBase` instance that manages this instance of the Tempo service.
host: address of the node hosting the tempo server.
external_url: external address of the node hosting the tempo server,
if an ingress is present.
relation_name: an optional string name of the relation between `charm`
and the Tempo charmed service. The default is "tracing".
Expand All @@ -519,6 +523,7 @@ def __init__(
super().__init__(charm, relation_name + "tracing-provider-v2")
self._charm = charm
self._host = host
self._external_url = external_url
self._relation_name = relation_name
self.framework.observe(
self._charm.on[relation_name].relation_joined, self._on_relation_event
Expand Down Expand Up @@ -585,6 +590,7 @@ def publish_receivers(self, receivers: Sequence[RawReceiver]):
try:
TracingProviderAppData(
host=self._host,
external_url=self._external_url,
receivers=[
Receiver(port=port, protocol=protocol) for protocol, port in receivers
],
Expand Down Expand Up @@ -612,16 +618,17 @@ class EndpointRemovedEvent(RelationBrokenEvent):
class EndpointChangedEvent(_AutoSnapshotEvent):
"""Event representing a change in one of the receiver endpoints."""

__args__ = ("host", "_ingesters")
__args__ = ("host", "external_url", "_receivers")

if TYPE_CHECKING:
host = "" # type: str
_ingesters = [] # type: List[dict]
external_url = "" # type: str
_receivers = [] # type: List[dict]

@property
def receivers(self) -> List[Receiver]:
"""Cast receivers back from dict."""
return [Receiver(**i) for i in self._ingesters]
return [Receiver(**i) for i in self._receivers]


class TracingEndpointRequirerEvents(CharmEvents):
Expand Down Expand Up @@ -776,7 +783,9 @@ def _on_tracing_relation_changed(self, event):
return

data = TracingProviderAppData.load(relation.data[relation.app])
self.on.endpoint_changed.emit(relation, data.host, [i.dict() for i in data.receivers]) # type: ignore
self.on.endpoint_changed.emit( # type: ignore
relation, data.host, data.external_url, [i.dict() for i in data.receivers]
)

def _on_tracing_relation_broken(self, event: RelationBrokenEvent):
"""Notify the providers that the endpoint is broken."""
Expand All @@ -787,28 +796,43 @@ def get_all_endpoints(
self, relation: Optional[Relation] = None
) -> Optional[TracingProviderAppData]:
"""Unmarshalled relation data."""
if not self.is_ready(relation or self._relation):
relation = relation or self._relation
if not self.is_ready(relation):
return
return TracingProviderAppData.load(relation.data[relation.app]) # type: ignore

def _get_endpoint(
self, relation: Optional[Relation], protocol: ReceiverProtocol, ssl: bool = False
):
ep = self.get_all_endpoints(relation)
if not ep:
self, relation: Optional[Relation], protocol: ReceiverProtocol
) -> Optional[str]:
app_data = self.get_all_endpoints(relation)
if not app_data:
return None
try:
receiver: Receiver = next(filter(lambda i: i.protocol == protocol, ep.receivers))
if receiver.protocol in ["otlp_grpc", "jaeger_grpc"]:
if ssl:
logger.warning("unused ssl argument - was the right protocol called?")
return f"{ep.host}:{receiver.port}"
if ssl:
return f"https://{ep.host}:{receiver.port}"
return f"http://{ep.host}:{receiver.port}"
except StopIteration:
receivers: List[Receiver] = list(
filter(lambda i: i.protocol == protocol, app_data.receivers)
)
if not receivers:
logger.error(f"no receiver found with protocol={protocol!r}")
return None
return
if len(receivers) > 1:
logger.error(
f"too many receivers with protocol={protocol!r}; using first one. Found: {receivers}"
)
return

receiver = receivers[0]
# if there's an external_url argument (v2.5+), use that. Otherwise, we use the tempo local fqdn
if app_data.external_url:
url = app_data.external_url
else:
# FIXME: if we don't get an external url but only a
# hostname, we don't know what scheme we need to be using. ASSUME HTTP
url = f"http://{app_data.host}:{receiver.port}"

if receiver.protocol.endswith("grpc"):
# TCP protocols don't want an http/https scheme prefix
url = url.split("://")[1]

return url

def get_endpoint(
self, protocol: ReceiverProtocol, relation: Optional[Relation] = None
Expand Down

0 comments on commit 75982a8

Please sign in to comment.