Fix syntax errors in Telemetry class by adding thread safety to __init__ method

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-04 20:13:47 +00:00
parent 93bea7e9b7
commit e14bcd59f3

View File

@@ -67,39 +67,41 @@ class Telemetry:
def __init__(self): def __init__(self):
if not self._initialized: if not self._initialized:
self.ready = False with self._instance_lock:
self.trace_set = False if not self._initialized:
self.ready = False
self.trace_set = False
if os.getenv("OTEL_SDK_DISABLED", "false").lower() == "true": if os.getenv("OTEL_SDK_DISABLED", "false").lower() == "true":
self._initialized = True self._initialized = True
return return
try: try:
telemetry_endpoint = "https://telemetry.crewai.com:4319" telemetry_endpoint = "https://telemetry.crewai.com:4319"
self.resource = Resource( self.resource = Resource(
attributes={SERVICE_NAME: "crewAI-telemetry"}, attributes={SERVICE_NAME: "crewAI-telemetry"},
) )
with suppress_warnings(): with suppress_warnings():
self.provider = TracerProvider(resource=self.resource) self.provider = TracerProvider(resource=self.resource)
processor = BatchSpanProcessor( processor = BatchSpanProcessor(
OTLPSpanExporter( OTLPSpanExporter(
endpoint=f"{telemetry_endpoint}/v1/traces", endpoint=f"{telemetry_endpoint}/v1/traces",
timeout=30, timeout=30,
) )
) )
self.provider.add_span_processor(processor) self.provider.add_span_processor(processor)
self.ready = True self.ready = True
except Exception as e: except Exception as e:
if isinstance( if isinstance(
e, e,
(SystemExit, KeyboardInterrupt, GeneratorExit, asyncio.CancelledError), (SystemExit, KeyboardInterrupt, GeneratorExit, asyncio.CancelledError),
): ):
raise # Re-raise the exception to not interfere with system signals raise # Re-raise the exception to not interfere with system signals
self.ready = False self.ready = False
self._initialized = True self._initialized = True
def __del__(self): def __del__(self):
"""Clean up resources when the instance is destroyed.""" """Clean up resources when the instance is destroyed."""