慢日志

edit

搜索慢日志

edit

分片级别的慢搜索日志允许将慢搜索(查询和获取阶段)记录到专用日志文件中。

可以为执行的查询阶段和获取阶段设置阈值,这里是一个示例:

index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms

index.search.slowlog.threshold.fetch.warn: 1s
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug: 500ms
index.search.slowlog.threshold.fetch.trace: 200ms

上述所有设置都是动态的,可以使用更新索引设置 API 为每个索引进行设置。例如:

PUT /my-index-000001/_settings
{
  "index.search.slowlog.threshold.query.warn": "10s",
  "index.search.slowlog.threshold.query.info": "5s",
  "index.search.slowlog.threshold.query.debug": "2s",
  "index.search.slowlog.threshold.query.trace": "500ms",
  "index.search.slowlog.threshold.fetch.warn": "1s",
  "index.search.slowlog.threshold.fetch.info": "800ms",
  "index.search.slowlog.threshold.fetch.debug": "500ms",
  "index.search.slowlog.threshold.fetch.trace": "200ms"
}

默认情况下,阈值是禁用的(设置为 -1)。

日志记录是在分片级别范围内进行的,这意味着在特定分片内执行搜索请求。它并不包括整个搜索请求,因为搜索请求可能会被广播到多个分片以执行。分片级别日志记录的一些好处是,与请求级别相比,可以关联到特定机器上的实际执行。

搜索慢日志文件在 log4j2.properties 文件中配置。

识别搜索慢日志来源

edit

识别是什么触发了慢查询通常很有用。 要包含触发慢搜索的用户的详细信息, 请使用 index.search.slowlog.include.user 设置。

PUT /my-index-000001/_settings
{
  "index.search.slowlog.include.user": true
}

这将会导致用户信息被包含在慢日志中。

{
  "@timestamp": "2024-02-21T12:42:37.255Z",
  "log.level": "WARN",
  "auth.type": "REALM",
  "elasticsearch.slowlog.id": "tomcat-123",
  "elasticsearch.slowlog.message": "[index6][0]",
  "elasticsearch.slowlog.search_type": "QUERY_THEN_FETCH",
  "elasticsearch.slowlog.source": "{\"query\":{\"match_all\":{\"boost\":1.0}}}",
  "elasticsearch.slowlog.stats": "[]",
  "elasticsearch.slowlog.took": "747.3micros",
  "elasticsearch.slowlog.took_millis": 0,
  "elasticsearch.slowlog.total_hits": "1 hits",
  "elasticsearch.slowlog.total_shards": 1,
  "user.name": "elastic",
  "user.realm": "reserved",
  "ecs.version": "1.2.0",
  "service.name": "ES_ECS",
  "event.dataset": "elasticsearch.index_search_slowlog",
  "process.thread.name": "elasticsearch[runTask-0][search][T#5]",
  "log.logger": "index.search.slowlog.query",
  "elasticsearch.cluster.uuid": "Ui23kfF1SHKJwu_hI1iPPQ",
  "elasticsearch.node.id": "JK-jn-XpQ3OsDUsq5ZtfGg",
  "elasticsearch.node.name": "node-0",
  "elasticsearch.cluster.name": "distribution_run"
}

如果一个调用是通过带有X-Opaque-ID头发起的,那么该ID将包含在搜索慢日志的elasticsearch.slowlog.id字段中。详情和最佳实践请参见X-Opaque-Id HTTP头

索引慢日志

edit

索引慢日志,功能类似于搜索慢日志。日志文件名以 _index_indexing_slowlog.json 结尾。日志和阈值的配置方式与搜索慢日志相同。 索引慢日志示例:

index.indexing.slowlog.threshold.index.warn: 10s
index.indexing.slowlog.threshold.index.info: 5s
index.indexing.slowlog.threshold.index.debug: 2s
index.indexing.slowlog.threshold.index.trace: 500ms
index.indexing.slowlog.source: 1000

以上所有设置都是动态的,可以使用更新索引设置 API 为每个索引进行设置。例如:

PUT /my-index-000001/_settings
{
  "index.indexing.slowlog.threshold.index.warn": "10s",
  "index.indexing.slowlog.threshold.index.info": "5s",
  "index.indexing.slowlog.threshold.index.debug": "2s",
  "index.indexing.slowlog.threshold.index.trace": "500ms",
  "index.indexing.slowlog.source": "1000"
}

要包含触发慢索引事件的用户信息,请使用index.indexing.slowlog.include.user设置。

PUT /my-index-000001/_settings
{
  "index.indexing.slowlog.include.user": true
}

默认情况下,Elasticsearch 会在慢日志中记录 _source 的前 1000 个字符。你可以通过 index.indexing.slowlog.source 来更改此设置。将其设置为 false0 将完全跳过记录源,而将其设置为 true 将记录整个源,无论其大小如何。原始的 _source 默认会被重新格式化,以确保它适合单个日志行。如果保留原始文档格式很重要,你可以通过将 index.indexing.slowlog.reformat 设置为 false 来关闭重新格式化,这将导致源以“原样”记录,并可能跨越多个日志行。

索引慢日志文件在 log4j2.properties 文件中配置。

慢日志级别

edit

您可以通过设置适当的阈值来模拟搜索或索引慢日志级别,从而关闭“更详细”的日志记录器。 例如,如果我们想要模拟 index.indexing.slowlog.level: INFO, 那么我们只需要将 index.indexing.slowlog.threshold.index.debugindex.indexing.slowlog.threshold.index.trace 设置为 -1