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 5 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
67 changes: 17 additions & 50 deletions packages/sample-app/sample_app/methods_decorated_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from traceloop.sdk import Traceloop
from traceloop.sdk.decorators import task, agent, workflow, tool
from traceloop.sdk.decorators import task, workflow

from openai import OpenAI

Expand All @@ -10,65 +10,32 @@
Traceloop.init(app_name="joke_generation_service")


@task(name="joke_creation", version=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you delete this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I copied this file from Gal branch, I just saw he excluded it from his merged PR, so I'll remove this change as well 🙈

def create_joke():
Traceloop.set_prompt(
"Tell me a joke about {subject}", {"subject": "opentelemetry"}, 1
)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Tell me a joke about opentelemetry"}],
)

return completion.choices[0].message.content


@agent(name="joke_translation")
def translate_joke_to_pirate(joke: str):
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": f"Translate the below joke to pirate-like english:\n\n{joke}",
}
],
)

history_jokes_tool()

return completion.choices[0].message.content


@tool(name="history_jokes")
def history_jokes_tool():
@task(name="pirate_name_extraction", version=1)
def extract_pirate_name():
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "get some history jokes"}],
messages=[{
"role": "user",
"content": """
Instructions: What is the name of the pirate name in the text below?

Text: Bill the pirate walks into a bar with a steering wheel in his pants. The bartender asks,
"Hey, what's with the steering wheel in your pants?" The pirate says, "Arrr, it's driving me nuts!"
"""
}],
)

return completion.choices[0].message.content


@task(name="signature_generation")
def generate_signature(joke: str):
completion = client.completions.create(
model="davinci-002", prompt="add a signature to the joke:\n\n" + joke
)

return completion.choices[0].text
@task(name="pirate_name_extraction_wrapper", version=1)
def extract_pirate_name_wrapper():
return extract_pirate_name()


@workflow(name="pirate_joke_generator")
@workflow(name="pirate_name_extraction")
def joke_workflow():
Traceloop.set_association_properties(
{"user_id": "user_12345", "chat_id": "chat_1234"}
)

eng_joke = create_joke()
pirate_joke = translate_joke_to_pirate(eng_joke)
signature = generate_signature(pirate_joke)
print(pirate_joke + "\n\n" + signature)
extract_pirate_name_wrapper()


joke_workflow()
14 changes: 9 additions & 5 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,17 @@ 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"
inner_inner_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH] ==
"outer_task.inner_task.inner_inner_task"
)
assert (
inner_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_NAME]
== "outer_task.inner_task"
inner_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH] == "outer_task.inner_task"
)
assert (
outer_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_NAME] == "outer_task"
outer_task_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH] == "outer_task"
)
with raises(KeyError):
_ = some_workflow_span.attributes[SpanAttributes.TRACELOOP_ENTITY_PATH]
22 changes: 10 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,15 @@ 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_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 +148,15 @@ 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_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