Skip to content

Commit

Permalink
Moved flushing execution to the last span in the context and improved…
Browse files Browse the repository at this point in the history
… the documentation to give examples of flushing properly manually
  • Loading branch information
Redna committed Jun 10, 2024
1 parent 629c1ac commit c9ed795
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,25 @@ class LangfuseConnector:
Lastly, you may disable flushing the data after each component by setting the `HAYSTACK_LANGFUSE_ENFORCE_FLUSH` environent
variable to `false`. By default, the data is flushed after each component and blocks the thread until the data is sent to
Langfuse. **Caution**: Disabling this feature may result in data loss if the program crashes before the data is sent to Langfuse.
Make sure you will call langfuse.flush() explicitly before the program exits. e.g. by using:
Make sure you will call langfuse.flush() explicitly before the program exits. e.g. by using tracer.actual_tracer.flush():
```python
from haystack.tracing import tracer
try:
# your code here
finally:
langfuse.flush()
tracer.actual_tracer.flush()
```
or in FastAPI by defining a shutdown event handler:
```python
from haystack.tracing import tracer
# ...
@app.on_event("shutdown")
async def shutdown_event():
langfuse.flush()
tracer.actual_tracer.flush()
```
Here is an example of how to use it:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> I
if len(self._context) == 1:
# The root span has to be a trace, which need to be removed from the context after the pipeline run
self._context.pop()

if self.enforce_flush:
self.flush()

if self.enforce_flush:
self._tracer.flush()
def flush(self):
self._tracer.flush()

def current_span(self) -> Span:
"""
Expand Down

0 comments on commit c9ed795

Please sign in to comment.