慢日志

查询慢日志

  分片级别的慢查询(可分为查询节点和获取阶段)日志可用写入特定的文件,可用针对不同的阶段设置阈值,如下

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

上诉所有的配置都是可用为每个索引进行动态配置的,如下

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,也就是未开启。

   日志作用于分片级别,也就是日志记录的是特定分片的操作日志,并不是这个查询的全部耗时,因为请求可能会被广播给多个或所有分片进行查询。这样做的好处是记录的时间除了与查询条件有关也和特定机器的性能有关。

找到慢查询的原因

  确定是什么导致的慢查询通常是必要且有益的,如果一个请求携带了X-Opaque-ID请求头,那么在记录的日志中未添加一个id字段,内容为该请求头,如下

{
  "type": "index_search_slowlog",
  "timestamp": "2030-08-30T11:59:37,786+02:00",
  "level": "WARN",
  "component": "i.s.s.query",
  "cluster.name": "distribution_run",
  "node.name": "node-0",
  "message": "[index6][0]",
  "took": "78.4micros",
  "took_millis": "0",
  "total_hits": "0 hits",
  "stats": "[]",
  "search_type": "QUERY_THEN_FETCH",
  "total_shards": "1",
  "source": "{\"query\":{\"match_all\":{\"boost\":1.0}}}",
  "id": "MY_USER_ID",
  "cluster.uuid": "Aq-c-PAeQiK3tfBYtig9Bw",
  "node.id": "D7fUYfnfTLa2D7y-xw6tZg"
}

写入操作的慢日志

  写入操作的慢日志作用类似与查询查找的慢日志,log日志的文件名以_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"
}

  默认情况下,Elasticsearch 会记录_source的前1000个字符到慢日志文件,可用通过配置项index.indexing.slowlog.source来修改字段长度,设置为false或0会不记录该项,设置为true则会记录完整的数据。_source数据将会被重新格式化以便在一行显示,如果必须要保留原始的格式,那么可以设置index.indexing.slowlog.reformat为false。日志采用log4j2.properties配置文件。

慢日志级别

  可以通过设置日志的级别来关闭一些长而无用的日志,如我们现在需要index.indexing.slowlog.level = INFO 以上级别的日志,那么我们可以设置index.indexing.slowlog.threshold.index.debug和 index.indexing.slowlog.threshold.index.trace为-1