Context
patronus.context
Context management for Patronus SDK.
This module provides classes and utility functions for managing the global Patronus context and accessing different components of the SDK like logging, tracing, and API clients.
PatronusScope
dataclass
PatronusScope(
service: Optional[str],
project_name: Optional[str],
app: Optional[str],
experiment_id: Optional[str],
experiment_name: Optional[str],
)
Scope information for Patronus context.
Defines the scope of the current Patronus application or experiment.
Attributes:
Name | Type | Description |
---|---|---|
service |
Optional[str]
|
The service name as defined in OTeL. |
project_name |
Optional[str]
|
The project name. |
app |
Optional[str]
|
The application name. |
experiment_id |
Optional[str]
|
The unique identifier for the experiment. |
experiment_name |
Optional[str]
|
The name of the experiment. |
PatronusContext
dataclass
PatronusContext(
scope: PatronusScope,
tracer_provider: TracerProvider,
logger_provider: LoggerProvider,
api_client: PatronusAPIClient,
exporter: BatchEvaluationExporter,
)
Context object for Patronus SDK.
Contains all the necessary components for the SDK to function properly.
Attributes:
Name | Type | Description |
---|---|---|
scope |
PatronusScope
|
Scope information for this context. |
tracer_provider |
TracerProvider
|
The OpenTelemetry tracer provider. |
logger_provider |
LoggerProvider
|
The OpenTelemetry logger provider. |
api_client |
PatronusAPIClient
|
Client for Patronus API communication. |
exporter |
BatchEvaluationExporter
|
Exporter for batch evaluation results. |
set_global_patronus_context
Set the global Patronus context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx
|
PatronusContext
|
The Patronus context to set globally. |
required |
get_current_context_or_none
Get the current Patronus context or None if not initialized.
Returns:
Type | Description |
---|---|
Optional[PatronusContext]
|
The current PatronusContext if set, otherwise None. |
get_current_context
Get the current Patronus context.
Returns:
Type | Description |
---|---|
PatronusContext
|
The current PatronusContext. |
Raises:
Type | Description |
---|---|
UninitializedError
|
If no active Patronus context is found. |
Source code in src/patronus/context/__init__.py
get_logger
Get a standard Python logger configured with the Patronus context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx
|
Optional[PatronusContext]
|
The Patronus context to use. If None, uses the current context. |
None
|
level
|
int
|
The logging level to set. Defaults to INFO. |
INFO
|
Returns:
Type | Description |
---|---|
Logger
|
A configured Python logger. |
Source code in src/patronus/context/__init__.py
get_logger_or_none
Get a standard Python logger or None if context is not initialized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level
|
int
|
The logging level to set. Defaults to INFO. |
INFO
|
Returns:
Type | Description |
---|---|
Optional[Logger]
|
A configured Python logger if context is available, otherwise None. |
Source code in src/patronus/context/__init__.py
get_pat_logger
Get a Patronus logger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx
|
Optional[PatronusContext]
|
The Patronus context to use. If None, uses the current context. |
None
|
Returns:
Type | Description |
---|---|
Logger
|
A Patronus logger. |
Source code in src/patronus/context/__init__.py
get_pat_logger_or_none
Get a Patronus logger or None if context is not initialized.
Returns:
Type | Description |
---|---|
Optional[Logger]
|
A Patronus logger if context is available, otherwise None. |
Source code in src/patronus/context/__init__.py
get_tracer
Get an OpenTelemetry tracer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx
|
Optional[PatronusContext]
|
The Patronus context to use. If None, uses the current context. |
None
|
Returns:
Type | Description |
---|---|
Tracer
|
An OpenTelemetry tracer. |
Source code in src/patronus/context/__init__.py
get_tracer_or_none
Get an OpenTelemetry tracer or None if context is not initialized.
Returns:
Type | Description |
---|---|
Optional[Tracer]
|
An OpenTelemetry tracer if context is available, otherwise None. |
Source code in src/patronus/context/__init__.py
get_api_client
Get the Patronus API client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx
|
Optional[PatronusContext]
|
The Patronus context to use. If None, uses the current context. |
None
|
Returns:
Type | Description |
---|---|
PatronusAPIClient
|
The Patronus API client. |
Source code in src/patronus/context/__init__.py
get_api_client_or_none
Get the Patronus API client or None if context is not initialized.
Returns:
Type | Description |
---|---|
Optional[PatronusAPIClient]
|
The Patronus API client if context is available, otherwise None. |
Source code in src/patronus/context/__init__.py
get_exporter
Get the batch evaluation exporter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx
|
Optional[PatronusContext]
|
The Patronus context to use. If None, uses the current context. |
None
|
Returns:
Type | Description |
---|---|
BatchEvaluationExporter
|
The batch evaluation exporter. |
Source code in src/patronus/context/__init__.py
get_exporter_or_none
Get the batch evaluation exporter or None if context is not initialized.
Returns:
Type | Description |
---|---|
Optional[BatchEvaluationExporter]
|
The batch evaluation exporter if context is available, otherwise None. |
Source code in src/patronus/context/__init__.py
get_scope
Get the Patronus scope.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx
|
Optional[PatronusContext]
|
The Patronus context to use. If None, uses the current context. |
None
|
Returns:
Type | Description |
---|---|
PatronusScope
|
The Patronus scope. |
Source code in src/patronus/context/__init__.py
get_scope_or_none
Get the Patronus scope or None if context is not initialized.
Returns:
Type | Description |
---|---|
Optional[PatronusScope]
|
The Patronus scope if context is available, otherwise None. |