Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Report service gateway HTTP response codes
[#49240377]
  • Loading branch information
Chris Brown and Phan Le committed May 31, 2013
1 parent 708d52d commit 4a46b86
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
12 changes: 12 additions & 0 deletions collector/lib/collector/service_gateway_handler.rb
Expand Up @@ -6,6 +6,7 @@ class ServiceGatewayHandler < ServiceHandler
def process(context)
process_plan_score_metric(context)
process_online_nodes(context)
process_response_codes(context)
end

# Sum up all nodes' available_capacity value for each service, report
Expand All @@ -29,11 +30,22 @@ def process_plan_score_metric(context)
end
end

def process_response_codes(context)
varz = context.varz

return unless varz.has_key?("responses_metrics")
varz.fetch("responses_metrics").each do |response_range, counter|
response_code = response_range.split("_")[1]
send_metric("services.http_status.#{response_code}", counter, context)
end
end

# Get online nodes varz for each service gateway, report the total
# number of online nodes
#
def process_online_nodes(context)
varz = context.varz

return unless varz.include?("nodes")
send_metric("services.online_nodes", varz["nodes"].length, context)
end
Expand Down
49 changes: 49 additions & 0 deletions collector/spec/unit/collector/service_gateway_handler_spec.rb
Expand Up @@ -14,6 +14,7 @@
handler = Collector::ServiceGatewayHandler.new(nil, nil)
handler.should_receive(:process_plan_score_metric).with(context)
handler.should_receive(:process_online_nodes).with(context)
handler.should_receive(:process_response_codes).with(context)
handler.process(context)
end
end
Expand Down Expand Up @@ -66,7 +67,55 @@ def self.test_report_metric(metric_name, key, value)
test_report_metric("services.plans.used_capacity", "used_capacity", 50)
test_report_metric("services.plans.max_capacity", "max_capacity", 500)
test_report_metric("services.plans.available_capacity", "available_capacity", 450)
end

describe "response code metrics" do
class FakeHistorian
attr_reader :sent_data

def initialize
@sent_data = []
end

def send_data(data)
@sent_data << data
end

def sent_data?(key, value, tags)
@sent_data.any? do |data|
data[:key] == key && data[:value] == value &&
data[:tags] == data[:tags].merge(tags)
end
end
end

let(:varz) do
{
"responses_metrics" => {
"responses_2xx" => 2,
"responses_3xx" => 3,
"responses_4xx" => 4,
"responses_5xx" => 5,
}
}
end

let(:timestamp) { 1000 }
let(:historian) { FakeHistorian.new }
let(:context) { Collector::HandlerContext.new(1, timestamp, varz) }
let(:handler) { Collector::ServiceGatewayHandler.new(historian, "job") }

it "reports response code metrics to the historian" do
handler.process_response_codes(context)
historian.sent_data?("services.http_status.2xx", 2,
{service_type: "unknown", component: "gateway"}).should == true
historian.sent_data?("services.http_status.3xx", 3,
{service_type: "unknown", component: "gateway"}).should == true
historian.sent_data?("services.http_status.4xx", 4,
{service_type: "unknown", component: "gateway"}).should == true
historian.sent_data?("services.http_status.5xx", 5,
{service_type: "unknown", component: "gateway"}).should == true
end
end

describe :process_online_nodes do
Expand Down

0 comments on commit 4a46b86

Please sign in to comment.