Class: ScoutApm::Instruments::Elasticsearch

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/instruments/elasticsearch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Elasticsearch

Returns a new instance of Elasticsearch.



8
9
10
11
# File 'lib/scout_apm/instruments/elasticsearch.rb', line 8

def initialize(context)
  @context = context
  @installed = false
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/scout_apm/instruments/elasticsearch.rb', line 6

def context
  @context
end

Instance Method Details

#installObject



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
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
74
75
76
77
78
79
# File 'lib/scout_apm/instruments/elasticsearch.rb', line 21

def install
  if defined?(::Elasticsearch) &&
      defined?(::Elasticsearch::Transport) &&
      defined?(::Elasticsearch::Transport::Client)

    @installed = true

    logger.info "Instrumenting Elasticsearch"

    ::Elasticsearch::Transport::Client.class_eval do
      include ScoutApm::Tracer

      def perform_request_with_scout_instruments(*args, &block)
        name = _sanitize_name(args[1])

        self.class.instrument("Elasticsearch", name, :ignore_children => true) do
          perform_request_without_scout_instruments(*args, &block)
        end
      end

      alias_method :perform_request_without_scout_instruments, :perform_request
      alias_method :perform_request, :perform_request_with_scout_instruments

      def _sanitize_name(name)
        name = name.split("/").last.gsub(/^_/, '')
        allowed_names = ["bench",
                         "bulk",
                         "count",
                         "exists",
                         "explain",
                         "field_stats",
                         "health",
                         "mget",
                         "mlt",
                         "mpercolate",
                         "msearch",
                         "mtermvectors",
                         "percolate",
                         "query",
                         "scroll",
                         "search_shards",
                         "source",
                         "suggest",
                         "template",
                         "termvectors",
                         "update",
                         "search", ]

        if allowed_names.include?(name)
          name
        else
          "Unknown"
        end
      rescue
        "Unknown"
      end
    end
  end
end

#installed?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/scout_apm/instruments/elasticsearch.rb', line 17

def installed?
  @installed
end

#loggerObject



13
14
15
# File 'lib/scout_apm/instruments/elasticsearch.rb', line 13

def logger
  context.logger
end