Module: ILove::Tracing::Twirp

Defined in:
lib/ilove/tracing/twirp.rb

Class Method Summary collapse

Class Method Details

.setup(cfg) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/ilove/tracing/twirp.rb', line 4

def self.setup(cfg)
  raise 'Cannot trace twirp without twirp_rails gem' unless defined?(::TwirpRails)

  ::TwirpRails::Routes::Helper.on_create_service do |service|
    trace_service service
  end
end

.trace_service(service) ⇒ Object



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
# File 'lib/ilove/tracing/twirp.rb', line 12

def self.trace_service(service)
  scope = nil

  service.before do |_rack_env, env|
    method_name = env[:rpc_method]

    scope = OpenTracing.start_active_span 'twirp call',
                                          child_of: OpenTracing.active_span,
                                          tags: { service: service.full_name, method: method_name }
  end

  service.on_error do |err, _env|
    if scope
      scope.span.set_tag :error_code, err.code
      scope.span.set_tag :error_msg, err.msg
      scope.close
      scope = nil
    end
  end

  service.on_success do |_env|
    if scope
      scope.close
      scope = nil
    end
  end

  service.exception_raised do |e, _env|
    if scope
      scope.span.set_tag :exception, e.class.name
      scope.close
      scope = nil
    end
  end
end