【问题标题】:parse ingest http logs api_name to elastic将摄取 http 日志 api_name 解析为弹性
【发布时间】:2021-04-09 11:36:24
【问题描述】:
我有带有字段 url:"/api/api_name/api_id" 的 http 日志
example1 网址:/api/apiX/0121313123
example2 网址:/api/apiY/012132/optionX/1000
从 url 中提取并仅在 elasticsearch 中提取“/api/api_name”并删除 id 以便以后在每个 api_name 的 kibana 分布中可视化的最佳做法是什么?
【问题讨论】:
标签:
elasticsearch
elastic-stack
【解决方案1】:
不确定这是否是最佳做法,但对我们有用的是我们将 URL 索引为仅用于 API 的单独字段:
DELETE urls
PUT /urls
{
"settings": {
"analysis": {
"char_filter": {
"api_extractor_char_filter": {
"type": "pattern_replace",
"pattern": "/?api/([^/]+)/?.*",
"replacement": "api/$1"
}
},
"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
- 从您的应用程序/日志收集器中分别提取 URL 和 API 名称