1616
1717import os
1818import sys
19- from typing import Optional
20- import importlib .util
19+ from typing import Any , Optional
2120from colorama import Fore
2221from langtrace_python_sdk .constants import LANGTRACE_SDK_NAME , SENTRY_DSN
2322from opentelemetry import trace
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+ )
6670from langtrace_python_sdk .utils .langtrace_sampler import LangtraceSampler
6771import sentry_sdk
6872
@@ -193,10 +197,10 @@ def init(
193197
194198def 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 ()
0 commit comments