Skip to content

Commit

Permalink
Check metrics auth valid before perf_capture_queue
Browse files Browse the repository at this point in the history
Check that the EMS's metrics authentication is valid before queueing
perf_capture
  • Loading branch information
agrare committed Jun 12, 2023
1 parent 7be0a73 commit be53bdc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def log_severity
}
}

def capture_ems_targets(_options = {})
begin
verify_metrics_connection!(ems)
rescue TargetValidationError, TargetValidationWarning => e
_log.send(e.log_severity, e.message)
return []
end

super
end

def prometheus_capture_context(target, start_time, end_time)
PrometheusCaptureContext.new(target, start_time, end_time, INTERVAL)
end
Expand All @@ -57,6 +68,19 @@ def metrics_connection(ems)
ems.connection_configurations.prometheus
end

def metrics_connection_valid?(ems)
metrics_connection(ems)&.authentication&.status == "Valid"
end

def verify_metrics_connection!(ems)
t = target || ems
target_name = "#{t.class.name.demodulize}(#{t.id})"
raise TargetValidationError, "no provider for #{target_name}" if ems.nil?

raise TargetValidationWarning, "no metrics endpoint found for #{target_name}" if metrics_connection(ems).nil?
raise TargetValidationWarning, "metrics authentication isn't valid for #{target_name}" unless metrics_connection_valid?(ems)
end

def capture_context(_ems, target, start_time, end_time)
# make start_time align to minutes
start_time = start_time.beginning_of_minute
Expand All @@ -73,11 +97,7 @@ def perf_collect_metrics(interval_name, start_time = nil, end_time = nil)
"[#{start_time}] [#{end_time}]")

begin
raise TargetValidationError, "no provider for #{target_name}" if ems.nil?

connection = metrics_connection(ems)
raise TargetValidationWarning, "no metrics endpoint found for #{target_name}" if connection.nil?
raise TargetValidationWarning, "metrics authentication isn't valid for #{target_name}" unless connection.authentication&.status == "Valid"
verify_metrics_connection!(ems)

context = capture_context(ems, target, start_time, end_time)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@
end
end

context "#perf_capture_all_queue" do
it "returns the objects" do
expect(@ems_kubernetes.perf_capture_object.perf_capture_all_queue).to include("Container" => [@container], "ContainerNode" => [@node])
end

context "with a missing metrics endpoint" do
before do
@ems_kubernetes.endpoints.find_by(:role => "prometheus").destroy
end

it "returns no objects" do
expect(@ems_kubernetes.perf_capture_object.perf_capture_all_queue).to be_empty
end
end

context "with invalid authentication on the metrics endpoint" do
before do
@ems_kubernetes.authentications.find_by(:authtype => "prometheus").update!(:status => "Error")
@ems_kubernetes.reload
end

it "returns no objects" do
expect(@ems_kubernetes.perf_capture_object.perf_capture_all_queue).to be_empty
end
end
end

context "#perf_collect_metrics" do
it "fails when no ems is defined" do
@node.ext_management_system = nil
Expand Down

0 comments on commit be53bdc

Please sign in to comment.