Module: SignalFx::Tracing::Instrumenter::Elasticsearch

Defined in:
lib/signalfx/tracing/instrumentation/elasticsearch.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.instrumentedObject (readonly)

Returns the value of attribute instrumented.



10
11
12
# File 'lib/signalfx/tracing/instrumentation/elasticsearch.rb', line 10

def instrumented
  @instrumented
end

Class Method Details

.instrument(opts = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/signalfx/tracing/instrumentation/elasticsearch.rb', line 12

def instrument(opts = {})
  return if @instrumented

  begin
    require 'elasticsearch'
  rescue LoadError
    return
  end

  begin
    require 'elasticsearch-tracer'
  rescue LoadError => e
    puts e.message
    return
  end

  patch_new if opts.fetch(:auto_instrument, false)

  # prevent re-instrumenting
  @instrumented = true
end

.patch_newObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/signalfx/tracing/instrumentation/elasticsearch.rb', line 34

def patch_new
  ::Elasticsearch::Client.module_eval do
    alias_method :new_original, :new

    def new(arguments = {}, &block)
      # create a new TracingClient, which is almost identical to the
      # default client, and add the tracing transport afterwards. This
      # allows us to maintain the original transport if the user has
      # specified a non-default transport
      client = ::Elasticsearch::Tracer::TracingClient.new(arguments, &block)
      client.transport = ::Elasticsearch::Tracer::Transport.new(tracer: OpenTracing.global_tracer,
                                                                active_span: -> { OpenTracing.global_tracer.active_span },
                                                                transport: client.transport)

      return client
    end
  end
end