如何使用ElasticSearch评估日志消息之间的时间 [英] How to evaluate time between log messages with ElasticSearch

查看:17
本文介绍了如何使用ElasticSearch评估日志消息之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的旧PHP web应用程序中的不同操作需要多长时间。有一个日志文件,它在操作开始和结束时写出消息。它看起来像这样。

日志文件

2018-08-13 13:05:07,217 [30813] ControllerA: actionA start
2018-08-13 13:05:07,280 [30813] ControllerA: actionA end
2018-08-13 13:05:08,928 [30813] ControllerB: actionA start
2018-08-13 13:05:08,942 [30813] ControllerB: actionA end
2018-08-13 13:05:09,035 [17685] ControllerC: actionA start
2018-08-13 13:05:09,049 [17685] ControllerC: actionA end
2018-08-13 13:05:09,115 [8885] ControllerB: actionB start
2018-08-13 13:05:09,128 [8885] ControllerB: actionB end

我用logstash和过滤解析了日志,得到了ElasticSearch可以理解的json格式。

LOGSTASH过滤

grok {
    match => { "message" => "%{EXIM_DATE:timestamp} [%{NUMBER:pid}] %{WORD:controller}: %{WORD:action} %{WORD:status}" }
}

结果随后由ElasticSearch编制索引,但我不知道如何才能知道每个操作需要多长时间。根据PID、控制器名称、操作名称和开始/结束状态,我掌握了确定操作需要多长时间所需的所有信息。 我希望显示Kibana中每个操作的持续时间,但我首先尝试使用查询从索引中获取数据。我read about aggregations,觉得它们可能适合这类任务。

我创建了以下查询:

ES查询

{
    "aggs":{
        "group_by_pid": {
            "terms": {
                "field": "pid"
            }
        },
        "aggs": {
            "group_by_controller": {
                "terms": {
                    "field": "controller"
                }
            }
            "aggs": {
                "group_by_action": {
                    "terms":{
                        "field": "action"
                    }
                }
            }
        }
    }
}

但响应始终为空。我目前甚至不确定是否可以在每个开始和结束操作之间进行计算,或者是否必须更新完整的日志记录并以PHP计算持续时间。

欢迎提出任何建议!

推荐答案

多亏了val和his response to another question的提示,我成功地使用logstash获得了不同日志事件的聚合时间。

这是配置:

input {
    file {
        path => "path/to/log.log"
    }
}
filter {
    grok {
        match => { "message" => "%{EXIM_DATE:timestamp} [%{NUMBER:pid}] %{WORD:controller}: %{WORD:action} %{WORD:status}" }
        add_tag => [ "%{status}" ]
    }

    elapsed {
        unique_id_field => "pid"
        start_tag => "start"
        end_tag => "end"
        new_event_on_match => false
    }

    if "elapsed" in [tags] {
        aggregate {
            task_id => "%{pid}"
            code => "map['duration'] = [(event.get('elapsed_time')*1000).to_i]"
            map_action => "create"
        }
    }
}
output {
    elasticsearch {
        hosts => [ "localhost:9200" ]
        index => "my_index_%{+xxxx_M}"
        action => "index"
    }
}

在Kibana中,我现在可以使用elapsed-filter创建的elapsed_time字段可视化每个请求所需的时间。

这篇关于如何使用ElasticSearch评估日志消息之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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