Skip to main content

Query gateway request analytics

Query the requests that Unkey routed to your deployments with the analytics.getGatewayRequests endpoint.
4 min read

The POST /v2/analytics.getGatewayRequests endpoint runs SQL queries on the requests that Unkey routed to your deployments. Use it from a trusted backend with a root key. Never put a root key in browser code.

For the request and response schemas, see the API reference.

Authenticate the request#

Use a root key with the project.*.read_gateway_requests permission. This permission gives access to the gateway request data of all projects in the workspace.

Unkey limits each query to the workspace of the root key. A query cannot read the data of a different workspace, and it cannot remove this filter.

Send a query#

Send a JSON object with a query string. The response contains meta.requestId, which identifies the API request, and data, which contains an array of result objects.

Response

Queries follow the SQL limits and the resource limits in Query restrictions. Only SELECT queries are permitted. CTEs, subqueries, UNION, and EXCEPT are also permitted.

Select the data of one project, app, or environment#

Each row contains project_id, app_id, and environment_id. Add a filter on one of these columns to get the data of one deployment target.

Use IN to select more than one target.

If you do not add one of these filters, the query reads all projects in the workspace.

Understand the time range#

Two limits control the time range of a query: the 7 day history of gateway_requests_v1 and the retention setting of your workspace plan. A query gets only the rows that satisfy both limits. If you do not add a time filter, Unkey limits the results to your workspace retention range.

Reference available columns#

The table contains one row for each request. Unkey filters workspace_id automatically, thus you do not need to add it to a query.

A query must name the columns that it needs. SELECT * fails, because the table also contains columns of the Unkey infrastructure that a workspace cannot read.

ColumnTypeDescription
request_idStringUnique request ID
timeInt64Request time as a Unix timestamp in milliseconds
project_idStringProject ID
app_idStringApp ID
environment_idStringEnvironment ID
deployment_idStringDeployment that received the request
instance_idStringInstance that processed the request
regionStringRegion that served the request
methodStringHTTP method in upper case
hostStringHost header of the request
pathStringRequest path
query_stringStringRaw query string
query_paramsMap(String, Array(String))Parsed query parameters
request_headersArray(String)Request headers as Key: Value pairs
request_bodyStringRequest body
response_statusInt32HTTP status code of the response
response_headersArray(String)Response headers as Key: Value pairs
response_bodyStringResponse body
user_agentStringUser agent of the caller
ip_addressStringIP address of the caller
total_latencyInt64Full end to end time in milliseconds
instance_latencyInt64Time your instance used, in milliseconds
gateway_latencyInt64Time the Unkey gateway added, in milliseconds

Read a latency percentile#

The table keeps total_latency, instance_latency, and gateway_latency as plain numbers. Use quantile on these columns.

Find the paths with the most errors#

Group by path to find the endpoints that return the most server errors. The table keeps time as Unix milliseconds, thus the time filter uses toUnixTimestamp64Milli.

Calculate an error rate for each deployment#

Use countIf to count a subset of the rows in the same query. This example compares the failed requests with all requests of each deployment.

Build a zero-filled time series#

Use WITH FILL to get each minute, including the minutes with no requests. This query gives a dashboard chart the full range without gap filling in the client.

Compare the status codes of each app#

Group by app_id and response_status to see the response mix of each app.