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

Commit

Permalink
fix for #120 (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti authored Jun 6, 2024
1 parent 721d53c commit cbd655c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/charms/tempo_k8s/v1/charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,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 = 6
LIBPATCH = 7

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

Expand Down Expand Up @@ -540,11 +540,13 @@ def wrapped_function(*args, **kwargs): # type: ignore
name = getattr(callable, "__qualname__", getattr(callable, "__name__", str(callable)))
with _span(f"{'(static) ' if static else ''}{qualifier} call: {name}"): # type: ignore
if static:
# fixme: do we or don't we need [1:]?
# The _trace_callable decorator doesn't always play nice with @staticmethods.
# Sometimes it will receive 'self', sometimes it won't.
# return callable(*args, **kwargs) # type: ignore
return callable(*args[1:], **kwargs) # type: ignore
# The _trace_callable decorator doesn't play super nice with @staticmethods.
# if you call MyObj().mystaticmethod we'll receive the MyObj instance as first argument,
# if you call MyObj.mystaticmethod we won't.
try:
inspect.signature(callable).bind(*args, **kwargs)
except TypeError:
return callable(*args[1:], **kwargs) # type: ignore
return callable(*args, **kwargs) # type: ignore

# wrapped_function.__signature__ = sig
Expand Down
50 changes: 50 additions & 0 deletions tests/scenario/test_charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,53 @@ def test_borky_tempo_return_value(borky_return_value, caplog):
# logger.exception in _setup_root_span_initializer
assert "exception retrieving the tracing endpoint from" in caplog.text
assert "proceeding with charm_tracing DISABLED." in caplog.text


class MyCharmStaticMethods(CharmBase):
META = {"name": "jolene"}

def __init__(self, fw):
super().__init__(fw)
fw.observe(self.on.start, self._on_start)

def _on_start(self, _):
assert OtherObj()._staticmeth(1) == 2
assert OtherObj._staticmeth(1) == 2

@property
def tempo(self):
return "foo.bar:80"


class OtherObj:
@staticmethod
def _staticmeth(i):
return 1 + i


autoinstrument(MyCharmStaticMethods, MyCharmStaticMethods.tempo, extra_types=[OtherObj])


def test_trace_staticmethods(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmStaticMethods, meta=MyCharmStaticMethods.META)
ctx.run("start", State())

spans = f.call_args_list[0].args[0]

span_names = [span.name for span in spans]
assert span_names == [
"(static) method call: OtherObj._staticmeth",
"(static) method call: OtherObj._staticmeth",
"method call: MyCharmStaticMethods._on_start",
"event: start",
"charm exec",
]

for span in spans:
assert span.resource.attributes["service.name"] == "jolene"

0 comments on commit cbd655c

Please sign in to comment.