【问题标题】:How to evaluate time between log messages with ElasticSearch如何使用 ElasticSearch 评估日志消息之间的时间
【发布时间】:2019-01-20 03:19:54
【问题描述】:

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

LOGSTASH 过滤器

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 中的持续时间。

欢迎提出任何建议!

【问题讨论】:

标签: elasticsearch logstash elasticsearch-aggregation


【解决方案1】:

感谢 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 字段来可视化每个请求所花费的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多