Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds partition key to logger + UI

elijahbenizzy50d22139195c
diff --git a/burr/cli/demo_data.py b/burr/cli/demo_data.py
index fcd7d7fd..888ab154 100644
--- a/burr/cli/demo_data.py
+++ b/burr/cli/demo_data.py
@@ -87,12 +87,13 @@ def generate_chatbot_data(data_dir: str, use_traces: bool):
def generate_counter_data(data_dir: str = "~/.burr"):
counts = [1, 10, 100, 50, 42]
- for count in counts:
+ for i, count in enumerate(counts):
app = counter_application.application(
count_up_to=count,
app_id=f"count-to-{count}",
storage_dir=data_dir,
hooks=[ProgressHook()],
+ partition_key=f"user_{i}",
)
app.run(halt_after=["result"])
diff --git a/burr/core/application.py b/burr/core/application.py
index 4c9ae20a..4eda9689 100644
--- a/burr/core/application.py
+++ b/burr/core/application.py
@@ -259,6 +259,7 @@ class Application:
state=self._state,
application_graph=self._graph,
app_id=self._uid,
+ partition_key=self._partition_key,
)
# TODO -- consider adding global inputs + global input factories to the builder
self.dependency_factory = {
diff --git a/burr/lifecycle/base.py b/burr/lifecycle/base.py
index 5253b41c..30508011 100644
--- a/burr/lifecycle/base.py
+++ b/burr/lifecycle/base.py
@@ -131,7 +131,7 @@ class PostApplicationCreateHook(abc.ABC):
self,
*,
app_id: str,
- partition_key: str,
+ partition_key: Optional[str],
state: "State",
application_graph: "ApplicationGraph",
**future_kwargs: Any,
@@ -139,6 +139,8 @@ class PostApplicationCreateHook(abc.ABC):
"""Runs after an "application" object is instantiated. This is run by the Application, in its constructor,
as the last step.
+ :param app_id: Application ID
+ :param partition_key: Partition key of application
:param state: Current state of the application
:param application_graph: Application graph of the application, representing the state machine
:param future_kwargs: Future keyword arguments for backwards compatibility
diff --git a/burr/tracking/client.py b/burr/tracking/client.py
index 48875f5a..c6ef95eb 100644
--- a/burr/tracking/client.py
+++ b/burr/tracking/client.py
@@ -16,6 +16,7 @@ from burr.lifecycle import (
PreStartSpanHook,
)
from burr.tracking.common.models import (
+ ApplicationMetadataModel,
ApplicationModel,
BeginEntryModel,
BeginSpanModel,
@@ -57,6 +58,7 @@ class LocalTrackingClient(
"""
GRAPH_FILENAME = "graph.json"
+ METADATA_FILENAME = "metadata.json"
LOG_FILENAME = "log.jsonl"
DEFAULT_STORAGE_DIR = "~/.burr"
@@ -154,20 +156,31 @@ class LocalTrackingClient(
self,
*,
app_id: str,
+ partition_key: Optional[str],
state: "State",
application_graph: "ApplicationGraph",
**future_kwargs: Any,
):
self._ensure_dir_structure(app_id)
self.f = open(os.path.join(self.storage_dir, app_id, self.LOG_FILENAME), "a")
- path = os.path.join(self.storage_dir, app_id, self.GRAPH_FILENAME)
- if os.path.exists(path):
- logger.info(f"Graph already exists at {path}. Not overwriting.")
+ graph_path = os.path.join(self.storage_dir, app_id, self.GRAPH_FILENAME)
+ if os.path.exists(graph_path):
+ logger.info(f"Graph already exists at {graph_path}. Not overwriting.")
return
graph = ApplicationModel.from_application_graph(application_graph).model_dump()
- with open(path, "w") as f:
+ with open(graph_path, "w") as f:
json.dump(graph, f)
+ metadata_path = os.path.join(self.storage_dir, app_id, self.METADATA_FILENAME)
+ if os.path.exists(metadata_path):
+ logger.info(f"Metadata already exists at {metadata_path}. Not overwriting.")
+ return
+ metadata = ApplicationMetadataModel(
+ partition_key=partition_key,
+ ).model_dump()
+ with open(metadata_path, "w") as f:
+ json.dump(metadata, f)
+
def _append_write_line(self, model: pydantic.BaseModel):
self.f.write(model.model_dump_json() + "\n")
self.f.flush()
diff --git a/burr/tracking/common/models.py b/burr/tracking/common/models.py
index edd09ee1..a7faefd1 100644
--- a/burr/tracking/common/models.py
+++ b/burr/tracking/common/models.py
@@ -86,6 +86,14 @@ class ApplicationModel(IdentifyingModel):
)
+class ApplicationMetadataModel(IdentifyingModel):
+ """Pydantic model that represents metadata for an application.
+ We will want to add tags here when we have them."""
+
+ partition_key: Optional[str]
+ type: str = "application_metadata"
+
+
INPUT_FILTERLIST = {"__tracer"}
diff --git a/burr/tracking/server/backend.py b/burr/tracking/server/backend.py
index 2ae7c8ad..39862bf2 100644
--- a/burr/tracking/server/backend.py
+++ b/burr/tracking/server/backend.py
@@ -7,6 +7,7 @@ import aiofiles
import aiofiles.os as aiofilesos
import fastapi
+from burr.tracking.common import models
from burr.tracking.common.models import BeginEntryModel, BeginSpanModel, EndEntryModel, EndSpanModel
from burr.tracking.server import schema
from burr.tracking.server.schema import ApplicationLogs, ApplicationSummary
@@ -119,11 +120,20 @@ class LocalBackend(BackendBase):
# skip hidden files/directories
continue
full_path = os.path.join(project_filepath, entry)
+ metadata_path = os.path.join(full_path, "metadata.json")
log_path = os.path.join(full_path, "log.jsonl")
if os.path.isdir(full_path):
+ if os.path.exists(metadata_path):
+ async with aiofiles.open(metadata_path) as f:
+ metadata = models.ApplicationMetadataModel.parse_obj(
+ json.loads(await f.read())
+ )
+ else:
+ metadata = models.ApplicationMetadataModel() # backwards compatibility
out.append(
schema.ApplicationSummary(
app_id=entry,
+ partition_key=metadata.partition_key,
first_written=await aiofilesos.path.getctime(full_path),
last_written=await aiofilesos.path.getmtime(full_path),
num_steps=await self.get_max_sequence_id(log_path),
diff --git a/burr/tracking/server/schema.py b/burr/tracking/server/schema.py
index e6d7c6c2..af13b6bb 100644
--- a/burr/tracking/server/schema.py
+++ b/burr/tracking/server/schema.py
@@ -23,6 +23,7 @@ class Project(pydantic.BaseModel):
class ApplicationSummary(pydantic.BaseModel):
app_id: str
+ partition_key: Optional[str]
first_written: datetime.datetime
last_written: datetime.datetime
num_steps: int
diff --git a/examples/conversational_rag/application.py b/examples/conversational_rag/application.py
index cafa0d50..c93f1454 100644
--- a/examples/conversational_rag/application.py
+++ b/examples/conversational_rag/application.py
@@ -95,7 +95,7 @@ def application(
)
.with_entrypoint("human_converse")
.with_tracker(project="demo:conversational-rag", params={"storage_dir": storage_dir})
- .with_identifiers(app_id=app_id)
+ .with_identifiers(app_id=app_id, partition_key="sample_user")
.with_hooks(*hooks if hooks else [])
.build()
)
diff --git a/telemetry/ui/src/api/models/ApplicationSummary.ts b/telemetry/ui/src/api/models/ApplicationSummary.ts
index ad867485..0f76a9ac 100644
--- a/telemetry/ui/src/api/models/ApplicationSummary.ts
+++ b/telemetry/ui/src/api/models/ApplicationSummary.ts
@@ -3,10 +3,10 @@
/* tslint:disable */
/* eslint-disable */
export type ApplicationSummary = {
- app_id: string;
- first_written: string;
- last_written: string;
- num_steps: number;
- tags: Record<string, string>;
+ app_id: string;
+ partition_key: string | null;
+ first_written: string;
+ last_written: string;
+ num_steps: number;
+ tags: Record<string, string>;
};
-
diff --git a/telemetry/ui/src/components/routes/AppList.tsx b/telemetry/ui/src/components/routes/AppList.tsx
index b801864c..e94957a2 100644
--- a/telemetry/ui/src/components/routes/AppList.tsx
+++ b/telemetry/ui/src/components/routes/AppList.tsx
@@ -44,6 +44,7 @@ export const AppListTable = (props: { apps: ApplicationSummary[]; projectId: str
<Table>
<TableHead>
<TableRow>
+ <TableHeader>Partition Key</TableHeader>
<TableHeader>ID</TableHeader>
<TableHeader>First Seen</TableHeader>
<TableHeader>Last Run</TableHeader>
@@ -64,6 +65,7 @@ export const AppListTable = (props: { apps: ApplicationSummary[]; projectId: str
className="hover:bg-gray-50 cursor-pointer"
href={`/project/${props.projectId}/${app.app_id}`}
>
+ <TableCell className="text-gray-600 font-sans">{app.partition_key}</TableCell>
<TableCell className="font-semibold text-gray-700">{app.app_id}</TableCell>
<TableCell>
<DateTimeDisplay date={app.first_written} mode="long" />