Skip to content

Commit af5ef2f

Browse files
committed
Test the Datadog reporter
1 parent 418526d commit af5ef2f

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

lib/kafka/datadog.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ def self.statsd
3131
@statsd ||= Statsd.new(Statsd::DEFAULT_HOST, Statsd::DEFAULT_HOST, namespace: STATSD_NAMESPACE)
3232
end
3333

34-
def self.statsd_host=(host)
34+
def self.host=(host)
3535
statsd.host = host
3636
end
3737

38-
def self.statsd_port=(port)
38+
def self.port=(port)
3939
statsd.port = port
4040
end
4141

spec/fake_datadog_agent.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "socket"
2+
3+
class FakeDatadogAgent
4+
attr_reader :host, :port, :metrics
5+
6+
def initialize
7+
@host = "127.0.0.1"
8+
@port = 9999
9+
@socket = UDPSocket.new
10+
@thread = nil
11+
@metrics = []
12+
13+
@socket.bind(@host, @port)
14+
end
15+
16+
def start
17+
@thread = Thread.new { loop { receive } }
18+
@thread.abort_on_exception = true
19+
end
20+
21+
def wait_for_metrics
22+
sleep 0.1 until @metrics.count > 0
23+
end
24+
25+
private
26+
27+
def receive
28+
data, sender = @socket.recvfrom(512)
29+
30+
data.split("\n").each do |message|
31+
metric = message.split(":").first
32+
@metrics << metric if metric
33+
end
34+
end
35+
end

spec/functional/datadog_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "kafka/datadog"
2+
require "socket"
3+
require "fake_datadog_agent"
4+
5+
describe "Reporting metrics to Datadog", functional: true do
6+
example "reporting connection metrics" do
7+
agent = FakeDatadogAgent.new
8+
9+
Kafka::Datadog.port = agent.port
10+
11+
agent.start
12+
13+
kafka.topics
14+
15+
agent.wait_for_metrics
16+
17+
expect(agent.metrics).to include("ruby_kafka.api.calls")
18+
expect(agent.metrics).to include("ruby_kafka.api.latency")
19+
expect(agent.metrics).to include("ruby_kafka.api.request_size")
20+
expect(agent.metrics).to include("ruby_kafka.api.response_size")
21+
end
22+
end

0 commit comments

Comments
 (0)