openlit 0.0.1__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.
@@ -0,0 +1,397 @@
1
+ # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument
2
+ """
3
+ Module for monitoring Cohere API calls.
4
+ """
5
+
6
+ import logging
7
+ from opentelemetry.trace import SpanKind, Status, StatusCode
8
+ from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
9
+ from openlit.__helpers import get_chat_model_cost, get_embed_model_cost, handle_exception
10
+ from openlit.semcov import SemanticConvetion
11
+
12
+ # Initialize logger for logging potential issues and operations
13
+ logger = logging.getLogger(__name__)
14
+
15
+ def embed(gen_ai_endpoint, version, environment, application_name, tracer,
16
+ pricing_info, trace_content, metrics, disable_metrics):
17
+ """
18
+ Generates a telemetry wrapper for embeddings to collect metrics.
19
+
20
+ Args:
21
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
22
+ version: Version of the monitoring package.
23
+ environment: Deployment environment (e.g., production, staging).
24
+ application_name: Name of the application using the OpenAI API.
25
+ tracer: OpenTelemetry tracer for creating spans.
26
+ pricing_info: Information used for calculating the cost of OpenAI usage.
27
+ trace_content: Flag indicating whether to trace the actual content.
28
+
29
+ Returns:
30
+ A function that wraps the embeddings method to add telemetry.
31
+ """
32
+
33
+ def wrapper(wrapped, instance, args, kwargs):
34
+ """
35
+ Wraps the 'embed' API call to add telemetry.
36
+
37
+ This collects metrics such as execution time, cost, and token usage, and handles errors
38
+ gracefully, adding details to the trace for observability.
39
+
40
+ Args:
41
+ wrapped: The original 'embed' method to be wrapped.
42
+ instance: The instance of the class where the original method is defined.
43
+ args: Positional arguments for the 'embed' method.
44
+ kwargs: Keyword arguments for the 'embed' method.
45
+
46
+ Returns:
47
+ The response from the original 'embed' method.
48
+ """
49
+
50
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
51
+ response = wrapped(*args, **kwargs)
52
+
53
+ try:
54
+ # Get prompt from kwargs and store as a single string
55
+ prompt = " ".join(kwargs.get("texts", []))
56
+
57
+
58
+ # Calculate cost of the operation
59
+ cost = get_embed_model_cost(kwargs.get("model", "embed-english-v2.0"),
60
+ pricing_info,
61
+ response.meta.billed_units.input_tokens)
62
+
63
+ # Set Span attributes
64
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
65
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
66
+ SemanticConvetion.GEN_AI_SYSTEM_COHERE)
67
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
68
+ SemanticConvetion.GEN_AI_TYPE_EMBEDDING)
69
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
70
+ gen_ai_endpoint)
71
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
72
+ environment)
73
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
74
+ application_name)
75
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
76
+ kwargs.get("model", "embed-english-v2.0"))
77
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_EMBEDDING_FORMAT,
78
+ kwargs.get("embedding_types", "float"))
79
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_EMBEDDING_DIMENSION,
80
+ kwargs.get("input_type", ""))
81
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
82
+ kwargs.get("user", ""))
83
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
84
+ response.id)
85
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
86
+ response.meta.billed_units.input_tokens)
87
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
88
+ response.meta.billed_units.input_tokens)
89
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
90
+ cost)
91
+ if trace_content:
92
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
93
+ prompt)
94
+
95
+ span.set_status(Status(StatusCode.OK))
96
+
97
+ if disable_metrics is False:
98
+ attributes = {
99
+ TELEMETRY_SDK_NAME:
100
+ "openlit",
101
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
102
+ application_name,
103
+ SemanticConvetion.GEN_AI_SYSTEM:
104
+ SemanticConvetion.GEN_AI_SYSTEM_COHERE,
105
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
106
+ environment,
107
+ SemanticConvetion.GEN_AI_TYPE:
108
+ SemanticConvetion.GEN_AI_TYPE_EMBEDDING,
109
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
110
+ kwargs.get("model", "embed-english-v2.0")
111
+ }
112
+
113
+ metrics["genai_requests"].add(1, attributes)
114
+ metrics["genai_total_tokens"].add(
115
+ response.meta.billed_units.input_tokens, attributes
116
+ )
117
+ metrics["genai_prompt_tokens"].add(
118
+ response.meta.billed_units.input_tokens, attributes
119
+ )
120
+ metrics["genai_cost"].record(cost, attributes)
121
+
122
+ # Return original response
123
+ return response
124
+
125
+ except Exception as e:
126
+ handle_exception(span, e)
127
+ logger.error("Error in trace creation: %s", e)
128
+
129
+ # Return original response
130
+ return response
131
+
132
+ return wrapper
133
+
134
+ def chat(gen_ai_endpoint, version, environment, application_name, tracer,
135
+ pricing_info, trace_content, metrics, disable_metrics):
136
+ """
137
+ Generates a telemetry wrapper for chat to collect metrics.
138
+
139
+ Args:
140
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
141
+ version: Version of the monitoring package.
142
+ environment: Deployment environment (e.g., production, staging).
143
+ application_name: Name of the application using the OpenAI API.
144
+ tracer: OpenTelemetry tracer for creating spans.
145
+ pricing_info: Information used for calculating the cost of OpenAI usage.
146
+ trace_content: Flag indicating whether to trace the actual content.
147
+
148
+ Returns:
149
+ A function that wraps the chat method to add telemetry.
150
+ """
151
+
152
+ def wrapper(wrapped, instance, args, kwargs):
153
+ """
154
+ Wraps the 'chat' API call to add telemetry.
155
+
156
+ This collects metrics such as execution time, cost, and token usage, and handles errors
157
+ gracefully, adding details to the trace for observability.
158
+
159
+ Args:
160
+ wrapped: The original 'chat' method to be wrapped.
161
+ instance: The instance of the class where the original method is defined.
162
+ args: Positional arguments for the 'chat' method.
163
+ kwargs: Keyword arguments for the 'chat' method.
164
+
165
+ Returns:
166
+ The response from the original 'chat' method.
167
+ """
168
+
169
+ with tracer.start_as_current_span(gen_ai_endpoint, kind=SpanKind.CLIENT) as span:
170
+ response = wrapped(*args, **kwargs)
171
+
172
+ try:
173
+ # Calculate cost of the operation
174
+ cost = get_chat_model_cost(kwargs.get("model", "command"),
175
+ pricing_info,
176
+ response.meta["billed_units"]["input_tokens"],
177
+ response.meta["billed_units"]["output_tokens"])
178
+
179
+ # Set Span attributes
180
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
181
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
182
+ SemanticConvetion.GEN_AI_SYSTEM_COHERE)
183
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
184
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
185
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
186
+ gen_ai_endpoint)
187
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
188
+ environment)
189
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
190
+ application_name)
191
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
192
+ kwargs.get("model", "command"))
193
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
194
+ kwargs.get("temperature", 0.3))
195
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
196
+ kwargs.get("max_tokens", ""))
197
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
198
+ kwargs.get("seed", ""))
199
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
200
+ kwargs.get("frequency_penalty", 0.0))
201
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
202
+ kwargs.get("presence_penalty", 0.0))
203
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
204
+ False)
205
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
206
+ response.response_id)
207
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
208
+ response.response_id)
209
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
210
+ response.meta["billed_units"]["input_tokens"])
211
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
212
+ response.meta["billed_units"]["output_tokens"])
213
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
214
+ response.meta["billed_units"]["input_tokens"] +
215
+ response.meta["billed_units"]["output_tokens"])
216
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
217
+ cost)
218
+ if trace_content:
219
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
220
+ kwargs.get("message", ""))
221
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
222
+ response.text)
223
+
224
+ span.set_status(Status(StatusCode.OK))
225
+
226
+ if disable_metrics is False:
227
+ attributes = {
228
+ TELEMETRY_SDK_NAME:
229
+ "openlit",
230
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
231
+ application_name,
232
+ SemanticConvetion.GEN_AI_SYSTEM:
233
+ SemanticConvetion.GEN_AI_SYSTEM_COHERE,
234
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
235
+ environment,
236
+ SemanticConvetion.GEN_AI_TYPE:
237
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
238
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
239
+ kwargs.get("model", "command")
240
+ }
241
+
242
+ metrics["genai_requests"].add(1, attributes)
243
+ metrics["genai_total_tokens"].add(
244
+ response.meta["billed_units"]["input_tokens"] +
245
+ response.meta["billed_units"]["output_tokens"], attributes)
246
+ metrics["genai_completion_tokens"].add(
247
+ response.meta["billed_units"]["output_tokens"], attributes)
248
+ metrics["genai_prompt_tokens"].add(
249
+ response.meta["billed_units"]["input_tokens"], attributes)
250
+ metrics["genai_cost"].record(cost, attributes)
251
+
252
+ # Return original response
253
+ return response
254
+
255
+ except Exception as e:
256
+ handle_exception(span, e)
257
+ logger.error("Error in trace creation: %s", e)
258
+
259
+ # Return original response
260
+ return response
261
+
262
+ return wrapper
263
+
264
+ def chat_stream(gen_ai_endpoint, version, environment, application_name,
265
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
266
+ """
267
+ Generates a telemetry wrapper for chat_stream to collect metrics.
268
+
269
+ Args:
270
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
271
+ version: Version of the monitoring package.
272
+ environment: Deployment environment (e.g., production, staging).
273
+ application_name: Name of the application using the OpenAI API.
274
+ tracer: OpenTelemetry tracer for creating spans.
275
+ pricing_info: Information used for calculating the cost of OpenAI usage.
276
+ trace_content: Flag indicating whether to trace the actual content.
277
+
278
+ Returns:
279
+ A function that wraps the chat method to add telemetry.
280
+ """
281
+
282
+ def wrapper(wrapped, instance, args, kwargs):
283
+ """
284
+ Wraps the 'chat_stream' API call to add telemetry.
285
+
286
+ This collects metrics such as execution time, cost, and token usage, and handles errors
287
+ gracefully, adding details to the trace for observability.
288
+
289
+ Args:
290
+ wrapped: The original 'chat_stream' method to be wrapped.
291
+ instance: The instance of the class where the original method is defined.
292
+ args: Positional arguments for the 'chat_stream' method.
293
+ kwargs: Keyword arguments for the 'chat_stream' method.
294
+
295
+ Returns:
296
+ The response from the original 'chat_stream' method.
297
+ """
298
+
299
+ def stream_generator():
300
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
301
+ # Placeholder for aggregating streaming response
302
+ llmresponse = ""
303
+
304
+ # Loop through streaming events capturing relevant details
305
+ for event in wrapped(*args, **kwargs):
306
+ # Collect message IDs and aggregated response from events
307
+ if event.event_type == "stream-end":
308
+ llmresponse = event.response.text
309
+ response_id = event.response.response_id
310
+ prompt_tokens = event.response.meta["billed_units"]["input_tokens"]
311
+ completion_tokens = event.response.meta["billed_units"]["output_tokens"]
312
+ finish_reason = event.finish_reason
313
+ yield event
314
+
315
+ # Handling exception ensure observability without disrupting operation
316
+ try:
317
+ # Calculate cost of the operation
318
+ cost = get_chat_model_cost(kwargs.get("model", "command"),
319
+ pricing_info, prompt_tokens, completion_tokens)
320
+
321
+ # Set Span attributes
322
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
323
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
324
+ SemanticConvetion.GEN_AI_SYSTEM_COHERE)
325
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
326
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
327
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
328
+ gen_ai_endpoint)
329
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
330
+ environment)
331
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
332
+ application_name)
333
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
334
+ kwargs.get("model", "command"))
335
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
336
+ kwargs.get("temperature", 0.3))
337
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
338
+ kwargs.get("max_tokens", ""))
339
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
340
+ kwargs.get("seed", ""))
341
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
342
+ kwargs.get("frequency_penalty", 0.0))
343
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
344
+ kwargs.get("presence_penalty", 0.0))
345
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
346
+ True)
347
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
348
+ response_id)
349
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
350
+ finish_reason)
351
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
352
+ prompt_tokens)
353
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
354
+ completion_tokens)
355
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
356
+ prompt_tokens + completion_tokens)
357
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
358
+ cost)
359
+ if trace_content:
360
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
361
+ kwargs.get("message", ""))
362
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
363
+ llmresponse)
364
+
365
+ span.set_status(Status(StatusCode.OK))
366
+
367
+ if disable_metrics is False:
368
+ attributes = {
369
+ TELEMETRY_SDK_NAME:
370
+ "openlit",
371
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
372
+ application_name,
373
+ SemanticConvetion.GEN_AI_SYSTEM:
374
+ SemanticConvetion.GEN_AI_SYSTEM_COHERE,
375
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
376
+ environment,
377
+ SemanticConvetion.GEN_AI_TYPE:
378
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
379
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
380
+ kwargs.get("model", "command")
381
+ }
382
+
383
+ metrics["genai_requests"].add(1, attributes)
384
+ metrics["genai_total_tokens"].add(
385
+ prompt_tokens + completion_tokens, attributes
386
+ )
387
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
388
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
389
+ metrics["genai_cost"].record(cost, attributes)
390
+
391
+ except Exception as e:
392
+ handle_exception(span, e)
393
+ logger.error("Error in trace creation: %s", e)
394
+
395
+ return stream_generator()
396
+
397
+ return wrapper
@@ -0,0 +1,74 @@
1
+ # pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
2
+ """Initializer of Auto Instrumentation of LangChain Functions"""
3
+ from typing import Collection
4
+ import importlib.metadata
5
+ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
6
+ from wrapt import wrap_function_wrapper
7
+
8
+ from openlit.instrumentation.langchain.langchain import general_wrap, hub
9
+
10
+ _instruments = ("langchain >= 0.1.1", "langchain-openai >= 0.1.1",
11
+ "langchain-core > 0.1.1", "langchain-community >= 0.0.31")
12
+
13
+ WRAPPED_METHODS = [
14
+ {
15
+ "package": "langchain_community.document_loaders.base",
16
+ "object": "BaseLoader.load",
17
+ "endpoint": "langchain.retrieve.load",
18
+ "wrapper": general_wrap,
19
+ },
20
+ {
21
+ "package": "langchain_community.document_loaders.base",
22
+ "object": "BaseLoader.aload",
23
+ "endpoint": "langchain.retrieve.load",
24
+ "wrapper": general_wrap,
25
+ },
26
+ {
27
+ "package": "langchain_text_splitters.base",
28
+ "object": "TextSplitter.split_documents",
29
+ "endpoint": "langchain.retrieve.split_documents",
30
+ "wrapper": general_wrap,
31
+ },
32
+ {
33
+ "package": "langchain_text_splitters.base",
34
+ "object": "TextSplitter.create_documents",
35
+ "endpoint": "langchain.retrieve.create_documents",
36
+ "wrapper": general_wrap,
37
+ },
38
+ {
39
+ "package": "langchain.hub",
40
+ "object": "pull",
41
+ "endpoint": "langchain.retrieve.prompt",
42
+ "wrapper": hub,
43
+ },
44
+ ]
45
+
46
+ class LangChainInstrumentor(BaseInstrumentor):
47
+ """An instrumentor for Cohere's client library."""
48
+
49
+ def instrumentation_dependencies(self) -> Collection[str]:
50
+ return _instruments
51
+
52
+ def _instrument(self, **kwargs):
53
+ application_name = kwargs.get("application_name")
54
+ environment = kwargs.get("environment")
55
+ tracer = kwargs.get("tracer")
56
+ pricing_info = kwargs.get("pricing_info")
57
+ trace_content = kwargs.get("trace_content")
58
+ version = importlib.metadata.version("langchain")
59
+
60
+ for wrapped_method in WRAPPED_METHODS:
61
+ wrap_package = wrapped_method.get("package")
62
+ wrap_object = wrapped_method.get("object")
63
+ gen_ai_endpoint = wrapped_method.get("endpoint")
64
+ wrapper = wrapped_method.get("wrapper")
65
+ wrap_function_wrapper(
66
+ wrap_package,
67
+ wrap_object,
68
+ wrapper(gen_ai_endpoint, version, environment, application_name,
69
+ tracer, pricing_info, trace_content),
70
+ )
71
+
72
+ @staticmethod
73
+ def _uninstrument(self, **kwargs):
74
+ pass
@@ -0,0 +1,161 @@
1
+ # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument
2
+ """
3
+ Module for monitoring Langchain applications.
4
+ """
5
+
6
+ import logging
7
+ from opentelemetry.trace import SpanKind, Status, StatusCode
8
+ from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
9
+ from openlit.__helpers import handle_exception
10
+ from openlit.semcov import SemanticConvetion
11
+
12
+ # Initialize logger for logging potential issues and operations
13
+ logger = logging.getLogger(__name__)
14
+
15
+ def general_wrap(gen_ai_endpoint, version, environment, application_name,
16
+ tracer, pricing_info, trace_content):
17
+ """
18
+ Creates a wrapper around a function call to trace and log its execution metrics.
19
+
20
+ This function wraps any given function to measure its execution time,
21
+ log its operation, and trace its execution using OpenTelemetry.
22
+
23
+ Parameters:
24
+ - gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.
25
+ - version (str): The version of the Langchain application.
26
+ - environment (str): The deployment environment (e.g., 'production', 'development').
27
+ - application_name (str): Name of the Langchain application.
28
+ - tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.
29
+ - pricing_info (dict): Information about the pricing for internal metrics (currently not used).
30
+ - trace_content (bool): Flag indicating whether to trace the content of the response.
31
+
32
+ Returns:
33
+ - function: A higher-order function that takes a function 'wrapped' and returns
34
+ a new function that wraps 'wrapped' with additional tracing and logging.
35
+ """
36
+
37
+ def wrapper(wrapped, instance, args, kwargs):
38
+ """
39
+ An inner wrapper function that executes the wrapped function, measures execution
40
+ time, and records trace data using OpenTelemetry.
41
+
42
+ Parameters:
43
+ - wrapped (Callable): The original function that this wrapper will execute.
44
+ - instance (object): The instance to which the wrapped function belongs. This
45
+ is used for instance methods. For static and classmethods,
46
+ this may be None.
47
+ - args (tuple): Positional arguments passed to the wrapped function.
48
+ - kwargs (dict): Keyword arguments passed to the wrapped function.
49
+
50
+ Returns:
51
+ - The result of the wrapped function call.
52
+
53
+ The wrapper initiates a span with the provided tracer, sets various attributes
54
+ on the span based on the function's execution and response, and ensures
55
+ errors are handled and logged appropriately.
56
+ """
57
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
58
+ response = wrapped(*args, **kwargs)
59
+
60
+ try:
61
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
62
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
63
+ "langchain")
64
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
65
+ gen_ai_endpoint)
66
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
67
+ environment)
68
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
69
+ SemanticConvetion.GEN_AI_TYPE_FRAMEWORK)
70
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
71
+ application_name)
72
+ span.set_attribute(SemanticConvetion.GEN_AI_RETRIEVAL_SOURCE,
73
+ response[0].metadata["source"])
74
+ span.set_status(Status(StatusCode.OK))
75
+
76
+ # Return original response
77
+ return response
78
+
79
+ except Exception as e:
80
+ handle_exception(span, e)
81
+ logger.error("Error in trace creation: %s", e)
82
+
83
+ # Return original response
84
+ return response
85
+
86
+ return wrapper
87
+
88
+ def hub(gen_ai_endpoint, version, environment, application_name, tracer,
89
+ pricing_info, trace_content):
90
+ """
91
+ Creates a wrapper around Langchain hub operations for tracing and logging.
92
+
93
+ Similar to `general_wrap`, this function focuses on wrapping functions involved
94
+ in interacting with the Langchain hub, adding specific metadata relevant to
95
+ hub operations to the span attributes.
96
+
97
+ Parameters:
98
+ - gen_ai_endpoint (str): A descriptor or name for the Langchain hub endpoint.
99
+ - version (str): The version of the Langchain application.
100
+ - environment (str): The deployment environment, such as 'production' or 'development'.
101
+ - application_name (str): Name of the Langchain application.
102
+ - tracer (opentelemetry.trace.Tracer): The tracer for OpenTelemetry tracing.
103
+ - pricing_info (dict): Pricing information for the operation (not currently used).
104
+ - trace_content (bool): Indicates if the content of the response should be traced.
105
+
106
+ Returns:
107
+ - function: A new function that wraps the original hub operation call with added
108
+ logging, tracing, and metric calculation functionalities.
109
+ """
110
+
111
+ def wrapper(wrapped, instance, args, kwargs):
112
+ """
113
+ An inner wrapper specifically designed for Langchain hub operations,
114
+ providing tracing, logging, and execution metrics.
115
+
116
+ Parameters:
117
+ - wrapped (Callable): The original hub operation function to be executed.
118
+ - instance (object): The instance of the class where the hub operation
119
+ method is defined. May be None for static or class methods.
120
+ - args (tuple): Positional arguments to pass to the hub operation function.
121
+ - kwargs (dict): Keyword arguments to pass to the hub operation function.
122
+
123
+ Returns:
124
+ - The result of executing the hub operation function.
125
+
126
+ This wrapper captures additional metadata relevant to Langchain hub operations,
127
+ creating spans with specific attributes and metrics that reflect the nature of
128
+ each hub call.
129
+ """
130
+
131
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
132
+ response = wrapped(*args, **kwargs)
133
+
134
+ try:
135
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
136
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
137
+ SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN)
138
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
139
+ gen_ai_endpoint)
140
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
141
+ environment)
142
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
143
+ SemanticConvetion.GEN_AI_TYPE_FRAMEWORK)
144
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
145
+ application_name)
146
+ span.set_attribute(SemanticConvetion.GEN_AI_HUB_OWNER,
147
+ response.metadata["lc_hub_owner"])
148
+ span.set_attribute(SemanticConvetion.GEN_AI_HUB_REPO,
149
+ response.metadata["lc_hub_repo"])
150
+ span.set_status(Status(StatusCode.OK))
151
+
152
+ return response
153
+
154
+ except Exception as e:
155
+ handle_exception(span, e)
156
+ logger.error("Error in trace creation: %s", e)
157
+
158
+ # Return original response
159
+ return response
160
+
161
+ return wrapper