Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add queries and tokens metrics #647

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
25 changes: 25 additions & 0 deletions agents-api/agents_api/metrics/counters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from functools import wraps
from typing import Callable, ParamSpec, TypeVar

from prometheus_client import Counter

P = ParamSpec("P")
T = TypeVar("T")


def increase_counter(metric_label: str, id_field_name: str = "developer_id"):
def decor(func: Callable[P, T] | None = None):
whiterabbit1983 marked this conversation as resolved.
Show resolved Hide resolved
metric = Counter(
metric_label,
f"Number of {metric_label} calls",
labelnames=(id_field_name,),
)

@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
metric.labels(kwargs.get(id_field_name, "not_set")).inc()
return func(*args, **kwargs)

return wrapper

return decor
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/agent/create_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ...autogen.openapi_model import Agent, CreateAgentRequest
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -57,6 +58,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("create_agent")
@beartype
def create_agent(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/agent/create_or_update_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ...autogen.openapi_model import Agent, CreateOrUpdateAgentRequest
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -48,6 +49,7 @@
Agent, one=True, transform=lambda d: {"id": UUID(d.pop("agent_id")), **d}
)
@cozo_query
@increase_counter("create_or_update_agent")
@beartype
def create_or_update_agent(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/agent/patch_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ...autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse
from ...common.utils.cozo import cozo_process_mutate_data
from ...common.utils.datetime import utcnow
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -36,6 +37,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("patch_agent")
@beartype
def patch_agent(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/agent/update_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ...autogen.openapi_model import ResourceUpdatedResponse, UpdateAgentRequest
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -35,6 +36,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("update_agent")
@beartype
def update_agent(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/docs/create_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ...autogen.openapi_model import CreateDocRequest, Doc
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -37,6 +38,7 @@
},
)
@cozo_query
@increase_counter("create_doc")
@beartype
def create_doc(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/entry/create_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ...common.utils.cozo import cozo_process_mutate_data
from ...common.utils.datetime import utcnow
from ...common.utils.messages import content_to_json
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
mark_session_updated_query,
Expand Down Expand Up @@ -40,6 +41,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("create_entries")
@beartype
def create_entries(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/execution/create_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ...autogen.openapi_model import CreateExecutionRequest, Execution
from ...common.utils.cozo import cozo_process_mutate_data
from ...common.utils.types import dict_like
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -36,6 +37,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("create_execution")
@beartype
def create_execution(
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from ...common.protocol.tasks import transition_to_execution_status, valid_transitions
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -72,6 +73,7 @@ def validate_transition_targets(data: CreateTransitionRequest) -> None:
_kind="inserted",
)
@cozo_query
@increase_counter("create_execution_transition")
@beartype
def create_execution_transition(
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from temporalio.client import WorkflowHandle

from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand All @@ -28,6 +29,7 @@
}
)
@cozo_query
@increase_counter("create_temporal_lookup")
@beartype
def create_temporal_lookup(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/execution/update_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
valid_previous_statuses as valid_previous_statuses_map,
)
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -41,6 +42,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("update_execution")
@beartype
def update_execution(
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ResourceUpdatedResponse,
)
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -43,6 +44,7 @@
},
)
@cozo_query
@increase_counter("create_or_update_session")
@beartype
def create_or_update_session(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/session/create_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pydantic import ValidationError

from ...autogen.openapi_model import CreateSessionRequest, Session
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -44,6 +45,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("create_session")
@beartype
def create_session(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/session/update_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ...autogen.openapi_model import ResourceUpdatedResponse, UpdateSessionRequest
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -51,6 +52,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("update_session")
@beartype
def update_session(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/task/create_or_update_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ...common.protocol.tasks import task_to_spec
from ...common.utils.cozo import cozo_process_mutate_data
from ...common.utils.datetime import utcnow
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -49,6 +50,7 @@
},
)
@cozo_query
@increase_counter("create_or_update_task")
@beartype
def create_or_update_task(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/task/create_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from ...common.protocol.tasks import spec_to_task, task_to_spec
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand All @@ -38,6 +39,7 @@
)
@wrap_in_class(spec_to_task, one=True, _kind="inserted")
@cozo_query
@increase_counter("create_task")
@beartype
def create_task(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/task/patch_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ...autogen.openapi_model import PatchTaskRequest, ResourceUpdatedResponse, TaskSpec
from ...common.protocol.tasks import task_to_spec
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -46,6 +47,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("patch_task")
@beartype
def patch_task(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/task/update_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ...autogen.openapi_model import ResourceUpdatedResponse, UpdateTaskRequest
from ...common.protocol.tasks import task_to_spec
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -45,6 +46,7 @@
},
)
@cozo_query
@increase_counter("update_task")
@beartype
def update_task(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/tools/create_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pydantic import ValidationError

from ...autogen.openapi_model import CreateToolRequest, Tool
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -39,6 +40,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("create_tools")
@beartype
def create_tools(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/tools/patch_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ...autogen.openapi_model import PatchToolRequest, ResourceUpdatedResponse
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -35,6 +36,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("patch_tool")
@beartype
def patch_tool(
*, developer_id: UUID, agent_id: UUID, tool_id: UUID, data: PatchToolRequest
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/tools/update_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
UpdateToolRequest,
)
from ...common.utils.cozo import cozo_process_mutate_data
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -38,6 +39,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("update_tool")
@beartype
def update_tool(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/user/create_or_update_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pydantic import ValidationError

from ...autogen.openapi_model import CreateOrUpdateUserRequest, User
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -45,6 +46,7 @@
)
@wrap_in_class(User, one=True, transform=lambda d: {"id": UUID(d.pop("user_id")), **d})
@cozo_query
@increase_counter("create_or_update_user")
@beartype
def create_or_update_user(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/user/create_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pydantic import ValidationError

from ...autogen.openapi_model import CreateUserRequest, User
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -56,6 +57,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("create_user")
@beartype
def create_user(
*,
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/models/user/patch_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ...autogen.openapi_model import PatchUserRequest, ResourceUpdatedResponse
from ...common.utils.cozo import cozo_process_mutate_data
from ...common.utils.datetime import utcnow
from ...metrics.counters import increase_counter
from ..utils import (
cozo_query,
partialclass,
Expand Down Expand Up @@ -50,6 +51,7 @@
_kind="inserted",
)
@cozo_query
@increase_counter("patch_user")
@beartype
def patch_user(
*,
Expand Down
Loading
Loading