Metrics
Application Kit provides a Redis-backed queue system for counting API requests. This enables usage tracking and billing calculations by recording how many requests each project makes to different products and endpoints.
Metrics Queue
The metrics queue records request counts that are processed asynchronously by a worker. Use add_job_to_queue_for_token() after authenticating a request to count it. The function:
- Extracts organization and project information from
ProjectTokenModel - Respects the
should_countcontext flag (internal requests on port 8000 are excluded) - Tracks request source via
X-SDK-Sourceheader orAI-Context: trueheader
Queue Infrastructure
The Queue class provides a reliable Redis-backed job queue with support for job tracking and failure handling. Jobs move through three Redis lists:
todo: Pending jobs waiting to be processeddoing: Jobs currently being processedfailed: Jobs that encountered errors
The ThreadPool and Worker classes provide background processing with proper Django database connection management via signals.
application_kit.metrics.queue
Core implementation of the metrics queue client interaction.
# get the project token on Django-based applications
project_token = get_project_token(request)
# on FastAPI-based applications:
project_token = request.state.project_token
# or through a dependency like AuthenticateKey.
add_job_to_queue_for_token(project_token, "MAPS", "load")
# if needed we can specify the count
add_job_to_queue_for_token(project_token, "STORES", "import", count=435)
Tip
The request header X-SDK-Source is internally used by SDKs to populate the job source field.
The request header AI-Context (boolean value) is used to populate the job source field.
build_job_data
build_job_data(
product,
kind,
organization,
counter=1,
timestamp=None,
source=None,
)
Builds a metrics job from the parameters.
Warning
This is meant to be used in cases no handled by add_job_to_queue_for_token.
| PARAMETER | DESCRIPTION |
|---|---|
product
|
The product key
TYPE:
|
kind
|
The kind belonging to the project
TYPE:
|
organization
|
The consumption will be added to organization/project
TYPE:
|
counter
|
How many queries should be counted
TYPE:
|
Source code in application_kit/metrics/queue.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
add_job_to_queue_for_token
add_job_to_queue_for_token(
project_token, product, kind, count=1
)
Adds a metrics job to the queue, taking into account the project_token.context.should_count context. Pass source from project_token context
| PARAMETER | DESCRIPTION |
|---|---|
project_token
|
The project token obtained from an authenticated request.
TYPE:
|
Source code in application_kit/metrics/queue.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
application_kit.queue.cua
JobHandler
Bases: Protocol
__call__
__call__(job_id, data)
Job Handler call protocol
Source code in application_kit/queue/cua.py
131 132 | |
WorkerBase
WorkerBase(queue, func)
Bases: Thread
Thread executing tasks from a given tasks queue
Source code in application_kit/queue/cua.py
138 139 140 141 142 143 | |
before_job_hook
before_job_hook()
Ran before job is ran
Source code in application_kit/queue/cua.py
150 151 | |
after_job_hook
after_job_hook()
Ran after job has been completed.
Source code in application_kit/queue/cua.py
153 154 | |
ThreadPool
ThreadPool(num_threads, func, config)
Pool of threads consuming tasks from a queue
Source code in application_kit/queue/cua.py
201 202 203 204 205 206 | |