如何使用ElasticSearch

时间:2018-08-13 11:42:31

标签: elasticsearch logstash elasticsearch-aggregation

我想弄清楚我的旧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格式。

日志存储过滤器

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

然后,结果由ElasticSearch索引,但是我不知道如何找出每个Action需要多长时间。基于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的持续时间。

欢迎提出任何建议!

1 个答案:

答案 0 :(得分: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字段来可视化每个请求花费的时间。

相关问题