-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Mateusz Hordyński <[email protected]> Co-authored-by: Michal Pstrag <[email protected]>
- Loading branch information
1 parent
6f3f08f
commit 713ed0a
Showing
18 changed files
with
506 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/document-search/chroma_otel.py → examples/document-search/otel.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os | ||
|
||
import typer | ||
|
||
from ragbits.core import audit | ||
|
||
if os.getenv("RAGBITS_VERBOSE", "0") == "1": | ||
typer.echo('Verbose mode is enabled with environment variable "RAGBITS_VERBOSE".') | ||
audit.set_trace_handlers("cli") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import time | ||
from enum import Enum | ||
|
||
from rich.live import Live | ||
from rich.tree import Tree | ||
|
||
from ragbits.core.audit.base import TraceHandler, format_attributes | ||
|
||
|
||
class SpanStatus(Enum): | ||
""" | ||
SpanStatus represents the status of the span. | ||
""" | ||
|
||
ERROR = "ERROR" | ||
STARTED = "STARTED" | ||
COMPLETED = "COMPLETED" | ||
|
||
|
||
class PrintColor(str, Enum): | ||
""" | ||
SpanPrintColor represents the color of font for printing the span related information to the console. | ||
""" | ||
|
||
RUNNING_COLOR = "bold blue" | ||
END_COLOR = "bold green" | ||
ERROR_COLOR = "bold red" | ||
TEXT_COLOR = "grey50" | ||
KEY_COLOR = "plum4" | ||
|
||
|
||
class CLISpan: | ||
""" | ||
CLI Span represents a single operation within a trace. | ||
""" | ||
|
||
def __init__(self, name: str, attributes: dict, parent: "CLISpan | None" = None) -> None: | ||
""" | ||
Constructs a new CLI Span. | ||
Args: | ||
name: The name of the span. | ||
attributes: The attributes of the span. | ||
parent: the parent of initiated span. | ||
""" | ||
self.name = name | ||
self.parent = parent | ||
self.attributes = attributes | ||
self.start_time = time.perf_counter() | ||
self.end_time: float | None = None | ||
self.status = SpanStatus.STARTED | ||
self.tree = Tree("") | ||
if self.parent is not None: | ||
self.parent.tree.add(self.tree) | ||
|
||
def update(self) -> None: | ||
""" | ||
Updates tree label based on span state. | ||
""" | ||
elapsed = f": {(self.end_time - self.start_time):.3f}s" if self.end_time else " ..." | ||
color = { | ||
SpanStatus.ERROR: PrintColor.ERROR_COLOR, | ||
SpanStatus.STARTED: PrintColor.RUNNING_COLOR, | ||
SpanStatus.COMPLETED: PrintColor.END_COLOR, | ||
}[self.status] | ||
name = f"[{color}]{self.name}[/{color}][{PrintColor.TEXT_COLOR}]{elapsed}[/{PrintColor.TEXT_COLOR}]" | ||
|
||
# TODO: Remove truncating after implementing better CLI formatting. | ||
attrs = [ | ||
f"[{PrintColor.KEY_COLOR}]{k}:[/{PrintColor.KEY_COLOR}] " | ||
f"[{PrintColor.TEXT_COLOR}]{str(v)[:120] + ' (...)' if len(str(v)) > 120 else v}[/{PrintColor.TEXT_COLOR}]" # noqa: PLR2004 | ||
for k, v in self.attributes.items() | ||
] | ||
self.tree.label = f"{name}\n{chr(10).join(attrs)}" if attrs else name | ||
|
||
def end(self) -> None: | ||
""" | ||
Sets the current time as the span's end time. | ||
The span's end time is the wall time at which the operation finished. | ||
Only the first call to `end` should modify the span, further calls are ignored. | ||
""" | ||
if self.end_time is None: | ||
self.end_time = time.perf_counter() | ||
|
||
|
||
class CLITraceHandler(TraceHandler[CLISpan]): | ||
""" | ||
CLITraceHandler class for all trace handlers. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.live = Live(auto_refresh=False) | ||
|
||
def start(self, name: str, inputs: dict, current_span: CLISpan | None = None) -> CLISpan: | ||
""" | ||
Log input data at the beginning of the trace. | ||
Args: | ||
name: The name of the trace. | ||
inputs: The input data. | ||
current_span: The current trace span. | ||
Returns: | ||
The updated current trace span. | ||
""" | ||
attributes = format_attributes(inputs, prefix="inputs") | ||
span = CLISpan( | ||
name=name, | ||
attributes=attributes, | ||
parent=current_span, | ||
) | ||
if current_span is None: | ||
self.live = Live(auto_refresh=False) | ||
self.live.start() | ||
self.tree = span.tree | ||
|
||
span.update() | ||
self.live.update(self.tree, refresh=True) | ||
|
||
return span | ||
|
||
def stop(self, outputs: dict, current_span: CLISpan) -> None: | ||
""" | ||
Log output data at the end of the trace. | ||
Args: | ||
outputs: The output data. | ||
current_span: The current trace span. | ||
""" | ||
attributes = format_attributes(outputs, prefix="outputs") | ||
current_span.attributes.update(attributes) | ||
current_span.status = SpanStatus.COMPLETED | ||
current_span.end() | ||
|
||
current_span.update() | ||
self.live.update(self.tree, refresh=True) | ||
|
||
if current_span.parent is None: | ||
self.live.stop() | ||
|
||
def error(self, error: Exception, current_span: CLISpan) -> None: | ||
""" | ||
Log error during the trace. | ||
Args: | ||
error: The error that occurred. | ||
current_span: The current trace span. | ||
""" | ||
attributes = format_attributes({"message": str(error), **vars(error)}, prefix="error") | ||
current_span.attributes.update(attributes) | ||
current_span.status = SpanStatus.ERROR | ||
current_span.end() | ||
|
||
current_span.update() | ||
self.live.update(self.tree, refresh=True) | ||
|
||
if current_span.parent is None: | ||
self.live.stop() |
Oops, something went wrong.