Skip to content

Commit 61b79d3

Browse files
authored
Merge pull request #355 from Scale3-Labs/development
Release 2.3.17: Fix disable instrumentation bug
2 parents 44e97ca + 5117012 commit 61b79d3

File tree

4 files changed

+82
-70
lines changed

4 files changed

+82
-70
lines changed

src/langtrace_python_sdk/langtrace.py

Lines changed: 25 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
import os
1818
import sys
19-
from typing import Optional
20-
import importlib.util
19+
from typing import Any, Optional
2120
from colorama import Fore
2221
from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME, SENTRY_DSN
2322
from opentelemetry import trace
@@ -62,7 +61,12 @@
6261
InstrumentationMethods,
6362
InstrumentationType,
6463
)
65-
from langtrace_python_sdk.utils import check_if_sdk_is_outdated, get_sdk_version
64+
from langtrace_python_sdk.utils import (
65+
check_if_sdk_is_outdated,
66+
get_sdk_version,
67+
is_package_installed,
68+
validate_instrumentations,
69+
)
6670
from langtrace_python_sdk.utils.langtrace_sampler import LangtraceSampler
6771
import sentry_sdk
6872

@@ -193,10 +197,10 @@ def init(
193197

194198
def init_instrumentations(
195199
disable_instrumentations: Optional[DisableInstrumentations],
196-
all_instrumentations: dict
200+
all_instrumentations: dict,
197201
):
198202
if disable_instrumentations is None:
199-
for idx, (name, v) in enumerate(all_instrumentations.items()):
203+
for name, v in all_instrumentations.items():
200204
if is_package_installed(name):
201205
v.instrument()
202206

@@ -205,61 +209,19 @@ def init_instrumentations(
205209
validate_instrumentations(disable_instrumentations)
206210

207211
for key in disable_instrumentations:
208-
for vendor in disable_instrumentations[key]:
209-
if key == "only":
210-
filtered_dict = {
211-
k: v
212-
for k, v in all_instrumentations.items()
213-
if k != vendor.value
214-
}
215-
for _, v in filtered_dict.items():
216-
v.instrument()
217-
else:
218-
filtered_dict = {
219-
k: v
220-
for k, v in all_instrumentations.items()
221-
if k == vendor.value
222-
}
223-
224-
for _, v in filtered_dict.items():
225-
v.instrument()
226-
227-
228-
def validate_instrumentations(disable_instrumentations):
229-
if disable_instrumentations is not None:
230-
for key, value in disable_instrumentations.items():
231-
if isinstance(value, str):
232-
# Convert single string to list of enum values
233-
disable_instrumentations[key] = [InstrumentationType.from_string(value)]
234-
elif isinstance(value, list):
235-
# Convert list of strings to list of enum values
236-
disable_instrumentations[key] = [
237-
(
238-
InstrumentationType.from_string(item)
239-
if isinstance(item, str)
240-
else item
241-
)
242-
for item in value
243-
]
244-
# Validate all items are of enum type
245-
if not all(
246-
isinstance(item, InstrumentationType)
247-
for item in disable_instrumentations[key]
248-
):
249-
raise TypeError(
250-
f"All items in {key} must be of type InstrumentationType"
251-
)
252-
if (
253-
disable_instrumentations.get("all_except") is not None
254-
and disable_instrumentations.get("only") is not None
255-
):
256-
raise ValueError(
257-
"Cannot specify both only and all_except in disable_instrumentations"
258-
)
259-
260-
261-
def is_package_installed(package_name):
262-
import pkg_resources
263-
264-
installed_packages = {p.key for p in pkg_resources.working_set}
265-
return package_name in installed_packages
212+
vendors = [k.value for k in disable_instrumentations[key]]
213+
214+
key = next(iter(disable_instrumentations))
215+
filtered_dict = {}
216+
if key == "all_except":
217+
filtered_dict = {
218+
k: v for k, v in all_instrumentations.items() if k in vendors
219+
}
220+
elif key == "only":
221+
filtered_dict = {
222+
k: v for k, v in all_instrumentations.items() if k not in vendors
223+
}
224+
225+
for name, v in filtered_dict.items():
226+
if is_package_installed(name):
227+
v.instrument()

src/langtrace_python_sdk/types/__init__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ class InstrumentationType(Enum):
88
ANTHROPIC = "anthropic"
99
GROQ = "groq"
1010
MISTRAL = "mistral"
11-
PINECONE = "pinecone"
12-
LLAMAINDEX = "llamaindex"
11+
PINECONE = "pinecone-client"
12+
LLAMAINDEX = "llama-index"
1313
CHROMADB = "chromadb"
1414
QDRANT = "qdrant"
1515
LANGCHAIN = "langchain"
16-
LANGCHAIN_CORE = "langchain_core"
17-
LANGCHAIN_COMMUNITY = "langchain_community"
16+
LANGCHAIN_CORE = "langchain-core"
17+
LANGCHAIN_COMMUNITY = "langchain-community"
1818
LANGGRAPH = "langgraph"
1919
WEAVIATE = "weaviate"
2020
OLLAMA = "ollama"
21+
AUTOGEN = "autogen"
22+
DSPY = "dspy-ai"
23+
CREWAI = "crewai"
24+
GEMINI = "google-generativeai"
25+
VERTEXAI = "google-cloud-aiplatform"
26+
MISTRALAI = "mistralai"
2127

2228
@staticmethod
2329
def from_string(value: str):
@@ -112,7 +118,10 @@ class InstrumentationMethods(TypedDict):
112118
cohere: List[VendorMethods.CohereMethods]
113119
weaviate: List[str]
114120

121+
115122
_T = TypeVar("_T")
123+
124+
116125
class NotGiven:
117126
"""
118127
A sentinel singleton class used to distinguish omitted keyword arguments
@@ -139,4 +148,4 @@ def __repr__(self) -> str:
139148

140149

141150
NotGivenOr = Union[_T, NotGiven]
142-
NOT_GIVEN = NotGiven()
151+
NOT_GIVEN = NotGiven()

src/langtrace_python_sdk/utils/__init__.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from langtrace_python_sdk.types import NOT_GIVEN
1+
from langtrace_python_sdk.types import NOT_GIVEN, InstrumentationType
22
from .sdk_version_checker import SDKVersionChecker
33
from opentelemetry.trace import Span
44
from langtrace.trace_attributes import SpanAttributes
@@ -49,3 +49,44 @@ def check_if_sdk_is_outdated():
4949

5050
def get_sdk_version():
5151
return SDKVersionChecker().get_sdk_version()
52+
53+
54+
def validate_instrumentations(disable_instrumentations):
55+
if disable_instrumentations is not None:
56+
if (
57+
disable_instrumentations.get("all_except") is not None
58+
and disable_instrumentations.get("only") is not None
59+
):
60+
raise ValueError(
61+
"Cannot specify both only and all_except in disable_instrumentations"
62+
)
63+
64+
for key, value in disable_instrumentations.items():
65+
if isinstance(value, str):
66+
# Convert single string to list of enum values
67+
disable_instrumentations[key] = [InstrumentationType.from_string(value)]
68+
elif isinstance(value, list):
69+
# Convert list of strings to list of enum values
70+
disable_instrumentations[key] = [
71+
(
72+
InstrumentationType.from_string(item)
73+
if isinstance(item, str)
74+
else item
75+
)
76+
for item in value
77+
]
78+
# Validate all items are of enum type
79+
if not all(
80+
isinstance(item, InstrumentationType)
81+
for item in disable_instrumentations[key]
82+
):
83+
raise TypeError(
84+
f"All items in {key} must be of type InstrumentationType"
85+
)
86+
87+
88+
def is_package_installed(package_name):
89+
import pkg_resources
90+
91+
installed_packages = {p.key for p in pkg_resources.working_set}
92+
return package_name in installed_packages
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.3.16"
1+
__version__ = "2.3.17"

0 commit comments

Comments
 (0)