mosowave

sinamon129による(主に)技術ブログ。Ruby,Ruby on Rails,Elasticsearchやその他について書きます。

Elasticsearchのslowlogの設定をした話

最近、日本語全文検索サーバーとしてElasticsearchを使っていて、
たまにクエリのキューが沢山たまってしまうことがあり、
原因になってるクエリを調べたくなりました。

slowlogがあるのは知っていたので見に行ったら空!設定されてなかったorz

今回はそんなslowlogを設定した話です。

Elasticsearchのバージョンは1.7.2です。

elasticsarch.ymlの下の方に

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

こんな項目が!!コメントアウトされてる\(^o^)/
コメントをはずした。

traceとかdebugとかはどこで設定するんだろう?とおもって公式のドキュメントをみていたら、
公式documentlogging.ymlにloggingの出力形式等の設定ができるっぽいので見に行ってみると、ありました。

中略

index.search.slowlog: TRACE, index_search_slow_log_file
index.indexing.slowlog: TRACE, index_indexing_slow_log_file

additivity:
index.search.slowlog: false
index.indexing.slowlog: false


traceのままでよかったので、search.slowlogとindexing.slowlogをtrueにして無事slowlogが吐かれるようになりました。
ログの形式をきめれたりもするので、いい感じに設定するとはかどりそうです。

設定できた感動でログの形式はデフォルトのまま+ログ集積してゴニョりたかったけど、とりあえず各サーバーのログを集めてチェックしたい!!
となったので、
デフォルトの設定で吐かれたログをパースしてcsvに吐く超適当プログラムをrubyで書いた。

require 'csv'

CSV.open('slow_logs.csv', 'wb') do |csv|
  csv << ['date', 'kind', 'node_name', 'index_name', 'shard_number', 'took', 'took_millis', 'source']
    File.open('search_slowlog') do |file|
      file.each_line do |line|
        md = line.match(/\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})\]\[TRACE\]\[(.*)\] \[(.*)\] \[(.*)\]\[(.*)\] took\[(.*)\], took_millis\[(.*)\], types\[\], stats\[\], search_type\[QUERY_THEN_FETCH\], total_shards\[12\], source\[(.*)\], extra_source\[\]/)
        csv << [
          md[1], # date
          md[2], # kind
          md[3], # node_name
          md[4], # index_name
          md[5], # shard_number
          md[6], # took
          md[7], # took millis
          md[8]  # source
        ]
      end
    end
  end
end

これをノードの台数分さまるプログラムをかいて(上記のプログラムに入れた)、スプレッドシートに貼ってソートしてチェック。
ほんとうはfluendとかでいい感じにあつめてkibanaとかでいい感じに見たかった(今後の課題)