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

Commit

Permalink
changed root span name to be more descriptive (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti authored Jun 19, 2024
1 parent bbb8959 commit 7bf0c6a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
13 changes: 8 additions & 5 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 = 9
LIBPATCH = 10

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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}.")

Expand Down
5 changes: 2 additions & 3 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions tests/scenario/test_charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
]


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 7bf0c6a

Please sign in to comment.