Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): chained entity path on nested tasks #1782

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/traceloop-sdk/tests/test_nested_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from opentelemetry.semconv_ai import SpanAttributes
from traceloop.sdk.decorators import task, workflow
from pytest import raises


def test_nested_tasks(exporter):
Expand Down Expand Up @@ -32,14 +33,15 @@ def inner_inner_task():
inner_inner_task_span = spans[0]
inner_task_span = spans[1]
outer_task_span = spans[2]
some_workflow_span = spans[3]

assert (
inner_inner_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_NAME]
== "outer_task.inner_task.inner_inner_task"
)
assert (
inner_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_NAME]
== "outer_task.inner_task"
inner_inner_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH] ==
"outer_task.inner_task"
)
assert (
outer_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_NAME] == "outer_task"
inner_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH] == "outer_task"
)
with raises(KeyError):
_ = outer_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH]
_ = some_workflow_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH]
20 changes: 8 additions & 12 deletions packages/traceloop-sdk/traceloop/sdk/decorators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from traceloop.sdk.tracing import get_tracer, set_workflow_name
from traceloop.sdk.tracing.tracing import (
TracerWrapper,
set_entity_name,
get_chained_entity_name,
set_entity_path,
get_chained_entity_path,
)
from traceloop.sdk.utils import camel_to_snake
from traceloop.sdk.utils.json_encoder import JSONEncoder
Expand Down Expand Up @@ -47,16 +47,14 @@ def wrap(*args, **kwargs):
TraceloopSpanKindValues.TASK,
TraceloopSpanKindValues.TOOL,
]:
chained_entity_name = get_chained_entity_name(entity_name)
set_entity_name(chained_entity_name)
else:
chained_entity_name = entity_name
entity_path = get_chained_entity_path(entity_name)
set_entity_path(entity_path)

span.set_attribute(
SpanAttributes.TRACELOOP_SPAN_KIND, tlp_span_kind.value
)
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_NAME, chained_entity_name
SpanAttributes.TRACELOOP_ENTITY_NAME, entity_name
)
if version:
span.set_attribute(SpanAttributes.TRACELOOP_ENTITY_VERSION, version)
Expand Down Expand Up @@ -149,16 +147,14 @@ async def wrap(*args, **kwargs):
TraceloopSpanKindValues.TASK,
TraceloopSpanKindValues.TOOL,
]:
chained_entity_name = get_chained_entity_name(entity_name)
set_entity_name(chained_entity_name)
else:
chained_entity_name = entity_name
entity_path = get_chained_entity_path(entity_name)
set_entity_path(entity_path)

span.set_attribute(
SpanAttributes.TRACELOOP_SPAN_KIND, tlp_span_kind.value
)
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_NAME, chained_entity_name
SpanAttributes.TRACELOOP_ENTITY_NAME, entity_name
)
if version:
span.set_attribute(SpanAttributes.TRACELOOP_ENTITY_VERSION, version)
Expand Down
14 changes: 7 additions & 7 deletions packages/traceloop-sdk/traceloop/sdk/tracing/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ def _span_processor_on_start(self, span, parent_context):
if workflow_name is not None:
span.set_attribute(SpanAttributes.TRACELOOP_WORKFLOW_NAME, workflow_name)

entity_name = get_value("entity_name")
if entity_name is not None:
span.set_attribute(SpanAttributes.TRACELOOP_ENTITY_NAME, entity_name)
entity_path = get_value("entity_path")
if entity_path is not None:
span.set_attribute(SpanAttributes.TRACELOOP_ENTITY_PATH, entity_path)

association_properties = get_value("association_properties")
if association_properties is not None:
Expand Down Expand Up @@ -444,12 +444,12 @@ def set_workflow_name(workflow_name: str) -> None:
attach(set_value("workflow_name", workflow_name))


def set_entity_name(entity_name: str) -> None:
attach(set_value("entity_name", entity_name))
def set_entity_path(entity_path: str) -> None:
attach(set_value("entity_path", entity_path))


def get_chained_entity_name(entity_name: str) -> str:
parent = get_value("entity_name")
def get_chained_entity_path(entity_name: str) -> str:
parent = get_value("entity_path")
if parent is None:
return entity_name
else:
Expand Down