Method: ZipkinQuery::Client.with_transport

Defined in:
lib/zipkin-query.rb

.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