forked from zendesk/ruby-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_datadog_agent.rb
65 lines (53 loc) · 1.34 KB
/
fake_datadog_agent.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
require "socket"
require "tempfile"
class FakeDatadogAgent
attr_reader :host, :port, :socket_path, :metrics
def initialize
@host = "127.0.0.1"
@port = 9999
@socket_path = Tempfile.open("fake_datadog_agent_sock") do |f|
path = f.path
f.unlink
path
end
@udp_socket = UDPSocket.new
@uds_socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
@threads = []
@metrics = []
end
def start
@udp_socket.bind(@host, @port)
@uds_socket.bind(Addrinfo.unix(@socket_path))
@threads << Thread.new { loop { receive_from_udp_socket } }
@threads << Thread.new { loop { receive_from_uds_socket } }
@threads.each { |th| th.abort_on_exception = true }
end
def stop
@threads.each(&:kill)
@udp_socket.close
@uds_socket.close
@threads = []
end
def wait_for_metrics(count: 1)
deadline = Time.now + 10
until @metrics.count >= count || Time.now > deadline
sleep 0.1
end
end
private
def receive_from_udp_socket
data, _ = @udp_socket.recvfrom(512)
add_metrics(data)
end
def receive_from_uds_socket
data, _ = @uds_socket.recvfrom(512)
add_metrics(data)
end
def add_metrics(data)
data.split("\n").each do |message|
metric = message.split(":").first
@metrics << metric if metric
end
end
end