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

Change recording to create a span of type "record_root" #1703

Open
wants to merge 62 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
2b4a3a9
draft
sfc-gh-gtokernliang Dec 11, 2024
49d79dc
update
sfc-gh-gtokernliang Dec 12, 2024
7207252
prefix
sfc-gh-gtokernliang Dec 17, 2024
f433019
updates
sfc-gh-gtokernliang Dec 18, 2024
2b03079
touchups
sfc-gh-gtokernliang Dec 18, 2024
a8e4955
add event_id
sfc-gh-gtokernliang Dec 18, 2024
a6c0b63
add type schema
sfc-gh-gtokernliang Dec 18, 2024
0ca7ebc
add event_id
sfc-gh-gtokernliang Dec 18, 2024
1c6cbc2
minor updates
sfc-gh-gtokernliang Dec 18, 2024
3059d79
pr feedback
sfc-gh-gtokernliang Dec 19, 2024
021a43c
update
sfc-gh-gtokernliang Dec 19, 2024
42f0575
ORM update
sfc-gh-gtokernliang Dec 20, 2024
fb7cf3b
Merge branch 'main' of github.com:truera/trulens into garett/SNOW-185…
sfc-gh-gtokernliang Dec 20, 2024
9c313b4
save
sfc-gh-gtokernliang Dec 11, 2024
20bb651
update exporter
sfc-gh-gtokernliang Dec 12, 2024
d0b4398
update
sfc-gh-gtokernliang Dec 12, 2024
99dcb44
nits
sfc-gh-gtokernliang Dec 12, 2024
834d6e7
fix parent
sfc-gh-gtokernliang Dec 16, 2024
eb61153
save
sfc-gh-gtokernliang Dec 18, 2024
7fd7de2
save
sfc-gh-gtokernliang Dec 18, 2024
aad3ffa
update typing
sfc-gh-gtokernliang Dec 18, 2024
9e27b0f
update
sfc-gh-gtokernliang Dec 18, 2024
8004a55
update
sfc-gh-gtokernliang Dec 19, 2024
f35f2b0
update exporter to have the attributes in attributes
sfc-gh-gtokernliang Dec 20, 2024
4ed4546
save
sfc-gh-gtokernliang Dec 12, 2024
c1516c7
save
sfc-gh-gtokernliang Dec 12, 2024
85f2e62
update
sfc-gh-gtokernliang Dec 12, 2024
47672f2
add try-catch
sfc-gh-gtokernliang Dec 12, 2024
aafae55
updatE
sfc-gh-gtokernliang Dec 18, 2024
3d23e0b
PR feedback
sfc-gh-gtokernliang Dec 18, 2024
fbc6e60
updates
sfc-gh-gtokernliang Dec 18, 2024
efa8c6e
update a bit
sfc-gh-gtokernliang Dec 18, 2024
cf97904
remove redundant print
sfc-gh-gtokernliang Dec 18, 2024
dc98c62
remove snowflake
sfc-gh-gtokernliang Dec 19, 2024
7dae6ff
remove instrument
sfc-gh-gtokernliang Dec 19, 2024
e1b685c
prepend namespace
sfc-gh-gtokernliang Dec 21, 2024
bf80a11
update semcov
sfc-gh-gtokernliang Dec 21, 2024
e64f8ee
Merge remote-tracking branch 'origin/main' into garett/SNOW-1854278
sfc-gh-dkurokawa Dec 21, 2024
79b157f
Fix api tests.
sfc-gh-dkurokawa Dec 21, 2024
0e3564b
Incorporate my own comments in the review except for the adding of a …
sfc-gh-dkurokawa Dec 21, 2024
4528258
Add framework for test.
sfc-gh-dkurokawa Dec 21, 2024
a1c461c
Add in better test.
sfc-gh-dkurokawa Dec 22, 2024
1c34cd8
Clean up some issues and fix import issues.
sfc-gh-dkurokawa Dec 22, 2024
7814807
Use `Dict` instead of `dict` for types.
sfc-gh-dkurokawa Dec 22, 2024
c390f94
Allow otel test to run with experimental flag.
sfc-gh-dkurokawa Dec 22, 2024
fe12d1b
Don't compare sdk versions.
sfc-gh-dkurokawa Dec 22, 2024
a7e3f53
Handle all rows that need to have the sdk version ignored.
sfc-gh-dkurokawa Dec 23, 2024
5dbc83a
Fix handling `None` issue for `ignore_locators` arg.
sfc-gh-dkurokawa Dec 23, 2024
3f1607c
save
sfc-gh-gtokernliang Dec 16, 2024
2a6e699
draft
sfc-gh-gtokernliang Dec 19, 2024
41b37b0
update
sfc-gh-gtokernliang Dec 19, 2024
2067909
save
sfc-gh-gtokernliang Dec 20, 2024
c61dda6
add back debugger
sfc-gh-gtokernliang Dec 20, 2024
402c0e5
update notebook
sfc-gh-gtokernliang Dec 20, 2024
364f6aa
fix
sfc-gh-gtokernliang Dec 21, 2024
17619e6
update semcov
sfc-gh-gtokernliang Dec 21, 2024
d300f67
remove artifacts
sfc-gh-gtokernliang Dec 24, 2024
659f82c
Merge branch 'main' of github.com:truera/trulens into garett/SNOW-185…
sfc-gh-gtokernliang Dec 24, 2024
bc26f34
remove span_types from SpanAttributes
sfc-gh-gtokernliang Dec 24, 2024
86850df
modified it to accept multiple tokens
sfc-gh-gtokernliang Dec 24, 2024
57b6809
fix bug with multiple func calls
sfc-gh-gtokernliang Dec 24, 2024
14869e1
PR feedback
sfc-gh-gtokernliang Dec 24, 2024
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
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ env-%:

env-tests:
poetry run pip install \
pytest \
jsondiff \
nbconvert \
nbformat \
pytest-subtests \
pytest-azurepipelines \
ruff \
opentelemetry-sdk \
pre-commit \
pytest \
pytest-azurepipelines \
pytest-cov \
jsondiff
pytest-subtests \
ruff \

env-tests-required:
poetry install --only required \
Expand Down
93 changes: 59 additions & 34 deletions examples/experimental/otel_exporter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,18 @@
"metadata": {},
"outputs": [],
"source": [
"from typing import Callable\n",
"import logging\n",
"\n",
"from opentelemetry import trace\n",
"from trulens.apps.custom import instrument\n",
"from trulens.experimental.otel_tracing.core.init import TRULENS_SERVICE_NAME\n",
"\n",
"\n",
"def decorator(func: Callable):\n",
" tracer = trace.get_tracer(TRULENS_SERVICE_NAME)\n",
"\n",
" def wrapper(*args, **kwargs):\n",
" print(\"start wrap\")\n",
"\n",
" with tracer.start_as_current_span(\"custom\"):\n",
" result = func(*args, **kwargs)\n",
" span = trace.get_current_span()\n",
" print(\"---span---\")\n",
" print(span.get_span_context())\n",
" span.set_attribute(\"result\", result)\n",
" span.set_status(trace.Status(trace.StatusCode.OK))\n",
" return result\n",
"\n",
" return wrapper"
"root = logging.getLogger()\n",
"root.setLevel(logging.DEBUG)\n",
"handler = logging.StreamHandler(sys.stdout)\n",
"handler.setLevel(logging.DEBUG)\n",
"handler.addFilter(logging.Filter(\"trulens\"))\n",
"formatter = logging.Formatter(\n",
" \"%(asctime)s - %(name)s - %(levelname)s - %(message)s\"\n",
")\n",
"handler.setFormatter(formatter)\n",
"root.addHandler(handler)"
]
},
{
Expand All @@ -63,22 +52,46 @@
"metadata": {},
"outputs": [],
"source": [
"from examples.dev.dummy_app.dummy import Dummy\n",
"\n",
"from trulens.experimental.otel_tracing.core.instrument import instrument\n",
"\n",
"class TestApp(Dummy):\n",
" def __init__(self):\n",
" super().__init__()\n",
"\n",
" @decorator\n",
" @instrument\n",
"class TestApp:\n",
" @instrument()\n",
" def respond_to_query(self, query: str) -> str:\n",
" return f\"answer: {self.nested(query)}\"\n",
"\n",
" @decorator\n",
" @instrument\n",
" @instrument(attributes={\"nested_attr1\": \"value1\"})\n",
" def nested(self, query: str) -> str:\n",
" return f\"nested: {query}\""
" return f\"nested: {self.nested2(query)}\"\n",
"\n",
" @instrument(\n",
" attributes=lambda ret, exception, *args, **kwargs: {\n",
" \"nested2_ret\": ret,\n",
" \"nested2_args[0]\": args[0],\n",
" }\n",
" )\n",
" def nested2(self, query: str) -> str:\n",
" nested_result = \"\"\n",
"\n",
" try:\n",
" nested_result = self.nested3(query)\n",
" except Exception:\n",
" pass\n",
"\n",
" return f\"nested2: {nested_result}\"\n",
"\n",
" @instrument(\n",
" attributes=lambda ret, exception, *args, **kwargs: {\n",
" \"nested3_ex\": exception.args if exception else None,\n",
" \"nested3_ret\": ret,\n",
" \"selector_name\": \"special\",\n",
" \"cows\": \"moo\",\n",
" }\n",
" )\n",
" def nested3(self, query: str) -> str:\n",
" if query == \"throw\":\n",
" raise ValueError(\"nested3 exception\")\n",
" return \"nested3\""
]
},
{
Expand All @@ -87,11 +100,16 @@
"metadata": {},
"outputs": [],
"source": [
"import dotenv\n",
"from trulens.core.session import TruSession\n",
"from trulens.experimental.otel_tracing.core.init import init\n",
"\n",
"dotenv.load_dotenv()\n",
"\n",
"session = TruSession()\n",
"init(session)"
"session.experimental_enable_feature(\"otel_tracing\")\n",
sfc-gh-gtokernliang marked this conversation as resolved.
Show resolved Hide resolved
"session.reset_database()\n",
"init(session, debug=True)"
]
},
{
Expand All @@ -100,9 +118,16 @@
"metadata": {},
"outputs": [],
"source": [
"from trulens.apps.custom import TruCustomApp\n",
"\n",
"test_app = TestApp()\n",
"custom_app = TruCustomApp(test_app)\n",
"\n",
"with custom_app as recording:\n",
" test_app.respond_to_query(\"test\")\n",
"\n",
"test_app.respond_to_query(\"test\")"
"with custom_app as recording:\n",
" test_app.respond_to_query(\"throw\")"
]
}
],
Expand Down
38 changes: 28 additions & 10 deletions src/core/trulens/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from abc import ABC
from abc import ABCMeta
from abc import abstractmethod
import contextlib
import contextvars
import datetime
import inspect
Expand Down Expand Up @@ -421,6 +422,17 @@ def tru(self) -> core_connector.DBConnector:
pydantic.PrivateAttr(default_factory=dict)
)

token: Optional[object] = None
"""
OTEL context token for the current context manager.
"""

span_context: Optional[contextlib.AbstractContextManager] = None
"""
Span context manager. Required to help keep track of the appropriate span context
to enter/exit.
"""

def __init__(
self,
connector: Optional[core_connector.DBConnector] = None,
Expand Down Expand Up @@ -1048,27 +1060,29 @@ def __enter__(self):
if self.session.experimental_feature(
core_experimental.Feature.OTEL_TRACING
):
from trulens.experimental.otel_tracing.core.app import _App
from trulens.experimental.otel_tracing.core.instrument import (
App as OTELApp,
)

return _App.__enter__(self)
return OTELApp.__enter__(self)

ctx = core_instruments._RecordingContext(app=self)

token = self.recording_contexts.set(ctx)
ctx.token = token

# self._set_context_vars()

return ctx

# For use as a context manager.
def __exit__(self, exc_type, exc_value, exc_tb):
if self.session.experimental_feature(
core_experimental.Feature.OTEL_TRACING
):
from trulens.experimental.otel_tracing.core.app import _App
from trulens.experimental.otel_tracing.core.instrument import (
App as OTELApp,
)

return _App.__exit__(self, exc_type, exc_value, exc_tb)
return OTELApp.__exit__(self, exc_type, exc_value, exc_tb)

ctx = self.recording_contexts.get()
self.recording_contexts.reset(ctx.token)
Expand All @@ -1085,9 +1099,11 @@ async def __aenter__(self):
if self.session.experimental_feature(
core_experimental.Feature.OTEL_TRACING
):
from trulens.experimental.otel_tracing.core.app import _App
from trulens.experimental.otel_tracing.core.instrument import (
App as OTELApp,
)

return await _App.__aenter__(self)
return OTELApp.__enter__(self)

ctx = core_instruments._RecordingContext(app=self)

Expand All @@ -1103,9 +1119,11 @@ async def __aexit__(self, exc_type, exc_value, exc_tb):
if self.session.experimental_feature(
core_experimental.Feature.OTEL_TRACING
):
from trulens.experimental.otel_tracing.core.app import _App
from trulens.experimental.otel_tracing.core.instrument import (
App as OTELApp,
)

return await _App.__aexit__(self, exc_type, exc_value, exc_tb)
return OTELApp.__exit__(self, exc_type, exc_value, exc_tb)

ctx = self.recording_contexts.get()
self.recording_contexts.reset(ctx.token)
Expand Down
1 change: 1 addition & 0 deletions src/core/trulens/core/database/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ def get_datasets(self) -> pd.DataFrame:

def insert_event(self, event: Event) -> types_schema.EventID:
"""See [DB.insert_event][trulens.core.database.base.DB.insert_event]."""

with self.session.begin() as session:
_event = self.orm.Event.parse(event, redact_keys=self.redact_keys)
session.add(_event)
Expand Down
Loading