Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /topoteretes_cognee /cognee /modules /observability /trace_context.py
| """Public tracing API: enable/disable tracing and retrieve traces.""" | |
| import os | |
| from typing import Optional | |
| from cognee.modules.observability.tracing import ( | |
| CogneeTrace, | |
| setup_tracing, | |
| shutdown_tracing, | |
| get_exporter, | |
| ) | |
| _tracing_enabled: bool = False | |
| def enable_tracing(console_output: bool = False) -> None: | |
| """Enable OpenTelemetry tracing, metrics, and log bridge for Cognee. | |
| Sets up a TracerProvider with an in-memory CogneeSpanExporter, a | |
| MeterProvider for memory-semconv metrics, and an OTel log bridge so | |
| all cognee loggers emit OTel log records. | |
| Optionally prints spans/metrics to the console when *console_output* is True. | |
| """ | |
| global _tracing_enabled | |
| setup_tracing(console_output=console_output) | |
| try: | |
| from cognee.modules.observability.metrics import setup_metrics | |
| setup_metrics(console_output=console_output) | |
| except Exception: | |
| pass | |
| try: | |
| from cognee.modules.observability.logs import setup_log_bridge | |
| setup_log_bridge(console_output=console_output) | |
| except Exception: | |
| pass | |
| _tracing_enabled = True | |
| def disable_tracing() -> None: | |
| """Disable tracing, metrics, and log bridge; shut down their providers.""" | |
| global _tracing_enabled | |
| shutdown_tracing() | |
| try: | |
| from cognee.modules.observability.metrics import shutdown_metrics | |
| shutdown_metrics() | |
| except Exception: | |
| pass | |
| try: | |
| from cognee.modules.observability.logs import shutdown_log_bridge | |
| shutdown_log_bridge() | |
| except Exception: | |
| pass | |
| _tracing_enabled = False | |
| def is_tracing_enabled() -> bool: | |
| """Return True when tracing is active. | |
| An explicit ``COGNEE_TRACING_ENABLED=false`` is authoritative and vetoes | |
| everything, including Langfuse-key auto-enablement and an already-latched | |
| enabled state. Otherwise checks the module-level flag, then the | |
| ``cognee_tracing_enabled`` config field, then falls back to the env var | |
| directly (to support runtime changes, e.g. in tests). When enabled but | |
| not yet initialized, lazily calls ``enable_tracing()`` if OpenTelemetry | |
| is available. | |
| """ | |
| global _tracing_enabled | |
| from cognee.base_config import _tracing_explicitly_disabled | |
| if _tracing_explicitly_disabled(): | |
| return False | |
| if _tracing_enabled: | |
| return True | |
| from cognee.base_config import get_base_config | |
| config = get_base_config() | |
| enabled = config.cognee_tracing_enabled or os.environ.get( | |
| "COGNEE_TRACING_ENABLED", "" | |
| ).lower() in ("true", "1", "yes") | |
| if enabled: | |
| try: | |
| enable_tracing() | |
| except ImportError: | |
| # OpenTelemetry not installed — flag as enabled so callers know | |
| # tracing is desired, but spans will be no-ops via _NullSpan. | |
| _tracing_enabled = True | |
| return True | |
| return False | |
| def get_last_trace() -> Optional[CogneeTrace]: | |
| """Return the most recent completed trace from the in-memory buffer.""" | |
| exporter = get_exporter() | |
| if exporter is None: | |
| return None | |
| spans = exporter.get_last_trace_spans() | |
| if spans is None: | |
| return None | |
| return CogneeTrace(spans) | |
| def get_all_traces() -> list[CogneeTrace]: | |
| """Return all traces currently held in the in-memory buffer.""" | |
| exporter = get_exporter() | |
| if exporter is None: | |
| return [] | |
| all_traces = exporter.get_all_traces() | |
| return [CogneeTrace(spans) for spans in all_traces.values()] | |
| def clear_traces() -> None: | |
| """Clear the in-memory trace buffer.""" | |
| exporter = get_exporter() | |
| if exporter is not None: | |
| exporter.clear() | |
Xet Storage Details
- Size:
- 3.67 kB
- Xet hash:
- 6fff751df49bd1d025fd6a683935e1ee1f7d65a18b7a5a1afa5944bd9c144fc3
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.