-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Event table for ORM to prepare for OTEL traces (#1692)
- Loading branch information
1 parent
c6b7f05
commit 226446d
Showing
8 changed files
with
430 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# !pip install opentelemetry-api\n", | ||
"# !pip install opentelemetry-sdk" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pathlib import Path\n", | ||
"import sys\n", | ||
"\n", | ||
"# Add base dir to path to be able to access test folder.\n", | ||
"base_dir = Path().cwd().parent.parent.resolve()\n", | ||
"if str(base_dir) not in sys.path:\n", | ||
" print(f\"Adding {base_dir} to sys.path\")\n", | ||
" sys.path.append(str(base_dir))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from opentelemetry import trace\n", | ||
"from opentelemetry.sdk.resources import Resource\n", | ||
"from opentelemetry.sdk.trace import TracerProvider\n", | ||
"from opentelemetry.sdk.trace.export import BatchSpanProcessor\n", | ||
"from opentelemetry.sdk.trace.export import ConsoleSpanExporter\n", | ||
"\n", | ||
"SERVICE_NAME = \"trulens\"\n", | ||
"\n", | ||
"\n", | ||
"def init():\n", | ||
" # Use Resource.create() instead of constructor directly\n", | ||
" resource = Resource.create({\"service.name\": SERVICE_NAME})\n", | ||
"\n", | ||
" trace.set_tracer_provider(TracerProvider(resource=resource))\n", | ||
" trace.get_tracer_provider().add_span_processor(\n", | ||
" BatchSpanProcessor(ConsoleSpanExporter())\n", | ||
" )\n", | ||
"\n", | ||
"\n", | ||
"init()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from typing import Callable\n", | ||
"\n", | ||
"from trulens.apps.custom import instrument\n", | ||
"\n", | ||
"SERVICE_NAME = \"trulens\"\n", | ||
"\n", | ||
"\n", | ||
"def decorator(func: Callable):\n", | ||
" tracer = trace.get_tracer(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" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from examples.dev.dummy_app.dummy import Dummy\n", | ||
"\n", | ||
"\n", | ||
"class TestApp(Dummy):\n", | ||
" def __init__(self):\n", | ||
" super().__init__()\n", | ||
"\n", | ||
" @decorator\n", | ||
" @instrument\n", | ||
" def respond_to_query(self, query: str) -> str:\n", | ||
" return f\"answer: {self.nested(query)}\"\n", | ||
"\n", | ||
" @decorator\n", | ||
" @instrument\n", | ||
" def nested(self, query: str) -> str:\n", | ||
" return f\"nested: {query}\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from trulens.core.session import TruSession\n", | ||
"\n", | ||
"SERVICE_NAME = \"trulens\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"session = TruSession()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "trulens", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/core/trulens/core/database/migrations/versions/10_create_event_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""create event table | ||
Revision ID: 10 | ||
Revises: 9 | ||
Create Date: 2024-12-11 09:32:48.976169 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "10" | ||
down_revision = "9" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(config) -> None: | ||
prefix = config.get_main_option("trulens.table_prefix") | ||
|
||
if prefix is None: | ||
raise RuntimeError("trulens.table_prefix is not set") | ||
|
||
op.create_table( | ||
prefix + "events", | ||
sa.Column("event_id", sa.VARCHAR(length=256), nullable=False), | ||
sa.Column("record", sa.JSON(), nullable=False), | ||
sa.Column("record_attributes", sa.JSON(), nullable=False), | ||
sa.Column("record_type", sa.VARCHAR(length=256), nullable=False), | ||
sa.Column("resource_attributes", sa.JSON(), nullable=False), | ||
sa.Column("start_timestamp", sa.TIMESTAMP(), nullable=False), | ||
sa.Column("timestamp", sa.TIMESTAMP(), nullable=False), | ||
sa.Column("trace", sa.JSON(), nullable=False), | ||
sa.PrimaryKeyConstraint("event_id"), | ||
) | ||
|
||
|
||
def downgrade(config) -> None: | ||
prefix = config.get_main_option("trulens.table_prefix") | ||
|
||
if prefix is None: | ||
raise RuntimeError("trulens.table_prefix is not set") | ||
|
||
op.drop_table(prefix + "events") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.