Skip to content

Commit 587494e

Browse files
authored
Merge pull request #341 from Scale3-Labs/rohit/S3EN-2793-sentry-integration
Add sentry integration
2 parents c2c7f5e + f45110f commit 587494e

File tree

6 files changed

+38
-1
lines changed

6 files changed

+38
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ langtrace.init(custom_remote_exporter=<your_exporter>, batch=<True or False>)
148148
| `api_host` | `Optional[str]` | `https://langtrace.ai/` | The API host for the remote exporter. |
149149
| `disable_instrumentations` | `Optional[DisableInstrumentations]` | `None` | You can pass an object to disable instrumentation for specific vendors ex: `{'only': ['openai']}` or `{'all_except': ['openai']}` |
150150

151+
### Error Reporting to Langtrace
152+
153+
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`
154+
151155
### Additional Customization
152156

153157
- `@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(<Registry ID>, options={"prompt_version": 1, "
229233
```
230234

231235
### Opt out of tracing prompt and completion data
236+
232237
By default, prompt and completion data are captured. If you would like to opt out of it, set the following env var,
233238

234239
`TRACE_PROMPT_COMPLETION_DATA=false`

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
'sqlalchemy',
3131
'fsspec>=2024.6.0',
3232
"transformers>=4.11.3",
33+
"sentry-sdk>=2.14.0",
3334
]
3435

3536
requires-python = ">=3.9"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
LANGTRACE_SDK_NAME = "langtrace-python-sdk"
2+
SENTRY_DSN = "https://[email protected]/4507929133056000"

src/langtrace_python_sdk/langtrace.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing import Optional
2020
import importlib.util
2121
from colorama import Fore
22+
from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME, SENTRY_DSN
2223
from opentelemetry import trace
2324
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
2425
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
@@ -60,8 +61,9 @@
6061
InstrumentationMethods,
6162
InstrumentationType,
6263
)
63-
from langtrace_python_sdk.utils import check_if_sdk_is_outdated
64+
from langtrace_python_sdk.utils import check_if_sdk_is_outdated, get_sdk_version
6465
from langtrace_python_sdk.utils.langtrace_sampler import LangtraceSampler
66+
import sentry_sdk
6567

6668

6769
def init(
@@ -164,6 +166,27 @@ def init(
164166
provider.add_span_processor(batch_processor_remote)
165167

166168
sys.stdout = sys.__stdout__
169+
if os.environ.get("LANGTRACE_ERROR_REPORTING", "True") == "True":
170+
sentry_sdk.init(
171+
dsn=SENTRY_DSN,
172+
traces_sample_rate=1.0,
173+
profiles_sample_rate=1.0,
174+
)
175+
sdk_options = {
176+
"service_name": os.environ.get("OTEL_SERVICE_NAME")
177+
or service_name
178+
or sys.argv[0],
179+
"disable_logging": disable_logging,
180+
"disable_instrumentations": disable_instrumentations,
181+
"disable_tracing_for_functions": disable_tracing_for_functions,
182+
"batch": batch,
183+
"write_spans_to_console": write_spans_to_console,
184+
"custom_remote_exporter": custom_remote_exporter,
185+
"sdk_name": LANGTRACE_SDK_NAME,
186+
"sdk_version": get_sdk_version(),
187+
"api_host": host,
188+
}
189+
sentry_sdk.set_context("sdk_init_options", sdk_options)
167190

168191

169192
def init_instrumentations(

src/langtrace_python_sdk/utils/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ def set_event_prompt(span: Span, prompt):
3131
def check_if_sdk_is_outdated():
3232
SDKVersionChecker().check()
3333
return
34+
35+
36+
def get_sdk_version():
37+
return SDKVersionChecker().get_sdk_version()

src/langtrace_python_sdk/utils/sdk_version_checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def is_outdated(self):
4444
return self._current_version < latest_version
4545
return False
4646

47+
def get_sdk_version(self):
48+
return self._current_version
49+
4750
def check(self):
4851
if self.is_outdated():
4952
print(

0 commit comments

Comments
 (0)