lmnr 0.5.3__py3-none-any.whl → 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. lmnr/__init__.py +6 -1
  2. lmnr/opentelemetry_lib/__init__.py +16 -36
  3. lmnr/opentelemetry_lib/decorators/__init__.py +219 -0
  4. lmnr/opentelemetry_lib/tracing/__init__.py +139 -1
  5. lmnr/opentelemetry_lib/tracing/_instrument_initializers.py +398 -0
  6. lmnr/opentelemetry_lib/tracing/attributes.py +14 -7
  7. lmnr/opentelemetry_lib/tracing/context_properties.py +53 -0
  8. lmnr/opentelemetry_lib/tracing/exporter.py +60 -0
  9. lmnr/opentelemetry_lib/tracing/instruments.py +121 -0
  10. lmnr/opentelemetry_lib/tracing/processor.py +96 -0
  11. lmnr/opentelemetry_lib/tracing/{context_manager.py → tracer.py} +6 -1
  12. lmnr/opentelemetry_lib/utils/package_check.py +3 -1
  13. lmnr/sdk/browser/browser_use_otel.py +1 -1
  14. lmnr/sdk/browser/pw_utils.py +8 -4
  15. lmnr/sdk/client/asynchronous/resources/agent.py +3 -1
  16. lmnr/sdk/client/synchronous/resources/agent.py +3 -1
  17. lmnr/sdk/decorators.py +4 -2
  18. lmnr/sdk/evaluations.py +3 -3
  19. lmnr/sdk/laminar.py +13 -31
  20. lmnr/sdk/utils.py +2 -3
  21. lmnr/version.py +1 -1
  22. {lmnr-0.5.3.dist-info → lmnr-0.6.0.dist-info}/METADATA +64 -62
  23. {lmnr-0.5.3.dist-info → lmnr-0.6.0.dist-info}/RECORD +26 -27
  24. lmnr/opentelemetry_lib/config/__init__.py +0 -12
  25. lmnr/opentelemetry_lib/decorators/base.py +0 -210
  26. lmnr/opentelemetry_lib/instruments.py +0 -42
  27. lmnr/opentelemetry_lib/tracing/content_allow_list.py +0 -24
  28. lmnr/opentelemetry_lib/tracing/tracing.py +0 -1016
  29. lmnr/opentelemetry_lib/utils/in_memory_span_exporter.py +0 -61
  30. {lmnr-0.5.3.dist-info → lmnr-0.6.0.dist-info}/LICENSE +0 -0
  31. {lmnr-0.5.3.dist-info → lmnr-0.6.0.dist-info}/WHEEL +0 -0
  32. {lmnr-0.5.3.dist-info → lmnr-0.6.0.dist-info}/entry_points.txt +0 -0
@@ -1,1016 +0,0 @@
1
- import atexit
2
- import copy
3
- import logging
4
- import uuid
5
-
6
- from contextvars import Context
7
- from lmnr.sdk.client.asynchronous.async_client import AsyncLaminarClient
8
- from lmnr.sdk.client.synchronous.sync_client import LaminarClient
9
- from lmnr.sdk.log import VerboseColorfulFormatter
10
- from lmnr.opentelemetry_lib.instruments import Instruments
11
- from lmnr.opentelemetry_lib.tracing.attributes import (
12
- ASSOCIATION_PROPERTIES,
13
- SPAN_IDS_PATH,
14
- SPAN_INSTRUMENTATION_SOURCE,
15
- SPAN_SDK_VERSION,
16
- SPAN_LANGUAGE_VERSION,
17
- SPAN_PATH,
18
- TRACING_LEVEL,
19
- )
20
- from lmnr.opentelemetry_lib.tracing.content_allow_list import ContentAllowList
21
- from lmnr.opentelemetry_lib.utils import is_notebook
22
- from lmnr.opentelemetry_lib.utils.package_check import is_package_installed
23
- from opentelemetry import trace
24
- from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
25
- OTLPSpanExporter as HTTPExporter,
26
- )
27
- from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
28
- OTLPSpanExporter as GRPCExporter,
29
- )
30
- from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import Compression
31
- from opentelemetry.instrumentation.threading import ThreadingInstrumentor
32
- from opentelemetry.context import get_value, attach, get_current, set_value
33
- from opentelemetry.propagate import set_global_textmap
34
- from opentelemetry.propagators.textmap import TextMapPropagator
35
- from opentelemetry.sdk.resources import Resource
36
- from opentelemetry.sdk.trace import TracerProvider, SpanProcessor, Span
37
- from opentelemetry.sdk.trace.export import (
38
- SpanExporter,
39
- SimpleSpanProcessor,
40
- BatchSpanProcessor,
41
- )
42
- from opentelemetry.trace import get_tracer_provider, ProxyTracerProvider
43
-
44
- from typing import Dict, Optional, Set
45
-
46
- from lmnr.version import __version__, PYTHON_VERSION
47
-
48
- module_logger = logging.getLogger(__name__)
49
- console_log_handler = logging.StreamHandler()
50
- console_log_handler.setFormatter(VerboseColorfulFormatter())
51
- module_logger.addHandler(console_log_handler)
52
-
53
-
54
- TRACER_NAME = "lmnr.tracer"
55
- EXCLUDED_URLS = """
56
- iam.cloud.ibm.com,
57
- dataplatform.cloud.ibm.com,
58
- ml.cloud.ibm.com,
59
- api.openai.com,
60
- openai.azure.com,
61
- api.anthropic.com,
62
- api.cohere.ai,
63
- pinecone.io,
64
- api.lmnr.ai,
65
- posthog.com,
66
- sentry.io,
67
- bedrock-runtime,
68
- sagemaker-runtime,
69
- googleapis.com,
70
- githubusercontent.com,
71
- openaipublic.blob.core.windows.net"""
72
-
73
- MAX_EVENTS_OR_ATTRIBUTES_PER_SPAN = 5000
74
-
75
-
76
- class TracerWrapper(object):
77
- resource_attributes: dict = {}
78
- enable_content_tracing: bool = True
79
- endpoint: str = None
80
- headers: Dict[str, str] = {}
81
- __tracer_provider: TracerProvider = None
82
- __logger: logging.Logger = None
83
- __span_id_to_path: dict[int, list[str]] = {}
84
- __span_id_lists: dict[int, list[str]] = {}
85
- __client: LaminarClient = None
86
- __async_client: AsyncLaminarClient = None
87
- __spans_processor: SpanProcessor = None
88
- __spans_exporter: SpanExporter = None
89
-
90
- def __new__(
91
- cls,
92
- disable_batch=False,
93
- processor: Optional[SpanProcessor] = None,
94
- propagator: Optional[TextMapPropagator] = None,
95
- exporter: Optional[SpanExporter] = None,
96
- should_enrich_metrics: bool = False,
97
- instruments: Optional[Set[Instruments]] = None,
98
- base_http_url: Optional[str] = None,
99
- project_api_key: Optional[str] = None,
100
- max_export_batch_size: Optional[int] = None,
101
- ) -> "TracerWrapper":
102
- cls._initialize_logger(cls)
103
- if not hasattr(cls, "instance"):
104
- obj = cls.instance = super(TracerWrapper, cls).__new__(cls)
105
- if not TracerWrapper.endpoint:
106
- return obj
107
-
108
- obj.__client = LaminarClient(
109
- base_url=base_http_url,
110
- project_api_key=project_api_key,
111
- )
112
- obj.__async_client = AsyncLaminarClient(
113
- base_url=base_http_url,
114
- project_api_key=project_api_key,
115
- )
116
-
117
- obj.__resource = Resource(attributes=TracerWrapper.resource_attributes)
118
- obj.__tracer_provider = init_tracer_provider(resource=obj.__resource)
119
- if processor:
120
- obj.__spans_processor = processor
121
- obj.__spans_processor_original_on_start = processor.on_start
122
- else:
123
- obj.__spans_exporter = (
124
- exporter
125
- if exporter
126
- else init_spans_exporter(
127
- TracerWrapper.endpoint, TracerWrapper.headers
128
- )
129
- )
130
- if disable_batch or is_notebook():
131
- obj.__spans_processor = SimpleSpanProcessor(obj.__spans_exporter)
132
- else:
133
- obj.__spans_processor = BatchSpanProcessor(
134
- obj.__spans_exporter,
135
- max_export_batch_size=max_export_batch_size,
136
- )
137
- obj.__spans_processor_original_on_start = None
138
-
139
- obj.__spans_processor.on_start = obj._span_processor_on_start
140
- obj.__tracer_provider.add_span_processor(obj.__spans_processor)
141
-
142
- if propagator:
143
- set_global_textmap(propagator)
144
-
145
- # this makes sure otel context is propagated so we always want it
146
- ThreadingInstrumentor().instrument()
147
-
148
- instrument_set = init_instrumentations(
149
- should_enrich_metrics,
150
- instruments,
151
- client=obj.__client,
152
- async_client=obj.__async_client,
153
- )
154
-
155
- if not instrument_set:
156
- cls.__logger.info(
157
- "No instruments set through Laminar. "
158
- "Only enabling basic OpenTelemetry tracing."
159
- )
160
-
161
- obj.__content_allow_list = ContentAllowList()
162
-
163
- # Force flushes for debug environments (e.g. local development)
164
- atexit.register(obj.exit_handler)
165
-
166
- return cls.instance
167
-
168
- def exit_handler(self):
169
- self.__span_id_to_path = {}
170
- self.flush()
171
-
172
- def _initialize_logger(self):
173
- self.__logger = logging.getLogger(__name__)
174
- console_log_handler = logging.StreamHandler()
175
- console_log_handler.setFormatter(VerboseColorfulFormatter())
176
- self.__logger.addHandler(console_log_handler)
177
-
178
- def _span_processor_on_start(
179
- self, span: Span, parent_context: Optional[Context] = None
180
- ):
181
- span_path_in_context = get_value("span_path", parent_context or get_current())
182
- span_path_in_context = None
183
- parent_span_path = span_path_in_context or (
184
- self.__span_id_to_path.get(span.parent.span_id) if span.parent else None
185
- )
186
- parent_span_ids_path = (
187
- self.__span_id_lists.get(span.parent.span_id, []) if span.parent else []
188
- )
189
- span_path = parent_span_path + [span.name] if parent_span_path else [span.name]
190
- span_ids_path = parent_span_ids_path + [
191
- str(uuid.UUID(int=span.get_span_context().span_id))
192
- ]
193
- span.set_attribute(SPAN_PATH, span_path)
194
- span.set_attribute(SPAN_IDS_PATH, span_ids_path)
195
- set_value("span_path", span_path, get_current())
196
- self.__span_id_to_path[span.get_span_context().span_id] = span_path
197
- self.__span_id_lists[span.get_span_context().span_id] = span_ids_path
198
-
199
- span.set_attribute(SPAN_INSTRUMENTATION_SOURCE, "python")
200
- span.set_attribute(SPAN_SDK_VERSION, __version__)
201
- span.set_attribute(SPAN_LANGUAGE_VERSION, f"python@{PYTHON_VERSION}")
202
-
203
- association_properties = get_value("association_properties")
204
- if association_properties is not None:
205
- _set_association_properties_attributes(span, association_properties)
206
-
207
- if not self.enable_content_tracing:
208
- if self.__content_allow_list.is_allowed(association_properties):
209
- attach(set_value("override_enable_content_tracing", True))
210
- else:
211
- attach(set_value("override_enable_content_tracing", False))
212
-
213
- # Call original on_start method if it exists in custom processor
214
- if self.__spans_processor_original_on_start:
215
- self.__spans_processor_original_on_start(span, parent_context)
216
-
217
- @staticmethod
218
- def set_static_params(
219
- resource_attributes: dict,
220
- enable_content_tracing: bool,
221
- endpoint: str,
222
- headers: Dict[str, str],
223
- ) -> None:
224
- TracerWrapper.resource_attributes = resource_attributes
225
- TracerWrapper.enable_content_tracing = enable_content_tracing
226
- TracerWrapper.endpoint = endpoint
227
- TracerWrapper.headers = headers
228
-
229
- @classmethod
230
- def verify_initialized(cls) -> bool:
231
- return hasattr(cls, "instance")
232
-
233
- @classmethod
234
- def clear(cls):
235
- # Any state cleanup. Now used in between tests
236
- cls.__span_id_to_path = {}
237
- cls.__span_id_lists = {}
238
-
239
- def shutdown(self):
240
- self.__tracer_provider.shutdown()
241
-
242
- def flush(self):
243
- return self.__spans_processor.force_flush()
244
-
245
- def get_tracer(self):
246
- return self.__tracer_provider.get_tracer(TRACER_NAME)
247
-
248
-
249
- def set_association_properties(properties: dict) -> None:
250
- attach(set_value("association_properties", properties))
251
-
252
- span = trace.get_current_span()
253
- _set_association_properties_attributes(span, properties)
254
-
255
-
256
- def get_association_properties(context: Optional[Context] = None) -> dict:
257
- return get_value("association_properties", context) or {}
258
-
259
-
260
- def update_association_properties(
261
- properties: dict,
262
- set_on_current_span: bool = True,
263
- context: Optional[Context] = None,
264
- ) -> None:
265
- """Only adds or updates properties that are not already present"""
266
- association_properties = get_value("association_properties", context) or {}
267
- association_properties.update(properties)
268
-
269
- attach(set_value("association_properties", association_properties, context))
270
-
271
- if set_on_current_span:
272
- span = trace.get_current_span()
273
- _set_association_properties_attributes(span, properties)
274
-
275
-
276
- def remove_association_properties(properties: dict) -> None:
277
- props: dict = copy.copy(get_value("association_properties") or {})
278
- for k in properties.keys():
279
- props.pop(k, None)
280
- set_association_properties(props)
281
-
282
-
283
- def _set_association_properties_attributes(span, properties: dict) -> None:
284
- for key, value in properties.items():
285
- if key == TRACING_LEVEL:
286
- span.set_attribute(f"lmnr.internal.{TRACING_LEVEL}", value)
287
- continue
288
- span.set_attribute(f"{ASSOCIATION_PROPERTIES}.{key}", value)
289
-
290
-
291
- def set_managed_prompt_tracing_context(
292
- key: str,
293
- version: int,
294
- version_name: str,
295
- version_hash: str,
296
- template_variables: dict,
297
- ) -> None:
298
- attach(set_value("managed_prompt", True))
299
- attach(set_value("prompt_key", key))
300
- attach(set_value("prompt_version", version))
301
- attach(set_value("prompt_version_name", version_name))
302
- attach(set_value("prompt_version_hash", version_hash))
303
- attach(set_value("prompt_template_variables", template_variables))
304
-
305
-
306
- def init_spans_exporter(api_endpoint: str, headers: Dict[str, str]) -> SpanExporter:
307
- if "http" in api_endpoint.lower() or "https" in api_endpoint.lower():
308
- return HTTPExporter(endpoint=f"{api_endpoint}/v1/traces", headers=headers)
309
- else:
310
- return GRPCExporter(
311
- endpoint=f"{api_endpoint}", headers=headers, compression=Compression.Gzip
312
- )
313
-
314
-
315
- # TODO: check if it's safer to use the default tracer provider obtained from
316
- # get_tracer_provider()
317
- def init_tracer_provider(resource: Resource) -> TracerProvider:
318
- provider: TracerProvider = None
319
- default_provider: TracerProvider = get_tracer_provider()
320
-
321
- if isinstance(default_provider, ProxyTracerProvider):
322
- provider = TracerProvider(resource=resource)
323
- trace.set_tracer_provider(provider)
324
- elif not hasattr(default_provider, "add_span_processor"):
325
- module_logger.error(
326
- "Cannot add span processor to the default provider since it doesn't support it"
327
- )
328
- return
329
- else:
330
- provider = default_provider
331
-
332
- return provider
333
-
334
-
335
- def init_instrumentations(
336
- should_enrich_metrics: bool,
337
- instruments: Optional[Set[Instruments]] = None,
338
- block_instruments: Optional[Set[Instruments]] = None,
339
- client: Optional[LaminarClient] = None,
340
- async_client: Optional[AsyncLaminarClient] = None,
341
- ):
342
- block_instruments = block_instruments or set()
343
- # These libraries are not instrumented by default,
344
- # but if the user wants, they can manually specify them
345
- default_off_instruments = set(
346
- [
347
- Instruments.REQUESTS,
348
- Instruments.URLLIB3,
349
- Instruments.REDIS,
350
- Instruments.PYMYSQL,
351
- ]
352
- )
353
-
354
- instruments = (
355
- instruments
356
- if instruments is not None
357
- else (set(Instruments) - default_off_instruments)
358
- )
359
-
360
- # Remove any instruments that were explicitly blocked
361
- instruments = instruments - block_instruments
362
-
363
- instrument_set = False
364
- for instrument in instruments:
365
- if instrument == Instruments.ALEPHALPHA:
366
- if init_alephalpha_instrumentor():
367
- instrument_set = True
368
- elif instrument == Instruments.ANTHROPIC:
369
- if init_anthropic_instrumentor(should_enrich_metrics):
370
- instrument_set = True
371
- elif instrument == Instruments.BEDROCK:
372
- if init_bedrock_instrumentor(should_enrich_metrics):
373
- instrument_set = True
374
- elif instrument == Instruments.CHROMA:
375
- if init_chroma_instrumentor():
376
- instrument_set = True
377
- elif instrument == Instruments.COHERE:
378
- if init_cohere_instrumentor():
379
- instrument_set = True
380
- elif instrument == Instruments.GOOGLE_GENERATIVEAI:
381
- if init_google_generativeai_instrumentor():
382
- instrument_set = True
383
- elif instrument == Instruments.GOOGLE_GENAI:
384
- if init_google_genai_instrumentor():
385
- instrument_set = True
386
- elif instrument == Instruments.GROQ:
387
- if init_groq_instrumentor():
388
- instrument_set = True
389
- elif instrument == Instruments.HAYSTACK:
390
- if init_haystack_instrumentor():
391
- instrument_set = True
392
- elif instrument == Instruments.LANCEDB:
393
- if init_lancedb_instrumentor():
394
- instrument_set = True
395
- elif instrument == Instruments.LANGCHAIN:
396
- if init_langchain_instrumentor():
397
- instrument_set = True
398
- elif instrument == Instruments.LLAMA_INDEX:
399
- if init_llama_index_instrumentor():
400
- instrument_set = True
401
- elif instrument == Instruments.MARQO:
402
- if init_marqo_instrumentor():
403
- instrument_set = True
404
- elif instrument == Instruments.MILVUS:
405
- if init_milvus_instrumentor():
406
- instrument_set = True
407
- elif instrument == Instruments.MISTRAL:
408
- if init_mistralai_instrumentor():
409
- instrument_set = True
410
- elif instrument == Instruments.OLLAMA:
411
- if init_ollama_instrumentor():
412
- instrument_set = True
413
- elif instrument == Instruments.OPENAI:
414
- if init_openai_instrumentor(should_enrich_metrics):
415
- instrument_set = True
416
- elif instrument == Instruments.PINECONE:
417
- if init_pinecone_instrumentor():
418
- instrument_set = True
419
- elif instrument == Instruments.PYMYSQL:
420
- if init_pymysql_instrumentor():
421
- instrument_set = True
422
- elif instrument == Instruments.QDRANT:
423
- if init_qdrant_instrumentor():
424
- instrument_set = True
425
- elif instrument == Instruments.REDIS:
426
- if init_redis_instrumentor():
427
- instrument_set = True
428
- elif instrument == Instruments.REPLICATE:
429
- if init_replicate_instrumentor():
430
- instrument_set = True
431
- elif instrument == Instruments.REQUESTS:
432
- if init_requests_instrumentor():
433
- instrument_set = True
434
- elif instrument == Instruments.SAGEMAKER:
435
- if init_sagemaker_instrumentor(should_enrich_metrics):
436
- instrument_set = True
437
- elif instrument == Instruments.TOGETHER:
438
- if init_together_instrumentor():
439
- instrument_set = True
440
- elif instrument == Instruments.TRANSFORMERS:
441
- if init_transformers_instrumentor():
442
- instrument_set = True
443
- elif instrument == Instruments.URLLIB3:
444
- if init_urllib3_instrumentor():
445
- instrument_set = True
446
- elif instrument == Instruments.VERTEXAI:
447
- if init_vertexai_instrumentor():
448
- instrument_set = True
449
- elif instrument == Instruments.WATSONX:
450
- if init_watsonx_instrumentor():
451
- instrument_set = True
452
- elif instrument == Instruments.WEAVIATE:
453
- if init_weaviate_instrumentor():
454
- instrument_set = True
455
- elif instrument == Instruments.PLAYWRIGHT:
456
- if init_playwright_instrumentor(client, async_client):
457
- instrument_set = True
458
- elif instrument == Instruments.PATCHRIGHT:
459
- if init_patchright_instrumentor(client, async_client):
460
- instrument_set = True
461
- elif instrument == Instruments.BROWSER_USE:
462
- if init_browser_use_instrumentor():
463
- instrument_set = True
464
- else:
465
- module_logger.warning(
466
- f"Warning: {instrument} instrumentation does not exist."
467
- )
468
- module_logger.warning(
469
- "Usage:\n"
470
- "from lmnr import Laminar, Instruments\n"
471
- "Laminar.init(instruments=set([Instruments.OPENAI]))"
472
- )
473
-
474
- return instrument_set
475
-
476
-
477
- def init_browser_use_instrumentor():
478
- try:
479
- if is_package_installed("browser-use"):
480
- from lmnr.sdk.browser.browser_use_otel import BrowserUseInstrumentor
481
-
482
- instrumentor = BrowserUseInstrumentor()
483
- instrumentor.instrument()
484
- return True
485
- except Exception as e:
486
- module_logger.error(f"Error initializing BrowserUse instrumentor: {e}")
487
- return False
488
-
489
-
490
- def init_playwright_instrumentor(
491
- client: LaminarClient, async_client: AsyncLaminarClient
492
- ):
493
- try:
494
- if is_package_installed("playwright"):
495
- from lmnr.sdk.browser.playwright_otel import PlaywrightInstrumentor
496
-
497
- instrumentor = PlaywrightInstrumentor(client, async_client)
498
- instrumentor.instrument()
499
- return True
500
- except Exception as e:
501
- module_logger.error(f"Error initializing Playwright instrumentor: {e}")
502
- return False
503
-
504
-
505
- def init_patchright_instrumentor(
506
- client: LaminarClient, async_client: AsyncLaminarClient
507
- ):
508
- try:
509
- if is_package_installed("patchright"):
510
- from lmnr.sdk.browser.patchright_otel import PatchrightInstrumentor
511
-
512
- instrumentor = PatchrightInstrumentor(client, async_client)
513
- instrumentor.instrument()
514
- return True
515
- except Exception as e:
516
- module_logger.error(f"Error initializing patchright instrumentor: {e}")
517
- return False
518
-
519
-
520
- def init_openai_instrumentor(should_enrich_metrics: bool):
521
- try:
522
- if is_package_installed("openai") and is_package_installed(
523
- "opentelemetry-instrumentation-openai"
524
- ):
525
- from opentelemetry.instrumentation.openai import OpenAIInstrumentor
526
-
527
- instrumentor = OpenAIInstrumentor(
528
- enrich_assistant=should_enrich_metrics,
529
- enrich_token_usage=should_enrich_metrics,
530
- # Default in the package provided is an empty function, which
531
- # results in dropping the image data if we don't explicitly
532
- # set it to None.
533
- upload_base64_image=None,
534
- )
535
- if not instrumentor.is_instrumented_by_opentelemetry:
536
- instrumentor.instrument()
537
- return True
538
-
539
- except Exception as e:
540
- module_logger.error(f"Error initializing OpenAI instrumentor: {e}")
541
- return False
542
-
543
-
544
- def init_anthropic_instrumentor(should_enrich_metrics: bool):
545
- try:
546
- if is_package_installed("anthropic") and is_package_installed(
547
- "opentelemetry-instrumentation-anthropic"
548
- ):
549
- from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
550
-
551
- instrumentor = AnthropicInstrumentor(
552
- enrich_token_usage=should_enrich_metrics,
553
- upload_base64_image=None,
554
- )
555
- if not instrumentor.is_instrumented_by_opentelemetry:
556
- instrumentor.instrument()
557
- return True
558
- except Exception as e:
559
- module_logger.error(f"Error initializing Anthropic instrumentor: {e}")
560
- return False
561
-
562
-
563
- def init_cohere_instrumentor():
564
- try:
565
- if is_package_installed("cohere") and is_package_installed(
566
- "opentelemetry-instrumentation-cohere"
567
- ):
568
- from opentelemetry.instrumentation.cohere import CohereInstrumentor
569
-
570
- instrumentor = CohereInstrumentor()
571
- if not instrumentor.is_instrumented_by_opentelemetry:
572
- instrumentor.instrument()
573
- return True
574
- except Exception as e:
575
- module_logger.error(f"Error initializing Cohere instrumentor: {e}")
576
- return False
577
-
578
-
579
- def init_pinecone_instrumentor():
580
- try:
581
- if is_package_installed("pinecone") and is_package_installed(
582
- "opentelemetry-instrumentation-pinecone"
583
- ):
584
- from opentelemetry.instrumentation.pinecone import PineconeInstrumentor
585
-
586
- instrumentor = PineconeInstrumentor()
587
- if not instrumentor.is_instrumented_by_opentelemetry:
588
- instrumentor.instrument()
589
- return True
590
- except Exception as e:
591
- module_logger.error(f"Error initializing Pinecone instrumentor: {e}")
592
- return False
593
-
594
-
595
- def init_qdrant_instrumentor():
596
- try:
597
- if is_package_installed("qdrant_client") and is_package_installed(
598
- "opentelemetry-instrumentation-qdrant"
599
- ):
600
- from opentelemetry.instrumentation.qdrant import QdrantInstrumentor
601
-
602
- instrumentor = QdrantInstrumentor()
603
- if not instrumentor.is_instrumented_by_opentelemetry:
604
- instrumentor.instrument()
605
- except Exception as e:
606
- module_logger.error(f"Error initializing Qdrant instrumentor: {e}")
607
- return False
608
-
609
-
610
- def init_chroma_instrumentor():
611
- try:
612
- if is_package_installed("chromadb") and is_package_installed(
613
- "opentelemetry-instrumentation-chromadb"
614
- ):
615
- from opentelemetry.instrumentation.chromadb import ChromaInstrumentor
616
-
617
- instrumentor = ChromaInstrumentor()
618
- if not instrumentor.is_instrumented_by_opentelemetry:
619
- instrumentor.instrument()
620
- return True
621
- except Exception as e:
622
- module_logger.error(f"Error initializing Chroma instrumentor: {e}")
623
- return False
624
-
625
-
626
- def init_google_generativeai_instrumentor():
627
- try:
628
- if is_package_installed("google-generativeai") and is_package_installed(
629
- "opentelemetry-instrumentation-google-generativeai"
630
- ):
631
- from opentelemetry.instrumentation.google_generativeai import (
632
- GoogleGenerativeAiInstrumentor,
633
- )
634
-
635
- instrumentor = GoogleGenerativeAiInstrumentor()
636
- if not instrumentor.is_instrumented_by_opentelemetry:
637
- instrumentor.instrument()
638
- return True
639
- except Exception as e:
640
- module_logger.error(f"Error initializing Gemini instrumentor: {e}")
641
- return False
642
-
643
-
644
- def init_google_genai_instrumentor():
645
- try:
646
- if is_package_installed("google-genai"):
647
- # TODO: uncomment this once we migrate to the contrib package
648
- # and is_package_installed(
649
- # "opentelemetry-instrumentation-google-genai"
650
- # ):
651
- # from opentelemetry.instrumentation.google_genai import (
652
- from ..opentelemetry.instrumentation.google_genai import (
653
- GoogleGenAiSdkInstrumentor,
654
- )
655
-
656
- instrumentor = GoogleGenAiSdkInstrumentor()
657
- if not instrumentor.is_instrumented_by_opentelemetry:
658
- instrumentor.instrument()
659
- return True
660
- except Exception as e:
661
- module_logger.error(f"Error initializing Google GenAI instrumentor: {e}")
662
- return False
663
-
664
-
665
- def init_haystack_instrumentor():
666
- try:
667
- if is_package_installed("haystack") and is_package_installed(
668
- "opentelemetry-instrumentation-haystack"
669
- ):
670
- from opentelemetry.instrumentation.haystack import HaystackInstrumentor
671
-
672
- instrumentor = HaystackInstrumentor()
673
- if not instrumentor.is_instrumented_by_opentelemetry:
674
- instrumentor.instrument()
675
- return True
676
- except Exception as e:
677
- module_logger.error(f"Error initializing Haystack instrumentor: {e}")
678
- return False
679
-
680
-
681
- def init_langchain_instrumentor():
682
- try:
683
- if is_package_installed("langchain") and is_package_installed(
684
- "opentelemetry-instrumentation-langchain"
685
- ):
686
- from opentelemetry.instrumentation.langchain import LangchainInstrumentor
687
-
688
- instrumentor = LangchainInstrumentor()
689
- if not instrumentor.is_instrumented_by_opentelemetry:
690
- instrumentor.instrument()
691
- return True
692
- except Exception as e:
693
- # FIXME: silencing this error temporarily, it appears to not be critical
694
- if str(e) != "No module named 'langchain_community'":
695
- module_logger.error(f"Error initializing LangChain instrumentor: {e}")
696
- return False
697
-
698
-
699
- def init_mistralai_instrumentor():
700
- try:
701
- if is_package_installed("mistralai") and is_package_installed(
702
- "opentelemetry-instrumentation-mistralai"
703
- ):
704
- from opentelemetry.instrumentation.mistralai import MistralAiInstrumentor
705
-
706
- instrumentor = MistralAiInstrumentor()
707
- if not instrumentor.is_instrumented_by_opentelemetry:
708
- instrumentor.instrument()
709
- return True
710
- except Exception as e:
711
- module_logger.error(f"Error initializing MistralAI instrumentor: {e}")
712
- return False
713
-
714
-
715
- def init_ollama_instrumentor():
716
- try:
717
- if is_package_installed("ollama") and is_package_installed(
718
- "opentelemetry-instrumentation-ollama"
719
- ):
720
- from opentelemetry.instrumentation.ollama import OllamaInstrumentor
721
-
722
- instrumentor = OllamaInstrumentor()
723
- if not instrumentor.is_instrumented_by_opentelemetry:
724
- instrumentor.instrument()
725
- return True
726
- except Exception as e:
727
- module_logger.error(f"Error initializing Ollama instrumentor: {e}")
728
- return False
729
-
730
-
731
- def init_transformers_instrumentor():
732
- try:
733
- if is_package_installed("transformers") and is_package_installed(
734
- "opentelemetry-instrumentation-transformers"
735
- ):
736
- from opentelemetry.instrumentation.transformers import (
737
- TransformersInstrumentor,
738
- )
739
-
740
- instrumentor = TransformersInstrumentor()
741
- if not instrumentor.is_instrumented_by_opentelemetry:
742
- instrumentor.instrument()
743
- return True
744
- except Exception as e:
745
- module_logger.error(f"Error initializing Transformers instrumentor: {e}")
746
- return False
747
-
748
-
749
- def init_together_instrumentor():
750
- try:
751
- if is_package_installed("together") and is_package_installed(
752
- "opentelemetry-instrumentation-together"
753
- ):
754
- from opentelemetry.instrumentation.together import TogetherAiInstrumentor
755
-
756
- instrumentor = TogetherAiInstrumentor()
757
- if not instrumentor.is_instrumented_by_opentelemetry:
758
- instrumentor.instrument()
759
- return True
760
- except Exception as e:
761
- module_logger.error(f"Error initializing TogetherAI instrumentor: {e}")
762
- return False
763
-
764
-
765
- def init_llama_index_instrumentor():
766
- try:
767
- if (
768
- is_package_installed("llama-index") or is_package_installed("llama_index")
769
- ) and is_package_installed("opentelemetry-instrumentation-llamaindex"):
770
- from opentelemetry.instrumentation.llamaindex import LlamaIndexInstrumentor
771
-
772
- instrumentor = LlamaIndexInstrumentor()
773
- if not instrumentor.is_instrumented_by_opentelemetry:
774
- instrumentor.instrument()
775
- return True
776
- except Exception as e:
777
- module_logger.error(f"Error initializing LlamaIndex instrumentor: {e}")
778
- return False
779
-
780
-
781
- def init_milvus_instrumentor():
782
- try:
783
- if is_package_installed("pymilvus") and is_package_installed(
784
- "opentelemetry-instrumentation-milvus"
785
- ):
786
- from opentelemetry.instrumentation.milvus import MilvusInstrumentor
787
-
788
- instrumentor = MilvusInstrumentor()
789
- if not instrumentor.is_instrumented_by_opentelemetry:
790
- instrumentor.instrument()
791
- return True
792
- except Exception as e:
793
- module_logger.error(f"Error initializing Milvus instrumentor: {e}")
794
- return False
795
-
796
-
797
- def init_requests_instrumentor():
798
- try:
799
- if is_package_installed("requests"):
800
- from opentelemetry.instrumentation.requests import RequestsInstrumentor
801
-
802
- instrumentor = RequestsInstrumentor()
803
- if not instrumentor.is_instrumented_by_opentelemetry:
804
- instrumentor.instrument(excluded_urls=EXCLUDED_URLS)
805
- return True
806
- except Exception as e:
807
- module_logger.error(f"Error initializing Requests instrumentor: {e}")
808
- return False
809
-
810
-
811
- def init_urllib3_instrumentor():
812
- try:
813
- if is_package_installed("urllib3"):
814
- from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor
815
-
816
- instrumentor = URLLib3Instrumentor()
817
- if not instrumentor.is_instrumented_by_opentelemetry:
818
- instrumentor.instrument(excluded_urls=EXCLUDED_URLS)
819
- return True
820
- except Exception as e:
821
- module_logger.error(f"Error initializing urllib3 instrumentor: {e}")
822
- return False
823
-
824
-
825
- def init_pymysql_instrumentor():
826
- try:
827
- if is_package_installed("sqlalchemy"):
828
- from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
829
-
830
- instrumentor = SQLAlchemyInstrumentor()
831
- if not instrumentor.is_instrumented_by_opentelemetry:
832
- instrumentor.instrument()
833
- return True
834
- except Exception as e:
835
- module_logger.error(f"Error initializing SQLAlchemy instrumentor: {e}")
836
- return False
837
-
838
-
839
- def init_bedrock_instrumentor(should_enrich_metrics: bool):
840
- try:
841
- if is_package_installed("boto3") and is_package_installed(
842
- "opentelemetry-instrumentation-bedrock"
843
- ):
844
- from opentelemetry.instrumentation.bedrock import BedrockInstrumentor
845
-
846
- instrumentor = BedrockInstrumentor(
847
- enrich_token_usage=should_enrich_metrics,
848
- )
849
- if not instrumentor.is_instrumented_by_opentelemetry:
850
- instrumentor.instrument()
851
- return True
852
- except Exception as e:
853
- module_logger.error(f"Error initializing Bedrock instrumentor: {e}")
854
- return False
855
-
856
-
857
- def init_replicate_instrumentor():
858
- try:
859
- if is_package_installed("replicate") and is_package_installed(
860
- "opentelemetry-instrumentation-replicate"
861
- ):
862
- from opentelemetry.instrumentation.replicate import ReplicateInstrumentor
863
-
864
- instrumentor = ReplicateInstrumentor()
865
- if not instrumentor.is_instrumented_by_opentelemetry:
866
- instrumentor.instrument()
867
- return True
868
- except Exception as e:
869
- module_logger.error(f"Error initializing Replicate instrumentor: {e}")
870
- return False
871
-
872
-
873
- def init_vertexai_instrumentor():
874
- try:
875
- if is_package_installed("vertexai") and is_package_installed(
876
- "opentelemetry-instrumentation-vertexai"
877
- ):
878
- from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
879
-
880
- instrumentor = VertexAIInstrumentor()
881
- if not instrumentor.is_instrumented_by_opentelemetry:
882
- instrumentor.instrument()
883
- return True
884
- except Exception as e:
885
- module_logger.warning(f"Error initializing Vertex AI instrumentor: {e}")
886
- return False
887
-
888
-
889
- def init_watsonx_instrumentor():
890
- try:
891
- if (
892
- is_package_installed("ibm-watsonx-ai")
893
- or is_package_installed("ibm-watson-machine-learning")
894
- ) and is_package_installed("opentelemetry-instrumentation-watsonx"):
895
- from opentelemetry.instrumentation.watsonx import WatsonxInstrumentor
896
-
897
- instrumentor = WatsonxInstrumentor()
898
- if not instrumentor.is_instrumented_by_opentelemetry:
899
- instrumentor.instrument()
900
- return True
901
- except Exception as e:
902
- module_logger.warning(f"Error initializing Watsonx instrumentor: {e}")
903
- return False
904
-
905
-
906
- def init_weaviate_instrumentor():
907
- try:
908
- if is_package_installed("weaviate") and is_package_installed(
909
- "opentelemetry-instrumentation-weaviate"
910
- ):
911
- from opentelemetry.instrumentation.weaviate import WeaviateInstrumentor
912
-
913
- instrumentor = WeaviateInstrumentor()
914
- if not instrumentor.is_instrumented_by_opentelemetry:
915
- instrumentor.instrument()
916
- return True
917
- except Exception as e:
918
- module_logger.warning(f"Error initializing Weaviate instrumentor: {e}")
919
- return False
920
-
921
-
922
- def init_alephalpha_instrumentor():
923
- try:
924
- if is_package_installed("aleph_alpha_client") and is_package_installed(
925
- "opentelemetry-instrumentation-alephalpha"
926
- ):
927
- from opentelemetry.instrumentation.alephalpha import AlephAlphaInstrumentor
928
-
929
- instrumentor = AlephAlphaInstrumentor()
930
- if not instrumentor.is_instrumented_by_opentelemetry:
931
- instrumentor.instrument()
932
- return True
933
- except Exception as e:
934
- module_logger.error(f"Error initializing Aleph Alpha instrumentor: {e}")
935
- return False
936
-
937
-
938
- def init_marqo_instrumentor():
939
- try:
940
- if is_package_installed("marqo") and is_package_installed(
941
- "opentelemetry-instrumentation-marqo"
942
- ):
943
- from opentelemetry.instrumentation.marqo import MarqoInstrumentor
944
-
945
- instrumentor = MarqoInstrumentor()
946
- if not instrumentor.is_instrumented_by_opentelemetry:
947
- instrumentor.instrument()
948
- return True
949
- except Exception as e:
950
- module_logger.error(f"Error initializing marqo instrumentor: {e}")
951
- return False
952
-
953
-
954
- def init_lancedb_instrumentor():
955
- try:
956
- if is_package_installed("lancedb") and is_package_installed(
957
- "opentelemetry-instrumentation-lancedb"
958
- ):
959
- from opentelemetry.instrumentation.lancedb import LanceInstrumentor
960
-
961
- instrumentor = LanceInstrumentor()
962
- if not instrumentor.is_instrumented_by_opentelemetry:
963
- instrumentor.instrument()
964
- return True
965
- except Exception as e:
966
- module_logger.error(f"Error initializing LanceDB instrumentor: {e}")
967
-
968
-
969
- def init_redis_instrumentor():
970
- try:
971
- if is_package_installed("redis") and is_package_installed(
972
- "opentelemetry-instrumentation-redis"
973
- ):
974
- from opentelemetry.instrumentation.redis import RedisInstrumentor
975
-
976
- instrumentor = RedisInstrumentor()
977
- if not instrumentor.is_instrumented_by_opentelemetry:
978
- instrumentor.instrument(excluded_urls=EXCLUDED_URLS)
979
- return True
980
- except Exception as e:
981
- module_logger.error(f"Error initializing redis instrumentor: {e}")
982
- return False
983
-
984
-
985
- def init_groq_instrumentor():
986
- try:
987
- if is_package_installed("groq") and is_package_installed(
988
- "opentelemetry-instrumentation-groq"
989
- ):
990
- from opentelemetry.instrumentation.groq import GroqInstrumentor
991
-
992
- instrumentor = GroqInstrumentor()
993
- if not instrumentor.is_instrumented_by_opentelemetry:
994
- instrumentor.instrument()
995
- return True
996
- except Exception as e:
997
- module_logger.error(f"Error initializing Groq instrumentor: {e}")
998
- return False
999
-
1000
-
1001
- def init_sagemaker_instrumentor(should_enrich_metrics: bool):
1002
- try:
1003
- if is_package_installed("boto3") and is_package_installed(
1004
- "opentelemetry-instrumentation-sagemaker"
1005
- ):
1006
- from opentelemetry.instrumentation.sagemaker import SageMakerInstrumentor
1007
-
1008
- instrumentor = SageMakerInstrumentor(
1009
- enrich_token_usage=should_enrich_metrics,
1010
- )
1011
- if not instrumentor.is_instrumented_by_opentelemetry:
1012
- instrumentor.instrument()
1013
- return True
1014
- except Exception as e:
1015
- module_logger.error(f"Error initializing SageMaker instrumentor: {e}")
1016
- return False