You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stackdriver仪表盘能否将自定义状态码指标以百分比展示?

Yes, You Can Do This (Here's How!)

Absolutely, converting your raw HTTP status code counts into percentage-based visualizations is totally possible in Cloud Monitoring (formerly Stackdriver) with your global logs-based custom metric. Let’s walk through the steps to make this happen:

1. Confirm Your Metric Structure

First, make sure your custom metric is set up with a label for HttpResponseStatus (or whatever you named it) that splits the count by each status code (200, 204, 500, etc.). This label is key for grouping and calculating ratios later.

2. Build the Percentage Calculation

Cloud Monitoring’s query editor lets you create ratio-based metrics directly. Here are two approaches depending on how you want to visualize:

Option 1: Individual Percentage Queries (For Granular Control)

  • Start by adding a new chart to your dashboard.
  • For the denominator (total requests):
    • Select your custom metric, set the aggregation to Sum, and remove any filters on the status code label (so it sums all status codes together).
  • For each status code you care about (e.g., 200):
    • Add another query as the numerator: select your metric, set aggregation to Sum, and add a filter HttpResponseStatus = 200.
    • Use the division operator (/) to divide the numerator by the denominator, then multiply by 100 to get a percentage (e.g., (sum(your_metric{status_code=200}) / sum(your_metric)) * 100).
  • Repeat this for each status code you want to display.

Option 2: Grouped Ratio (Faster for All Status Codes)

If you want to show all status codes at once without building individual queries:

  • Select your custom metric, set aggregation to Sum, and use Group By to group by your HttpResponseStatus label.
  • Go to the Transform menu (top of the query editor) and select Ratio to aggregation.
  • In the transform settings, set the aggregation to Sum (this will calculate each group’s value as a ratio of the total sum).
  • Multiply the result by 100 to convert the ratio to a percentage (add * 100 in the query editor).

3. Choose the Right Visualization

  • Pie Chart: Perfect for showing a snapshot of current percentage breakdowns (e.g., 95% 200s, 4% 204s, 1% 500s).
  • Stacked Bar/Line Chart: Great if you want to see how the percentage breakdown changes over time (e.g., how 500 errors spiked during a deployment).
  • Make sure to set the resource scope to Global in the query settings to match your log resource type.

Pro Tips

  • Filter out rare status codes (e.g., 1xx) to keep your chart clean—use a filter like HttpResponseStatus IN (200,204,404,500) to focus on what matters.
  • Remember that logs-based metrics have a small delay (usually 1-5 minutes), so give it a minute after deploying changes to see accurate percentages.
  • You can rename each series in the chart settings (e.g., "200 OK" instead of the raw metric label) to make the dashboard more readable for your team.

内容的提问来源于stack exchange,提问作者Jason Hornett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:46:19