Module: ZipkinQuery::Client

Defined in:
lib/zipkin-query.rb

Constant Summary collapse

NODE_PATH =

The node location in zookeeper that stores the cassandra location

"/twitter/service/zipkin/query"

Class Method Summary collapse

Class Method Details

.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

.with_transport(opts = {}) ⇒ Object



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
66
67
68
69
70
71
72
73
# File 'lib/zipkin-query.rb', line 29

def self.with_transport(opts = {})
  # Open a connection to a thrift server, do something, and close the connection

  begin

    if opts[:use_local_server]
      # If we're running local (development mode) return a set value
      host = "localhost"
      port = 9149

      # Create the connection to the local b3 query_daemon which uses different
      # transport mechanism
      socket = Thrift::Socket.new(host, port)
      transport = Thrift::BufferedTransport.new(socket)
    else
      # Get the host and port of the location of the query service from zookeeper
      zk_host = opts[:zk_host] || "localhost"
      zk_port = opts[:zk_port] || 2181

      host, port = Client::get_query_service(zk_host, zk_port, opts)

      # Create the connection to the b3 query_daemon
      socket = Thrift::Socket.new(host, port)
      buffered_tp = Thrift::BufferedTransport.new(socket)
      transport = Thrift::FramedTransport.new(buffered_tp)
    end

    protocol = Thrift::BinaryProtocol.new(transport)
    client = ThriftClient.new(Zipkin::ZipkinQuery::Client, host + ':' + port.to_s, :retries => 0, :timeout => 60)

    # set up tracing for the client we use to talk to the query daemon
    client_id = FinagleThrift::ClientId.new(:name => "zipkin.prod")
    FinagleThrift.enable_tracing!(client, client_id, "zipkin")

    begin
      transport.open
      yield(client)
    ensure
      transport.close
    end
  rescue ZookeeperExceptions::ZookeeperException::ConnectionClosed => ze
    "Could not connect to zookeeper at #{opts[:zk_host]}:#{opts[:zk_port]}"
  end

end