From 01dc2af801119508e946e86ab23dd876e25fd44f Mon Sep 17 00:00:00 2001 From: Rohit Kadhe Date: Tue, 10 Sep 2024 10:50:18 -0600 Subject: [PATCH 1/2] add sentry integration --- .../constants/__init__.py | 1 + src/langtrace_python_sdk/langtrace.py | 28 ++++++++++++++++++- src/langtrace_python_sdk/utils/__init__.py | 4 +++ .../utils/sdk_version_checker.py | 3 ++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/langtrace_python_sdk/constants/__init__.py b/src/langtrace_python_sdk/constants/__init__.py index f0dbbdb9..4b77a4b4 100644 --- a/src/langtrace_python_sdk/constants/__init__.py +++ b/src/langtrace_python_sdk/constants/__init__.py @@ -1 +1,2 @@ LANGTRACE_SDK_NAME = "langtrace-python-sdk" +SENTRY_DSN = "https://7f8eed3a1237fb2efef0f5e96ab407af@o1419498.ingest.us.sentry.io/4507929133056000" diff --git a/src/langtrace_python_sdk/langtrace.py b/src/langtrace_python_sdk/langtrace.py index 7be1bf9f..c8169aa3 100644 --- a/src/langtrace_python_sdk/langtrace.py +++ b/src/langtrace_python_sdk/langtrace.py @@ -19,6 +19,7 @@ from typing import Optional import importlib.util from colorama import Fore +from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME, SENTRY_DSN from opentelemetry import trace from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from opentelemetry.sdk.resources import SERVICE_NAME, Resource @@ -60,8 +61,9 @@ InstrumentationMethods, InstrumentationType, ) -from langtrace_python_sdk.utils import check_if_sdk_is_outdated +from langtrace_python_sdk.utils import check_if_sdk_is_outdated, get_sdk_version from langtrace_python_sdk.utils.langtrace_sampler import LangtraceSampler +import sentry_sdk def init( @@ -164,6 +166,30 @@ def init( provider.add_span_processor(batch_processor_remote) sys.stdout = sys.__stdout__ + if ( + os.environ.get("LANGTRACE_ERROR_REPORTING", "True") == "True" + or os.environ.get("LANGTRACE_ERROR_REPORTING", "True") == "true" + ): + sentry_sdk.init( + dsn=SENTRY_DSN, + traces_sample_rate=1.0, + profiles_sample_rate=1.0, + ) + sdk_options = { + "service_name": os.environ.get("OTEL_SERVICE_NAME") + or service_name + or sys.argv[0], + "disable_logging": disable_logging, + "disable_instrumentations": disable_instrumentations, + "disable_tracing_for_functions": disable_tracing_for_functions, + "batch": batch, + "write_spans_to_console": write_spans_to_console, + "custom_remote_exporter": custom_remote_exporter, + "sdk_name": LANGTRACE_SDK_NAME, + "sdk_version": get_sdk_version(), + "api_host": host, + } + sentry_sdk.set_context("sdk_init_options", sdk_options) def init_instrumentations( diff --git a/src/langtrace_python_sdk/utils/__init__.py b/src/langtrace_python_sdk/utils/__init__.py index df6925f3..43ffa5a7 100644 --- a/src/langtrace_python_sdk/utils/__init__.py +++ b/src/langtrace_python_sdk/utils/__init__.py @@ -31,3 +31,7 @@ def set_event_prompt(span: Span, prompt): def check_if_sdk_is_outdated(): SDKVersionChecker().check() return + + +def get_sdk_version(): + return SDKVersionChecker().get_sdk_version() diff --git a/src/langtrace_python_sdk/utils/sdk_version_checker.py b/src/langtrace_python_sdk/utils/sdk_version_checker.py index 6b030a27..c708aa82 100644 --- a/src/langtrace_python_sdk/utils/sdk_version_checker.py +++ b/src/langtrace_python_sdk/utils/sdk_version_checker.py @@ -44,6 +44,9 @@ def is_outdated(self): return self._current_version < latest_version return False + def get_sdk_version(self): + return self._current_version + def check(self): if self.is_outdated(): print( From f45110f969d5d6f369434aaef0aafd1aa7312508 Mon Sep 17 00:00:00 2001 From: Rohit Kadhe Date: Tue, 10 Sep 2024 11:01:17 -0600 Subject: [PATCH 2/2] update readme --- README.md | 5 +++++ pyproject.toml | 1 + src/langtrace_python_sdk/langtrace.py | 5 +---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 30d3e6cd..3a65dcab 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,10 @@ langtrace.init(custom_remote_exporter=, batch=) | `api_host` | `Optional[str]` | `https://langtrace.ai/` | The API host for the remote exporter. | | `disable_instrumentations` | `Optional[DisableInstrumentations]` | `None` | You can pass an object to disable instrumentation for specific vendors ex: `{'only': ['openai']}` or `{'all_except': ['openai']}` | +### Error Reporting to Langtrace + +By default all sdk errors are reported to langtrace via Sentry. This can be disabled by setting the following enviroment variable to `False` like so `LANGTRACE_ERROR_REPORTING=False` + ### Additional Customization - `@with_langtrace_root_span` - this decorator is designed to organize and relate different spans, in a hierarchical manner. When you're performing multiple operations that you want to monitor together as a unit, this function helps by establishing a "parent" (`LangtraceRootSpan` or whatever is passed to `name`) span. Then, any calls to the LLM APIs made within the given function (fn) will be considered "children" of this parent span. This setup is especially useful for tracking the performance or behavior of a group of operations collectively, rather than individually. @@ -229,6 +233,7 @@ prompt = get_prompt_from_registry(, options={"prompt_version": 1, " ``` ### Opt out of tracing prompt and completion data + By default, prompt and completion data are captured. If you would like to opt out of it, set the following env var, `TRACE_PROMPT_COMPLETION_DATA=false` diff --git a/pyproject.toml b/pyproject.toml index c0b8c990..4875e93b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ dependencies = [ 'sqlalchemy', 'fsspec>=2024.6.0', "transformers>=4.11.3", + "sentry-sdk>=2.14.0", ] requires-python = ">=3.9" diff --git a/src/langtrace_python_sdk/langtrace.py b/src/langtrace_python_sdk/langtrace.py index c8169aa3..01c6417d 100644 --- a/src/langtrace_python_sdk/langtrace.py +++ b/src/langtrace_python_sdk/langtrace.py @@ -166,10 +166,7 @@ def init( provider.add_span_processor(batch_processor_remote) sys.stdout = sys.__stdout__ - if ( - os.environ.get("LANGTRACE_ERROR_REPORTING", "True") == "True" - or os.environ.get("LANGTRACE_ERROR_REPORTING", "True") == "true" - ): + if os.environ.get("LANGTRACE_ERROR_REPORTING", "True") == "True": sentry_sdk.init( dsn=SENTRY_DSN, traces_sample_rate=1.0,