Class: DeadBro::ElasticsearchSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/dead_bro/elasticsearch_subscriber.rb

Constant Summary collapse

THREAD_LOCAL_KEY =
:dead_bro_elasticsearch_events
MAX_TRACKED_EVENTS =
500

Class Method Summary collapse

Class Method Details

.extract_operation(method, path) ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dead_bro/elasticsearch_subscriber.rb', line 49

def self.extract_operation(method, path)
  return "unknown" if path.nil?

  clean = path.to_s.split("?").first.to_s
  m = method.to_s.upcase

  if clean =~ /_search\z/i
    "search"
  elsif clean =~ /_msearch\z/i
    "msearch"
  elsif clean =~ /_bulk\z/i
    "bulk"
  elsif clean =~ /_doc\/[^\/]+\/_update\z/i
    "update"
  elsif clean =~ /_update\/[^\/]+\z/i
    "update"
  elsif clean =~ /_delete_by_query\z/i
    "delete_by_query"
  elsif clean =~ /_count\z/i
    "count"
  elsif clean =~ /_mapping\z/i
    m == "GET" ? "get_mapping" : "put_mapping"
  elsif clean =~ /_doc\/[^\/]+\z/i
    case m
    when "GET"        then "get"
    when "DELETE"     then "delete"
    when "POST", "PUT" then "index"
    else "doc"
    end
  elsif clean =~ /_doc\z/i
    "index"
  elsif clean =~ /\A\/_cluster\//i
    "cluster"
  elsif clean =~ /\A\/_cat\//i
    "cat"
  elsif clean =~ /\A\/[^\/]+\z/
    case m
    when "PUT"    then "create_index"
    when "DELETE" then "delete_index"
    when "HEAD"   then "index_exists"
    when "GET"    then "get_index"
    else "index_op"
    end
  else
    m.downcase
  end
rescue
  "unknown"
end

.record(method:, path:, status:, duration_ms:) ⇒ Object

Called by HttpInstrumentation when it detects a Net::HTTP request to an ES host.



16
17
18
19
20
21
22
23
# File 'lib/dead_bro/elasticsearch_subscriber.rb', line 16

def self.record(method:, path:, status:, duration_ms:)
  events = Thread.current[THREAD_LOCAL_KEY]
  return unless events
  return unless should_continue_tracking?

  events << build_event(method, path, status, duration_ms)
rescue
end

.sanitize_path(path) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/dead_bro/elasticsearch_subscriber.rb', line 99

def self.sanitize_path(path)
  return "" if path.nil?
  path.to_s
    .gsub(/\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i, "/{id}")
    .gsub(/\/\d+(?=\/|\z)/, "/{id}")
rescue
  path.to_s
end

.should_continue_tracking?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dead_bro/elasticsearch_subscriber.rb', line 35

def self.should_continue_tracking?
  events = Thread.current[THREAD_LOCAL_KEY]
  return false unless events
  return false if events.length >= MAX_TRACKED_EVENTS

  start_time = Thread.current[DeadBro::TRACKING_START_TIME_KEY]
  if start_time
    elapsed_seconds = Time.now - start_time
    return false if elapsed_seconds >= DeadBro::MAX_TRACKING_DURATION_SECONDS
  end

  true
end

.start_request_trackingObject



25
26
27
# File 'lib/dead_bro/elasticsearch_subscriber.rb', line 25

def self.start_request_tracking
  Thread.current[THREAD_LOCAL_KEY] = []
end

.stop_request_trackingObject



29
30
31
32
33
# File 'lib/dead_bro/elasticsearch_subscriber.rb', line 29

def self.stop_request_tracking
  events = Thread.current[THREAD_LOCAL_KEY]
  Thread.current[THREAD_LOCAL_KEY] = nil
  events || []
end

.subscribe!Object

Install gem-based notification subscriber (request.elasticsearch / request.elastic_transport). The Net::HTTP path is handled by HttpInstrumentation, which calls .record directly.



10
11
12
13
# File 'lib/dead_bro/elasticsearch_subscriber.rb', line 10

def self.subscribe!
  install_notifications_subscription!
rescue
end