Class: OpenTracing::Instrumentation::Thrift::TracedProtocol

Inherits:
Thrift::BaseProtocol
  • Object
show all
Extended by:
Forwardable
Includes:
Thrift::ProtocolDecorator
Defined in:
lib/opentracing/instrumentation/thrift/traced_protocol.rb

Overview

TracedProtocol wrap any raw Thrift protocol instance. Not thread safe!

Usage (without multiplexed):

buffered_transport = ::Thrift::BufferedTransport.new(transport)
protocol = ::Thrift::BinaryProtocol.new(buffered_transport)
traced_protocol = \
  OpenTracing::Instrumentation::Thrift::TracedProtocol.new(protocol)

Usage (multiplexed):

buffered_transport = ::Thrift::BufferedTransport.new(transport)
protocol = ::Thrift::BinaryProtocol.new(buffered_transport)
traced_protocol =
  OpenTracing::Instrumentation::Thrift::TracedProtocol
    .new(protocol)
multiplexed_protocol =
  ::Thrift::MultiplexedProtocol
    .new(traced_protocol, 'OrderService')

Constant Summary collapse

WRITE_DIRECTION =
'write'
READ_DIRECTION =
'read'

Instance Method Summary collapse

Constructor Details

#initialize(protocol, config: TracedProtocolConfig.new) {|@config| ... } ⇒ TracedProtocol

Returns a new instance of TracedProtocol.

Yields:

  • (@config)


32
33
34
35
36
37
# File 'lib/opentracing/instrumentation/thrift/traced_protocol.rb', line 32

def initialize(protocol, config: TracedProtocolConfig.new)
  super(protocol)
  @config = config.dup
  yield @config if block_given?
  @protocol_tags = config.tags_builder.build_protocol_tags(protocol)
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
42
43
# File 'lib/opentracing/instrumentation/thrift/traced_protocol.rb', line 39

def ==(other)
  protocol == other.protocol &&
    config == other.config &&
    protocol_tags == other.protocol_tags
end

#read_message_beginObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opentracing/instrumentation/thrift/traced_protocol.rb', line 64

def read_message_begin
  start_time = Time.now.utc

  name, type, rseqid = super

  self.read_span = \
    safe_start_span(READ_DIRECTION, name, type,
                    start_time: start_time)

  [name, type, rseqid]
end

#read_message_endObject



76
77
78
79
80
# File 'lib/opentracing/instrumentation/thrift/traced_protocol.rb', line 76

def read_message_end
  super
ensure
  safe_close_span(read_span)
end

#write_message_begin(name, type, seqid) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/opentracing/instrumentation/thrift/traced_protocol.rb', line 45

def write_message_begin(name, type, seqid)
  self.write_span = \
    safe_start_span(WRITE_DIRECTION, name, type)

  # Call protocol instaed super, beacaus ProtocolDecorator do not
  # pass arguments to protocol.write_message_begin
  protocol.write_message_begin(name, type, seqid)
rescue StandardError => e
  write_error(write_span, e)
  safe_close_span(write_span)
  raise e
end

#write_message_endObject



58
59
60
61
62
# File 'lib/opentracing/instrumentation/thrift/traced_protocol.rb', line 58

def write_message_end
  super
ensure
  safe_close_span(write_span)
end