首页 文章

如何使用ElasticSearch评估日志消息之间的时间

提问于
浏览
0

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

LOGFILE

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和grok过滤器解析日志,以获得ElasticSearch可以理解的JSON格式 .

LOGSTASH FILTER

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

结果然后由ElasticSearch索引,但我不知道如何找出每个Action需要多长时间 . 基于pid,控制器的名称以及操作的名称和开始/结束状态,我获得了查找操作所需时间所需的所有信息 . 我想在Kibana中显示每个操作的持续时间,但我首先尝试通过查询从索引中获取数据 . 我认为他们可能适合这种任务 .

我创建了以下查询:

ES QUERY

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

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

欢迎任何建议!

1 回答

  • 0

    感谢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 字段来显示每个请求所需的时间 .

相关问题