Skip to content

EventHandler

db-ally provides an EventHandler abstract class that can be used to log the runs of db-ally to any external systems.

Lifecycle

Every run of db-ally will trigger any instance of EventHandler that was registered using dbally.use_event_handler method in following manner:

  1. EventHandler.request_start is called with RequestStart, it can return a context object that will be passed to next calls.
  2. For each event that occurs during the run, EventHandler.event_start is called with an Event and the context object returned by EventHandler.request_start. It can return context for the event_end method.
  3. When the event ends EventHandler.event_end is called with an Event and the context object returned by EventHandler.event_start.
  4. On the end of the run EventHandler.request_end is called with RequestEnd and the context object returned by EventHandler.request_start.

dbally.audit.event_handlers.EventHandler

Bases: Generic[RequestCtx, EventCtx], ABC

Base event handler interface.

request_start abstractmethod async

request_start(user_request)

Log the start of the request.

PARAMETER DESCRIPTION
user_request

TYPE: RequestStart

PARAMETER DESCRIPTION
user_request

The start of the request.

TYPE: RequestStart

Source code in src/dbally/audit/event_handlers/base.py
@abc.abstractmethod
async def request_start(self, user_request: RequestStart) -> RequestCtx:
    """
    Log the start of the request.

    Args:
        user_request: The start of the request.
    """

event_start abstractmethod async

event_start(event, request_context)

Log the start of the event.

PARAMETER DESCRIPTION
event

TYPE: Union[LLMEvent]

request_context

TYPE: RequestCtx

PARAMETER DESCRIPTION
event

Event to be logged.

TYPE: Union[LLMEvent]

request_context

Optional context passed from request_start method

TYPE: RequestCtx

Source code in src/dbally/audit/event_handlers/base.py
@abc.abstractmethod
async def event_start(self, event: Union[LLMEvent], request_context: RequestCtx) -> EventCtx:
    """
    Log the start of the event.

    Args:
        event: Event to be logged.
        request_context: Optional context passed from request_start method
    """

event_end abstractmethod async

event_end(event, request_context, event_context)

Log the end of the event.

PARAMETER DESCRIPTION
event

TYPE: Union[None, LLMEvent]

request_context

TYPE: RequestCtx

event_context

TYPE: EventCtx

PARAMETER DESCRIPTION
event

Event to be logged.

TYPE: Union[None, LLMEvent]

request_context

Optional context passed from request_start method

TYPE: RequestCtx

event_context

Optional context passed from event_start method

TYPE: EventCtx

Source code in src/dbally/audit/event_handlers/base.py
@abc.abstractmethod
async def event_end(
    self, event: Union[None, LLMEvent], request_context: RequestCtx, event_context: EventCtx
) -> None:
    """
    Log the end of the event.

    Args:
        event: Event to be logged.
        request_context: Optional context passed from request_start method
        event_context: Optional context passed from event_start method
    """

request_end abstractmethod async

request_end(output, request_context)

Log the end of the request.

PARAMETER DESCRIPTION
output

TYPE: RequestEnd

request_context

TYPE: RequestCtx

PARAMETER DESCRIPTION
output

The output of the request.

TYPE: RequestEnd

request_context

Optional context passed from request_start method

TYPE: RequestCtx

Source code in src/dbally/audit/event_handlers/base.py
@abc.abstractmethod
async def request_end(self, output: RequestEnd, request_context: RequestCtx) -> None:
    """
    Log the end of the request.

    Args:
        output: The output of the request.
        request_context: Optional context passed from request_start method
    """

dbally.data_models.audit.RequestStart dataclass

RequestStart(collection_name, question)

Class representing request start data.

collection_name instance-attribute

collection_name

question instance-attribute

question

dbally.data_models.audit.RequestEnd dataclass

RequestEnd(result)

Class representing request end data.

result instance-attribute

result