2020-03-11 11:09:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PodLogs
|
2020-03-11 23:10:15 -04:00
|
|
|
class ElasticsearchService < PodLogs::BaseService
|
2020-03-11 11:09:37 -04:00
|
|
|
steps :check_arguments,
|
|
|
|
:get_raw_pods,
|
|
|
|
:get_pod_names,
|
|
|
|
:check_times,
|
|
|
|
:check_search,
|
2020-03-12 20:09:34 -04:00
|
|
|
:check_cursor,
|
2020-03-11 11:09:37 -04:00
|
|
|
:pod_logs,
|
|
|
|
:filter_return_keys
|
|
|
|
|
|
|
|
self.reactive_cache_worker_finder = ->(id, _cache_key, namespace, params) { new(::Clusters::Cluster.find(id), namespace, params: params) }
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid_params
|
2020-03-23 20:09:24 -04:00
|
|
|
super + %w(search start_time end_time cursor)
|
2020-03-12 20:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def success_return_keys
|
|
|
|
super + %i(cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
2020-04-15 14:09:36 -04:00
|
|
|
def get_raw_pods(result)
|
|
|
|
client = cluster&.application_elastic_stack&.elasticsearch_client
|
|
|
|
return error(_('Unable to connect to Elasticsearch')) unless client
|
|
|
|
|
|
|
|
result[:raw_pods] = ::Gitlab::Elasticsearch::Logs::Pods.new(client).pods(namespace)
|
|
|
|
|
|
|
|
success(result)
|
|
|
|
rescue Elasticsearch::Transport::Transport::ServerError => e
|
|
|
|
::Gitlab::ErrorTracking.track_exception(e)
|
|
|
|
|
|
|
|
error(_('Elasticsearch returned status code: %{status_code}') % {
|
|
|
|
# ServerError is the parent class of exceptions named after HTTP status codes, eg: "Elasticsearch::Transport::Transport::Errors::NotFound"
|
|
|
|
# there is no method on the exception other than the class name to determine the type of error encountered.
|
|
|
|
status_code: e.class.name.split('::').last
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
def check_times(result)
|
2020-03-23 20:09:24 -04:00
|
|
|
result[:start_time] = params['start_time'] if params.key?('start_time') && Time.iso8601(params['start_time'])
|
|
|
|
result[:end_time] = params['end_time'] if params.key?('end_time') && Time.iso8601(params['end_time'])
|
2020-03-11 11:09:37 -04:00
|
|
|
|
|
|
|
success(result)
|
|
|
|
rescue ArgumentError
|
|
|
|
error(_('Invalid start or end time format'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_search(result)
|
|
|
|
result[:search] = params['search'] if params.key?('search')
|
|
|
|
|
2020-05-07 05:09:51 -04:00
|
|
|
return error(_('Invalid search parameter')) if result[:search] && !result[:search].is_a?(String)
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
2020-03-12 20:09:34 -04:00
|
|
|
def check_cursor(result)
|
|
|
|
result[:cursor] = params['cursor'] if params.key?('cursor')
|
|
|
|
|
2020-05-07 05:09:51 -04:00
|
|
|
return error(_('Invalid cursor parameter')) if result[:cursor] && !result[:cursor].is_a?(String)
|
|
|
|
|
2020-03-12 20:09:34 -04:00
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
def pod_logs(result)
|
|
|
|
client = cluster&.application_elastic_stack&.elasticsearch_client
|
|
|
|
return error(_('Unable to connect to Elasticsearch')) unless client
|
|
|
|
|
2020-05-12 20:07:50 -04:00
|
|
|
chart_above_v2 = cluster.application_elastic_stack.chart_above_v2?
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-04-15 14:09:36 -04:00
|
|
|
response = ::Gitlab::Elasticsearch::Logs::Lines.new(client).pod_logs(
|
2020-03-11 11:09:37 -04:00
|
|
|
namespace,
|
2020-03-17 23:09:43 -04:00
|
|
|
pod_name: result[:pod_name],
|
2020-03-12 20:09:34 -04:00
|
|
|
container_name: result[:container_name],
|
|
|
|
search: result[:search],
|
2020-03-23 20:09:24 -04:00
|
|
|
start_time: result[:start_time],
|
|
|
|
end_time: result[:end_time],
|
2020-04-21 11:21:10 -04:00
|
|
|
cursor: result[:cursor],
|
2020-05-12 20:07:50 -04:00
|
|
|
chart_above_v2: chart_above_v2
|
2020-03-11 11:09:37 -04:00
|
|
|
)
|
|
|
|
|
2020-03-12 20:09:34 -04:00
|
|
|
result.merge!(response)
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
success(result)
|
|
|
|
rescue Elasticsearch::Transport::Transport::ServerError => e
|
|
|
|
::Gitlab::ErrorTracking.track_exception(e)
|
|
|
|
|
|
|
|
error(_('Elasticsearch returned status code: %{status_code}') % {
|
|
|
|
# ServerError is the parent class of exceptions named after HTTP status codes, eg: "Elasticsearch::Transport::Transport::Errors::NotFound"
|
|
|
|
# there is no method on the exception other than the class name to determine the type of error encountered.
|
|
|
|
status_code: e.class.name.split('::').last
|
|
|
|
})
|
2020-04-15 14:09:36 -04:00
|
|
|
rescue ::Gitlab::Elasticsearch::Logs::Lines::InvalidCursor
|
2020-03-12 20:09:34 -04:00
|
|
|
error(_('Invalid cursor value provided'))
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|