Module: Salopulse::RequestContext
- Defined in:
- lib/salopulse/request_context.rb
Constant Summary collapse
- KEY =
:salopulse_request_context- SUPPRESS_KEY =
:salopulse_suppressed
Class Method Summary collapse
- .active? ⇒ Boolean
- .clear ⇒ Object
- .current ⇒ Object
- .elapsed_ms ⇒ Object
- .monotonic_now ⇒ Object
- .record_sql_event(event, fingerprint) ⇒ Object
- .set_tag(key, value) ⇒ Object
- .set_user(attrs) ⇒ Object
- .start(endpoint:, http_method:) ⇒ Object
- .suppressed? ⇒ Boolean
- .with_suppression ⇒ Object
Class Method Details
.active? ⇒ Boolean
28 29 30 |
# File 'lib/salopulse/request_context.rb', line 28 def active? !current.nil? end |
.clear ⇒ Object
32 33 34 |
# File 'lib/salopulse/request_context.rb', line 32 def clear Thread.current[KEY] = nil end |
.current ⇒ Object
24 25 26 |
# File 'lib/salopulse/request_context.rb', line 24 def current Thread.current[KEY] end |
.elapsed_ms ⇒ Object
52 53 54 55 |
# File 'lib/salopulse/request_context.rb', line 52 def elapsed_ms return 0 unless current ((monotonic_now - current[:started_at]) * 1000).to_i end |
.monotonic_now ⇒ Object
72 73 74 |
# File 'lib/salopulse/request_context.rb', line 72 def monotonic_now Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
.record_sql_event(event, fingerprint) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/salopulse/request_context.rb', line 44 def record_sql_event(event, fingerprint) ctx = current return false unless ctx ctx[:sql_events] << [event, fingerprint] ctx[:sql_fingerprint_counts][fingerprint] += 1 true end |
.set_tag(key, value) ⇒ Object
40 41 42 |
# File 'lib/salopulse/request_context.rb', line 40 def set_tag(key, value) current[:tags][key] = value if current end |
.set_user(attrs) ⇒ Object
36 37 38 |
# File 'lib/salopulse/request_context.rb', line 36 def set_user(attrs) current[:user] = attrs if current end |
.start(endpoint:, http_method:) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/salopulse/request_context.rb', line 10 def start(endpoint:, http_method:) Thread.current[KEY] = { request_id: SecureRandom.uuid, endpoint: endpoint, http_method: http_method, sql_events: [], sql_fingerprint_counts: Hash.new(0), started_at: monotonic_now, user: nil, tags: {}, suppressed: false } end |
.suppressed? ⇒ Boolean
57 58 59 |
# File 'lib/salopulse/request_context.rb', line 57 def suppressed? Thread.current[SUPPRESS_KEY] == true end |
.with_suppression ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/salopulse/request_context.rb', line 61 def with_suppression previous_ctx = Thread.current[KEY] previous_suppress = Thread.current[SUPPRESS_KEY] Thread.current[KEY] = nil Thread.current[SUPPRESS_KEY] = true yield ensure Thread.current[KEY] = previous_ctx Thread.current[SUPPRESS_KEY] = previous_suppress end |