Skip to content

Commit

Permalink
add decorators onto tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
axiomofjoy committed Jan 18, 2025
1 parent c499fe1 commit 177b7d6
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 289 deletions.
67 changes: 20 additions & 47 deletions python/openinference-instrumentation/examples/tracer.ipynb
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notes:\n",
"\n",
"- imagine `phoenix.otel.register` returns `OpenInferenceTracerProvider`\n",
"- imagine there is an `@agent` decorator that behaves the same as `@chain`\n",
"- imagine decorators can be also be used from a particular tracer with `@tracer.chain`\n",
"- import paths will change\n",
"\n",
"\n",
"Proposed imports:\n",
"\n",
"```python\n",
"from openinference.instrumentation import (\n",
" get_input_value_and_mime_type,\n",
" get_openinference_span_kind,\n",
" get_output_value_and_mime_type,\n",
" get_tool_attributes,\n",
")\n",
"from openinference.instrumentation import tool, chain, OpenInferenceTracerProvider\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
Expand All @@ -43,14 +18,12 @@
"\n",
"from openinference.instrumentation import (\n",
" TracerProvider,\n",
" chain,\n",
" get_current_span,\n",
" get_input_value_and_mime_type,\n",
" get_output_value_and_mime_type,\n",
" get_span_kind,\n",
" get_tool_attributes,\n",
" suppress_tracing,\n",
" tool,\n",
" using_attributes,\n",
")\n",
"from openinference.semconv.resource import ResourceAttributes"
Expand Down Expand Up @@ -174,7 +147,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_plain_text_output(input: str) -> str:\n",
" return \"output\"\n",
"\n",
Expand All @@ -188,7 +161,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_json_output(input: str) -> Dict[str, Any]:\n",
" return {\"output\": \"output\"}\n",
"\n",
Expand All @@ -202,7 +175,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain()\n",
"@tracer.chain()\n",
"def decorated_chain_with_no_parameters(input: str) -> Dict[str, Any]:\n",
" return {\"output\": \"output\"}\n",
"\n",
Expand All @@ -216,7 +189,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain(name=\"decorated-chain-with-overriden-name\")\n",
"@tracer.chain(name=\"decorated-chain-with-overriden-name\")\n",
"def this_name_should_be_overriden(input: str) -> Dict[str, Any]:\n",
" return {\"output\": \"output\"}\n",
"\n",
Expand All @@ -234,7 +207,7 @@
" return {\"output\": \"output\"}\n",
"\n",
"\n",
"decorated = chain(chain_with_decorator_applied_as_function)\n",
"decorated = tracer.chain(chain_with_decorator_applied_as_function)\n",
"decorated(\"input\")"
]
},
Expand All @@ -250,9 +223,9 @@
" return {\"output\": \"output\"}\n",
"\n",
"\n",
"decorated = chain(name=\"decorated-chain-with-decorator-applied-as-function-with-overriden-name\")(\n",
" this_name_should_be_overriden_with_decorator_applied_as_function_with_parameters\n",
")\n",
"decorated = tracer.chain(\n",
" name=\"decorated-chain-with-decorator-applied-as-function-with-overriden-name\"\n",
")(this_name_should_be_overriden_with_decorator_applied_as_function_with_parameters)\n",
"decorated(\"input\")"
]
},
Expand All @@ -262,7 +235,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"async def decorated_async_chain(input: str) -> str:\n",
" return \"output\"\n",
"\n",
Expand All @@ -276,7 +249,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_error(input: str) -> str:\n",
" raise ValueError(\"error\")\n",
"\n",
Expand All @@ -293,7 +266,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_child_span(input: str) -> str:\n",
" with tracer.start_as_current_span(\n",
" \"child-span\",\n",
Expand All @@ -315,7 +288,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_child_span_error(input: str) -> str:\n",
" with tracer.start_as_current_span(\n",
" \"child-span\",\n",
Expand All @@ -338,7 +311,7 @@
"outputs": [],
"source": [
"class ChainRunner:\n",
" @chain\n",
" @tracer.chain\n",
" def decorated_chain_method(self, input1: str, input2: str) -> str:\n",
" return \"output\"\n",
"\n",
Expand All @@ -353,7 +326,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_input_and_output_set_inside_the_wrapped_function(input: str) -> str:\n",
" span = get_current_span()\n",
" span.set_input(\"overridden-input\")\n",
Expand Down Expand Up @@ -393,7 +366,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_suppress_tracing(input: str) -> str:\n",
" return \"output\"\n",
"\n",
Expand Down Expand Up @@ -431,7 +404,7 @@
"metadata": {},
"outputs": [],
"source": [
"@chain\n",
"@tracer.chain\n",
"def decorated_chain_with_context_attributes(input: str) -> str:\n",
" return \"output\"\n",
"\n",
Expand Down Expand Up @@ -500,7 +473,7 @@
"metadata": {},
"outputs": [],
"source": [
"@tool\n",
"@tracer.tool\n",
"def decorated_tool(input1: str, input2: int) -> None:\n",
" \"\"\"\n",
" tool-description\n",
Expand All @@ -516,7 +489,7 @@
"metadata": {},
"outputs": [],
"source": [
"@tool\n",
"@tracer.tool\n",
"async def decorated_tool_async(input1: str, input2: int) -> None:\n",
" \"\"\"\n",
" tool-description\n",
Expand All @@ -532,7 +505,7 @@
"metadata": {},
"outputs": [],
"source": [
"@tool(\n",
"@tracer.tool(\n",
" name=\"decorated-tool-with-overriden-name\",\n",
" description=\"overriden-tool-description\",\n",
")\n",
Expand All @@ -551,7 +524,7 @@
"metadata": {},
"outputs": [],
"source": [
"@tool\n",
"@tracer.tool\n",
"def tool_with_changes_inside_the_wrapped_function(input1: str, input2: int) -> str:\n",
" span = get_current_span()\n",
" print(type(span))\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
OITracer,
TraceConfig,
TracerProvider,
chain,
get_current_span,
get_input_value_and_mime_type,
get_output_value_and_mime_type,
get_span_kind,
get_tool_attributes,
suppress_tracing,
tool,
)
from .context_attributes import (
get_attributes_from_context,
Expand Down Expand Up @@ -42,11 +40,9 @@
"OITracer",
"REDACTED_VALUE",
"TracerProvider",
"chain",
"get_current_span",
"get_input_value_and_mime_type",
"get_output_value_and_mime_type",
"get_span_kind",
"get_tool_attributes",
"tool",
]
Loading

0 comments on commit 177b7d6

Please sign in to comment.