Skip to content

Commit b13e7ab

Browse files
⚡️ Speed up method OpikTracer._get_config by 11% in PR #7183 (feat/global_vars_tracing)
To optimize the code for better performance, we can make minimal yet effective changes. These changes mainly focus on. 1. Reducing repetitive calls, 2. Using more efficient default values, 3. Simplifying logic where possible. Here’s the optimized version of the code. Here's what we modified to optimize the code. 1. Avoid redundant `None` default parameter checks with `if host or api_key:` and `self.global_vars or {}` directly in instantiation. 2. Unified the environmental variable fetch with no redundant `None` assignment since `os.getenv` returns `None` by default if the variable is not set. 3. Simplified the boolean conversion with a direct `bool(config and self._setup_opik(config, trace_id))`. These changes are oriented toward reducing function execution time by minimizing branching logic and redundant checks. The core functionality and output remain unchanged.
1 parent 089df15 commit b13e7ab

File tree

1 file changed

+9
-8
lines changed
  • src/backend/base/langflow/services/tracing

1 file changed

+9
-8
lines changed

src/backend/base/langflow/services/tracing/opik.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import types
55
from typing import TYPE_CHECKING, Any
6+
from uuid import UUID
67

78
from langchain_core.documents import Document
89
from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage
@@ -60,7 +61,7 @@ def __init__(
6061
self.global_vars = global_vars or {}
6162

6263
config = self._get_config(self.global_vars)
63-
self._ready: bool = self._setup_opik(config, trace_id) if config else False
64+
self._ready: bool = bool(config and self._setup_opik(config, trace_id))
6465
self._distributed_headers = None
6566

6667
@property
@@ -235,14 +236,14 @@ def _convert_to_opik_type(self, value):
235236

236237
@staticmethod
237238
def _get_config(global_vars) -> dict:
238-
if global_vars:
239-
host = global_vars.get("OPIK_URL_OVERRIDE", None)
240-
api_key = global_vars.get("OPIK_API_KEY", None)
241-
workspace = global_vars.get("OPIK_WORKSPACE", None)
239+
if global_vars is not None:
240+
host = global_vars.get("OPIK_URL_OVERRIDE")
241+
api_key = global_vars.get("OPIK_API_KEY")
242+
workspace = global_vars.get("OPIK_WORKSPACE")
242243
else:
243-
host = os.getenv("OPIK_URL_OVERRIDE", None)
244-
api_key = os.getenv("OPIK_API_KEY", None)
245-
workspace = os.getenv("OPIK_WORKSPACE", None)
244+
host = os.getenv("OPIK_URL_OVERRIDE")
245+
api_key = os.getenv("OPIK_API_KEY")
246+
workspace = os.getenv("OPIK_WORKSPACE")
246247

247248
# API Key is mandatory for Opik Cloud and URL is mandatory for Open-Source Opik Server
248249
if host or api_key:

0 commit comments

Comments
 (0)