首页 > 解决方案 > 用于计算 API 的 http 状态百分比的 Elasticsearch 查询

问题描述

我需要计算 API 的每个状态的百分比。

参考线程:Elasticsearch 查询以计算每个 API 的命中数

在上述线程的帮助下,我能够获得前 5 个 API 的状态计数。除此之外,我还想计算百分比。

目前我有这样的查询

{
"query": {
    "bool": {
        "must": [
            {
                "range": {
                    "@timestamp": {
                        "from": "now-15m",
                        "to": "now",
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                    }
                }
            }
        ],
        "adjust_pure_negative": true,
        "boost": 1
    }
},
"aggregations": {
    "Url": {
        "terms": {
            "field": "data.url.keyword",
            "size": 5,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
                {
                    "_count": "desc"
                },
                {
                    "_key": "asc"
                }
            ]
        },
        "aggregations": {
            "Status": {
                "terms": {
                    "field": "data.response.status",
                    "size": 5,
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": [
                        {
                            "_count": "desc"
                        },
                        {
                            "_key": "asc"
                        }
                    ]
                }
            }
        }
    }
}
}

我得到如下输出

"aggregations": {
    "Url": {
        "doc_count_error_upper_bound": 940,
        "sum_other_doc_count": 52374,
        "buckets": [
            {
                "Status": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "doc_count": 3261,
                            "key": 200
                        },
                        {
                            "doc_count": 254,
                            "key": 400
                        }
                    ]
                },
                "doc_count": 3515,
                "key": "/account/me"
            },
            {
                "Status": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "doc_count": 3376,
                            "key": 200
                        }
                    ]
                },
                "doc_count": 3385,
                "key": "/PlanDetails"
            },
            {
                "Status": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "doc_count": 3282,
                            "key": 200
                        }
                    ]
                },
                "doc_count": 3282,
                "key": "/evaluation"
            },
            {
                "Status": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "doc_count": 3205,
                            "key": 200
                        }
                    ]
                },
                "doc_count": 3205,
                "key": "/user/me"
            },
            {
                "Status": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "doc_count": 3055,
                            "key": 200
                        }
                    ]
                },
                "doc_count": 3055,
                "key": "/user"
            }
        ]
    }
}
}

所以我可以获得我的前 5 个热门 API 以及它们的状态和计数。但我也想获得每个 API 的状态百分比

像这样的东西

API               
/search/results  200 : 30(89%) 201: 10(10%) 500:1(1%)
/eligibility     200 : 20(90%) 500 : 3(10%)

任何帮助,将不胜感激。

标签: elasticsearchkibanaelasticsearch-aggregation

解决方案


这种计算应该由客户端完成。

例如,在响应的以下片段中:

            {
                "Status": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "doc_count": 3261, <--- divide this
                            "key": 200
                        },
                        {
                            "doc_count": 254, <---- or this 
                            "key": 400
                        }
                    ]
                },
                "doc_count": 3515, <--- by this
                "key": "/account/me"
            },

使用每个 URL doc_count,您可以计算如下数字:

API               
/account/me  200 : 3261(93%) 400: 254(7%)

推荐阅读