diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-13 03:09:34 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-13 03:09:34 +0300 |
commit | 3cd08f4bf96cda3e9d3abf233095107832b17c20 (patch) | |
tree | dc09a618783a79d70f2a404374d4b850ccf9cc84 /lib/gitlab/elasticsearch | |
parent | dd4bee69b7d55620f7dc9db8c36b478bd4959755 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/elasticsearch')
-rw-r--r-- | lib/gitlab/elasticsearch/logs.rb | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/lib/gitlab/elasticsearch/logs.rb b/lib/gitlab/elasticsearch/logs.rb index eca8b71dd7d..f976f6ce305 100644 --- a/lib/gitlab/elasticsearch/logs.rb +++ b/lib/gitlab/elasticsearch/logs.rb @@ -3,6 +3,8 @@ module Gitlab module Elasticsearch class Logs + InvalidCursor = Class.new(RuntimeError) + # How many log lines to fetch in a query LOGS_LIMIT = 500 @@ -10,7 +12,7 @@ module Gitlab @client = client end - def pod_logs(namespace, pod_name, container_name = nil, search = nil, start_time = nil, end_time = nil) + def pod_logs(namespace, pod_name, container_name: nil, search: nil, start_time: nil, end_time: nil, cursor: nil) query = { bool: { must: [] } }.tap do |q| filter_pod_name(q, pod_name) filter_namespace(q, namespace) @@ -19,7 +21,7 @@ module Gitlab filter_times(q, start_time, end_time) end - body = build_body(query) + body = build_body(query, cursor) response = @client.search body: body format_response(response) @@ -27,8 +29,8 @@ module Gitlab private - def build_body(query) - { + def build_body(query, cursor = nil) + body = { query: query, # reverse order so we can query N-most recent records sort: [ @@ -40,6 +42,12 @@ module Gitlab # fixed limit for now, we should support paginated queries size: ::Gitlab::Elasticsearch::Logs::LOGS_LIMIT } + + unless cursor.nil? + body[:search_after] = decode_cursor(cursor) + end + + body end def filter_pod_name(query, pod_name) @@ -100,7 +108,9 @@ module Gitlab end def format_response(response) - result = response.fetch("hits", {}).fetch("hits", []).map do |hit| + results = response.fetch("hits", {}).fetch("hits", []) + last_result = results.last + results = results.map do |hit| { timestamp: hit["_source"]["@timestamp"], message: hit["_source"]["message"] @@ -108,7 +118,32 @@ module Gitlab end # we queried for the N-most recent records but we want them ordered oldest to newest - result.reverse + { + logs: results.reverse, + cursor: last_result.nil? ? nil : encode_cursor(last_result["sort"]) + } + end + + # we want to hide the implementation details of the search_after parameter from the frontend + # behind a single easily transmitted value + def encode_cursor(obj) + obj.join(',') + end + + def decode_cursor(obj) + cursor = obj.split(',').map(&:to_i) + + unless valid_cursor(cursor) + raise InvalidCursor, "invalid cursor format" + end + + cursor + end + + def valid_cursor(cursor) + cursor.instance_of?(Array) && + cursor.length == 2 && + cursor.map {|i| i.instance_of?(Integer)}.reduce(:&) end end end |