Method: ZipkinQuery::Client.get_query_service

Defined in:
lib/zipkin-query.rb

.get_query_service(zk_host, zk_port, opts = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zipkin-query.rb', line 75

def self.get_query_service(zk_host, zk_port, opts={})
  # Takes either:
  # - ZooKeeper config options that map to a Zipkin Query server set OR
  # - Direct host/port of a Query daemon

  if opts[:skip_zookeeper]
    return [ opts[:zipkin_query_host], opts[:zipkin_query_port] ]
  end

  node_path = opts[:node_path] || NODE_PATH

  # TODO: throw error if it fails
  zk = Zookeeper.new("#{zk_host}:#{zk_port}")

  begin
    # TODO: handle errors here
    children = zk.get_children(:path => node_path)
    node_key = children[:children][0]

    # TODO: throw errors
    node = zk.get(:path => "#{node_path}/#{node_key}")
  ensure
    zk.close() if zk
  end

  # Deserialize the result
  d = Thrift::Deserializer.new
  si = d.deserialize(Twitter::Thrift::ServiceInstance.new, node[:data])

  # Return the host and port
  [si.serviceEndpoint.host, si.serviceEndpoint.port]
end