解析摄取 http 日志 api_name 到 elastic

parse ingest http logs api_name to elastic

我有包含字段 url 的 http 日志:"/api/api_name/api_id"

示例 1 url:/api/apiX/0121313123

示例 2 url:/api/apiY/012132/optionX/1000

从 url 中提取并在 elasticsearch 中仅提取“/api/api_name”并删除 id 的最佳做法是什么,以便稍后在每个 [=23 的 kibana 分发中进行可视化=]?

不确定这是否是最佳做法,但对我们有用的是我们将 URL 索引为一个单独的字段,仅为 API:

DELETE urls

PUT /urls
{
  "settings": {
    "analysis": {
      "char_filter": {
        "api_extractor_char_filter": {
          "type": "pattern_replace",
          "pattern": "/?api/([^/]+)/?.*",
          "replacement": "api/"
        }
      },
      "normalizer": {
        "api_extractor": {
          "filter": [
            "lowercase",
            "asciifolding"
          ],
          "char_filter": [
            "api_extractor_char_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "url": {
        "type": "text",
        "fields": {
          "api": {
            "type": "keyword",
            "normalizer": "api_extractor"
          }
        }
      }
    }
  }
}

POST /urls/_doc
{"url":"/api/apiX/0121313123"}
POST /urls/_doc
{"url":"/api/apiY/012132/optionX/1000"}

GET /urls/_search
{
  "query": {
    "term": {
      "url.api": {
        "value": "/api/apiY"
      }
    }
  }
}

这样我们就可以保留原始的 URL 和 .api 字段索引,只保留您需要的内容。该字段可用于精确搜索和聚合。它适用于您的用例。

其他可能的方式:

  • 使用摄取管道更改文档源并提取 URL
  • 从您的 application/logs 收集器中分别提取 URL 和 API 名称