2021-07-06 17:07:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ServicePing
|
2022-05-13 20:08:57 -04:00
|
|
|
class BuildPayload
|
2021-07-06 17:07:50 -04:00
|
|
|
def execute
|
2022-04-26 11:10:32 -04:00
|
|
|
filtered_usage_data
|
2021-07-06 17:07:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-04-26 11:10:32 -04:00
|
|
|
def raw_payload
|
|
|
|
@raw_payload ||= ::Gitlab::Usage::ServicePingReport.for(output: :all_metrics_values)
|
2021-07-06 17:07:50 -04:00
|
|
|
end
|
|
|
|
|
2022-04-26 11:10:32 -04:00
|
|
|
def filtered_usage_data(payload = raw_payload, parents = [])
|
|
|
|
return unless payload.is_a?(Hash)
|
|
|
|
|
|
|
|
payload.keep_if do |label, node|
|
|
|
|
key_path = parents.dup.append(label).join('.')
|
|
|
|
|
|
|
|
if has_metric_definition?(key_path)
|
|
|
|
include_metric?(key_path)
|
|
|
|
else
|
|
|
|
filtered_usage_data(node, parents.dup << label) if node.is_a?(Hash)
|
|
|
|
end
|
|
|
|
end
|
2021-07-06 17:07:50 -04:00
|
|
|
end
|
|
|
|
|
2022-04-26 11:10:32 -04:00
|
|
|
def include_metric?(key_path)
|
|
|
|
valid_metric_status?(key_path) && permitted_metric?(key_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_metric_status?(key_path)
|
|
|
|
metric_definitions[key_path]&.valid_service_ping_status?
|
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_categories
|
2022-05-13 20:08:57 -04:00
|
|
|
@permitted_categories ||= ::ServicePing::PermitDataCategories.new.execute
|
2022-04-26 11:10:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_metric?(key_path)
|
|
|
|
permitted_categories.include?(metric_category(key_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_metric_definition?(key_path)
|
|
|
|
metric_definitions[key_path].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def metric_category(key_path)
|
|
|
|
metric_definitions[key_path]
|
|
|
|
&.attributes
|
2022-05-13 20:08:57 -04:00
|
|
|
&.fetch(:data_category, ::ServicePing::PermitDataCategories::OPTIONAL_CATEGORY)
|
2022-04-26 11:10:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def metric_definitions
|
|
|
|
@metric_definitions ||= ::Gitlab::Usage::MetricDefinition.definitions
|
2021-07-06 17:07:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-13 20:08:57 -04:00
|
|
|
ServicePing::BuildPayload.prepend_mod_with('ServicePing::BuildPayload')
|