From 7bf0c6ae6f351785f1e8f46792b671afd6682aa1 Mon Sep 17 00:00:00 2001 From: PietroPasotti Date: Wed, 19 Jun 2024 09:50:24 +0200 Subject: [PATCH] changed root span name to be more descriptive (#140) --- lib/charms/tempo_k8s/v1/charm_tracing.py | 13 ++++++++----- tests/integration/test_integration.py | 5 ++--- tests/scenario/test_charm_tracing.py | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/charms/tempo_k8s/v1/charm_tracing.py b/lib/charms/tempo_k8s/v1/charm_tracing.py index 5932c8d8..dc84e3f4 100644 --- a/lib/charms/tempo_k8s/v1/charm_tracing.py +++ b/lib/charms/tempo_k8s/v1/charm_tracing.py @@ -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 = 9 +LIBPATCH = 10 PYDEPS = ["opentelemetry-exporter-otlp-proto-http==1.21.0"] @@ -297,13 +297,14 @@ def wrap_init(self: CharmBase, framework: Framework, *args, **kwargs): # default service name isn't just app name because it could conflict with the workload service name _service_name = service_name or f"{self.app.name}-charm" + unit_name = self.unit.name resource = Resource.create( attributes={ "service.name": _service_name, "compose_service": _service_name, "charm_type": type(self).__name__, # juju topology - "juju_unit": self.unit.name, + "juju_unit": unit_name, "juju_application": self.app.name, "juju_model": self.model.name, "juju_model_uuid": self.model.uuid, @@ -341,16 +342,18 @@ def wrap_init(self: CharmBase, framework: Framework, *args, **kwargs): _tracer = get_tracer(_service_name) # type: ignore _tracer_token = tracer.set(_tracer) - dispatch_path = os.getenv("JUJU_DISPATCH_PATH", "") + dispatch_path = os.getenv("JUJU_DISPATCH_PATH", "") # something like hooks/install + event_name = dispatch_path.split("/")[1] if "/" in dispatch_path else dispatch_path + root_span_name = f"{unit_name}: {event_name} event" + span = _tracer.start_span(root_span_name, attributes={"juju.dispatch_path": dispatch_path}) # all these shenanigans are to work around the fact that the opentelemetry tracing API is built # on the assumption that spans will be used as contextmanagers. # Since we don't (as we need to close the span on framework.commit), # we need to manually set the root span as current. - span = _tracer.start_span("charm exec", attributes={"juju.dispatch_path": dispatch_path}) ctx = set_span_in_context(span) - # log a trace id so we can look it up in tempo. + # log a trace id, so we can pick it up from the logs (and jhack) to look it up in tempo. root_trace_id = hex(span.get_span_context().trace_id)[2:] # strip 0x prefix logger.debug(f"Starting root trace with id={root_trace_id!r}.") diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py index a25b6766..67772374 100644 --- a/tests/integration/test_integration.py +++ b/tests/integration/test_integration.py @@ -110,9 +110,8 @@ async def test_verify_traces_http(ops_test: OpsTest): found = False for trace in traces: - if ( - trace["rootServiceName"] == APP_NAME + "-charm" - and trace["rootTraceName"] == "charm exec" + if trace["rootServiceName"] == APP_NAME + "-charm" and trace["rootTraceName"].startswith( + f"{APP_NAME}/0" ): found = True diff --git a/tests/scenario/test_charm_tracing.py b/tests/scenario/test_charm_tracing.py index f297a4cc..49d2de04 100644 --- a/tests/scenario/test_charm_tracing.py +++ b/tests/scenario/test_charm_tracing.py @@ -213,7 +213,7 @@ def test_base_tracer_endpoint_event(caplog): evt = span2.events[0] assert evt.name == "start" - assert span3.name == "charm exec" + assert span3.name == "frank/0: start event" for span in spans: assert span.resource.attributes["service.name"] == "frank-charm" @@ -286,7 +286,7 @@ def test_base_tracer_endpoint_methods(caplog): "method call: MyCharmWithMethods.c", "method call: MyCharmWithMethods._on_start", "event: start", - "charm exec", + "frank/0: start event", ] @@ -339,7 +339,7 @@ def test_base_tracer_endpoint_custom_event(caplog): "event: foo", "method call: MyCharmWithCustomEvents._on_start", "event: start", - "charm exec", + "frank/0: start event", ] # only the charm exec span is a root assert not spans[-1].parent @@ -540,7 +540,7 @@ def test_trace_staticmethods(caplog): "method call: OtherObj._staticmeth2", "method call: MyCharmStaticMethods._on_start", "event: start", - "charm exec", + "jolene/0: start event", ] for span in spans: