-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathobservability.rb
More file actions
61 lines (54 loc) · 2.05 KB
/
observability.rb
File metadata and controls
61 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
module Html2rss
module Web
##
# Structured observability event emitter for request-critical paths.
module Observability
SCHEMA_VERSION = '1.0'
class << self
# @param event_name [String]
# @param outcome [String] expected values: success|failure.
# @param details [Hash{Symbol=>Object}]
# @param level [Symbol]
# @return [void]
def emit(event_name:, outcome:, details: {}, level: :info)
LogEvent.emit(payload: build_payload(event_name, outcome, details), level: level)
rescue StandardError => error
handle_emit_error(error, event_name, outcome)
end
private
# @param error [StandardError]
# @param event_name [String]
# @param outcome [String]
# @return [void]
def handle_emit_error(error, event_name, outcome)
Kernel.warn("Observability emit error: #{error.message}")
Kernel.warn("event_name=#{event_name} outcome=#{outcome}")
end
# @param event_name [String]
# @param outcome [String]
# @param details [Hash{Symbol=>Object}]
# @return [Hash{Symbol=>Object}]
def build_payload(event_name, outcome, details)
context = RequestContext.current_h
base_payload(event_name, outcome, context).merge(details: details)
end
# @param event_name [String]
# @param outcome [String]
# @param context [Hash{Symbol=>Object}]
# @return [Hash{Symbol=>Object}]
def base_payload(event_name, outcome, context)
{
event_name: event_name, schema_version: SCHEMA_VERSION, request_id: context[:request_id],
route_group: context[:route_group], actor: context[:actor], outcome: outcome, **context_fields(context)
}
end
# @param context [Hash{Symbol=>Object}]
# @return [Hash{Symbol=>Object}]
def context_fields(context)
context.slice(:path, :method, :strategy, :started_at)
end
end
end
end
end