Force flush any pending spans and data for a tracer instance.
This function ensures that all pending spans and telemetry data are
immediately sent to their destinations, rather than waiting for
automatic batching/flushing. It handles multiple flush targets and
provides comprehensive error handling.
:param tracer_instance: The tracer instance to flush
:type tracer_instance: HoneyHiveTracer
:param timeout_millis: Maximum time to wait for flush completion in milliseconds
:type timeout_millis: float
:return: True if flush completed successfully within timeout, False otherwise
:rtype: bool
Example:
.. code-block:: python
# Flush with default timeout (30 seconds)
success = force_flush_tracer(tracer)
# Flush with custom timeout (5 seconds)
success = force_flush_tracer(tracer, timeout_millis=5000)
# Use before critical sections
if force_flush_tracer(tracer):
print("All spans flushed successfully")
else:
print("Flush timeout or error occurred")
Note:
This function attempts to flush multiple components in sequence:
the tracer provider, custom span processors, and batch processors.
It returns True only if all components flush successfully.
Source code in src/honeyhive/tracer/lifecycle/flush.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 | def force_flush_tracer(tracer_instance: Any, timeout_millis: float = 30000) -> bool:
"""Force flush any pending spans and data for a tracer instance.
This function ensures that all pending spans and telemetry data are
immediately sent to their destinations, rather than waiting for
automatic batching/flushing. It handles multiple flush targets and
provides comprehensive error handling.
:param tracer_instance: The tracer instance to flush
:type tracer_instance: HoneyHiveTracer
:param timeout_millis: Maximum time to wait for flush completion in milliseconds
:type timeout_millis: float
:return: True if flush completed successfully within timeout, False otherwise
:rtype: bool
**Example:**
.. code-block:: python
# Flush with default timeout (30 seconds)
success = force_flush_tracer(tracer)
# Flush with custom timeout (5 seconds)
success = force_flush_tracer(tracer, timeout_millis=5000)
# Use before critical sections
if force_flush_tracer(tracer):
print("All spans flushed successfully")
else:
print("Flush timeout or error occurred")
**Note:**
This function attempts to flush multiple components in sequence:
the tracer provider, custom span processors, and batch processors.
It returns True only if all components flush successfully.
"""
safe_log(tracer_instance, "debug", "Force flush requested")
flush_results: List[Tuple[str, bool]] = []
safe_log(
tracer_instance,
"debug",
f"force_flush_tracer: Acquiring _lifecycle_lock (timeout: {timeout_millis}ms)",
)
try:
safe_log(
tracer_instance,
"debug",
"force_flush_tracer: Attempting to acquire _lifecycle_lock...",
)
# Use environment-optimized flush timeout
flush_timeout_seconds = timeout_millis / 1000.0
with acquire_lifecycle_lock_optimized(
"flush", custom_timeout=flush_timeout_seconds
) as acquired:
if not acquired:
safe_log(
tracer_instance,
"warning",
f"Failed to acquire _lifecycle_lock ({flush_timeout_seconds}s)",
)
return False
safe_log(
tracer_instance,
"debug",
"force_flush_tracer: Successfully acquired _lifecycle_lock",
)
# 1. Flush the tracer provider if available and supports it
_flush_tracer_provider(tracer_instance, timeout_millis, flush_results)
# 2. Flush our custom span processor if available
_flush_span_processor(tracer_instance, timeout_millis, flush_results)
# 3. Flush any batch span processors attached to the provider
_flush_batch_processors(tracer_instance, timeout_millis, flush_results)
# Calculate overall result
overall_success = all(result for _, result in flush_results)
_log_flush_results(tracer_instance, overall_success, flush_results)
return overall_success
except Exception as e:
# Graceful degradation - never crash host
safe_log(
tracer_instance,
"error",
"Force flush failed",
honeyhive_data={
"error": str(e),
"error_type": type(e).__name__,
"operation": "force_flush_tracer",
},
)
return False
|