ElasticSearch查询,计算接口的http状态百分比 [英] Elasticsearch query to calculate percentage of http statuses of API

查看:29
本文介绍了ElasticSearch查询,计算接口的http状态百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

引用线程:Elasticsearch query to count number of hits for each 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               
/search/results  200 : 30(89%) 201: 10(10%) 500:1(1%)
/eligibility     200 : 20(90%) 500 : 3(10%)

如有任何帮助,我们将不胜感激。

推荐答案

此类计算应由客户端执行。

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

            {
                "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"
            },

使用每URLdoc_count可以计算如下数字:

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

这篇关于ElasticSearch查询,计算接口的http状态百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆