To divide your existing PromQL query by another vector in order to get a per-request rate, you need to ensure that the division operation aligns the time series correctly based on their labels. In PromQL, when performing operations like division between two metrics, you need to handle label matching carefully to avoid issues with mismatched time series.
Here’s how you can modify your query to divide the token count rate by the invocation count, ensuring proper label alignment:
Step-by-Step Explanation
-
Your Current Query:
(sum by (type)(rate(aiplatform_googleapis_com:publisher_online_serving_token_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])))
- This calculates the per-second rate of token counts over the specified interval (
${__interval}) and aggregates the results by the type label.
-
The Denominator:
- You want to divide this by
aiplatform_googleapis_com:publisher_online_serving_model_invocation_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}, which represents the total number of model invocations.
- To make this a rate (e.g., invocations per second), you should also apply the
rate() function to the invocation count metric, similar to how you handled the token count.
-
Label Matching:
- When dividing two metrics in PromQL, the labels of the time series on both sides of the division must match, or you need to use operators like
on() or group_left() to specify how to align the series.
- Since your numerator is aggregated by
type using sum by (type), the denominator should also be aggregated by type to ensure the division is performed correctly for each type.
-
Final Query:
Here’s the complete PromQL query to calculate the per-request rate of token counts:
(sum by (type)(rate(aiplatform_googleapis_com:publisher_online_serving_token_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])))
/
(sum by (type)(rate(aiplatform_googleapis_com:publisher_online_serving_model_invocation_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])))
Explanation of the Final Query
-
Numerator:
rate(aiplatform_googleapis_com:publisher_online_serving_token_count{...}[${__interval}]): Computes the per-second rate of token counts over the specified interval.
sum by (type)(...): Aggregates the token count rates by the type label, summing up all time series with the same type.
-
Denominator:
rate(aiplatform_googleapis_com:publisher_online_serving_model_invocation_count{...}[${__interval}]): Computes the per-second rate of model invocations over the same interval.
sum by (type)(...): Aggregates the invocation count rates by the type label, ensuring the labels match the numerator.
-
Division:
- The
/ operator divides the aggregated token count rate by the aggregated invocation count rate, producing the per-request token count for each type.
Notes for Google Stackdriver (Google Cloud Monitoring)
-
Metric Naming:
- In Google Cloud Monitoring, metric names often include a domain prefix (e.g.,
aiplatform.googleapis.com). Ensure that the metric names in your query exactly match those provided by Google Cloud Monitoring. You can verify the metric names in the Metrics Explorer.
-
Interval (${__interval}):
- The
${__interval} variable is typically provided by Grafana or other visualization tools to dynamically set the time range for the rate() function based on the dashboard’s time range. If you’re running this query directly in Google Cloud Monitoring, you may need to replace ${__interval} with a fixed duration, such as 5m (5 minutes), depending on your use case.
-
Handling Missing Data:
-
Testing the Query:
- Use the Google Cloud Monitoring Query Editor to test your query and ensure it returns the expected results. You can also use the Metrics Explorer to visualize the individual metrics (
token_count and invocation_count) before combining them in the query.
Example Use Case
If you’re using this query in a Grafana dashboard with Google Cloud Monitoring as the data source, the ${__interval} variable will automatically adjust based on the dashboard’s time range. The query will produce a time series for each type, showing the average number of tokens processed per model invocation request.
Final Query (Grafana-Compatible)
Here’s the final query, assuming you’re using Grafana with Google Cloud Monitoring:
(sum by (type)(rate(aiplatform_googleapis_com:publisher_online_serving_token_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])))
/
(sum by (type)(rate(aiplatform_googleapis_com:publisher_online_serving_model_invocation_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])))
This query will give you the per-request token count, aggregated by type, over the selected time range.