Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Reduce log spam on looking up tracer (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkay authored Feb 8, 2024
1 parent abb3087 commit b849798
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
16 changes: 5 additions & 11 deletions lib/charms/tempo_k8s/v1/charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def my_tracing_endpoint(self) -> Optional[str]:
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version

LIBPATCH = 1
LIBPATCH = 2

PYDEPS = ["opentelemetry-exporter-otlp-proto-http>=1.21.0"]

Expand Down Expand Up @@ -200,15 +200,12 @@ def _get_tracer() -> Optional[Tracer]:
return tracer.get()
except LookupError:
try:
logger.debug("tracer was not found in context variable, looking up in default context")
ctx: Context = copy_context()
if context_tracer := _get_tracer_from_context(ctx):
return context_tracer.get()
else:
logger.debug("Couldn't find context var for tracer: span will be skipped")
return None
except LookupError as err:
logger.debug(f"Couldn't find tracer: span will be skipped, err: {err}")
return None


Expand All @@ -219,7 +216,6 @@ def _span(name: str) -> Generator[Optional[Span], Any, Any]:
with tracer.start_as_current_span(name) as span:
yield cast(Span, span)
else:
logger.debug("tracer not found")
yield None


Expand All @@ -243,9 +239,9 @@ def _get_tracing_endpoint(tracing_endpoint_getter, self, charm):
tracing_endpoint = tracing_endpoint_getter(self)

if tracing_endpoint is None:
logger.warning(
f"{charm}.{getattr(tracing_endpoint_getter, '__qualname__', str(tracing_endpoint_getter))} "
f"returned None; continuing with tracing DISABLED."
logger.debug(
"Charm tracing is disabled. Tracing endpoint is not defined - "
"tracing is not available or relation is not set."
)
return
elif not isinstance(tracing_endpoint, str):
Expand All @@ -266,15 +262,14 @@ def _get_server_cert(server_cert_getter, self, charm):

if server_cert is None:
logger.warning(
f"{charm}.{server_cert_getter} returned None; continuing with INSECURE connection."
f"{charm}.{server_cert_getter} returned None; sending traces over INSECURE connection."
)
return
elif not Path(server_cert).is_absolute():
raise ValueError(
f"{charm}.{server_cert_getter} should return a valid tls cert absolute path (string | Path)); "
f"got {server_cert} instead."
)
logger.debug("Certificate successfully retrieved.") # todo: some more validation?
return server_cert


Expand All @@ -300,7 +295,6 @@ def wrap_init(self: CharmBase, framework: Framework, *args, **kwargs):

original_event_context = framework._event_context

logging.debug("Initializing opentelemetry tracer...")
_service_name = service_name or self.app.name

resource = Resource.create(
Expand Down
2 changes: 1 addition & 1 deletion tests/scenario/test_charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_base_tracer_endpoint_disabled(caplog):
ctx = Context(MyCharmSimpleDisabled, meta=MyCharmSimpleDisabled.META)
ctx.run("start", State())

assert "continuing with tracing DISABLED." in caplog.text
assert "Charm tracing is disabled." in caplog.text
assert not f.called


Expand Down

0 comments on commit b849798

Please sign in to comment.