Class: RestfulResource::Instrumentation
- Inherits:
-
Object
- Object
- RestfulResource::Instrumentation
- Defined in:
- lib/restful_resource/instrumentation.rb
Instance Attribute Summary collapse
-
#api_name ⇒ Object
readonly
Returns the value of attribute api_name.
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#cache_instrument_name ⇒ Object
readonly
Returns the value of attribute cache_instrument_name.
-
#metric_class ⇒ Object
readonly
Returns the value of attribute metric_class.
-
#request_instrument_name ⇒ Object
readonly
Returns the value of attribute request_instrument_name.
-
#server_cache_instrument_name ⇒ Object
readonly
Returns the value of attribute server_cache_instrument_name.
Instance Method Summary collapse
-
#base_request_path(event) ⇒ Object
Converts a path like “/api/v2/cap_derivatives/75423/with_colours” to “api_v2_cap_derivatives_with_colours”.
- #cache_notifier_namespace(metric:, event: nil) ⇒ Object
-
#initialize(app_name:, api_name:, request_instrument_name:, cache_instrument_name:, server_cache_instrument_name:, metric_class:) ⇒ Instrumentation
constructor
A new instance of Instrumentation.
- #path_from_event(event) ⇒ Object
- #status_from_event(event) ⇒ Object
- #subscribe_to_notifications ⇒ Object
- #url_from_event(event) ⇒ Object
- #validate_metric_class! ⇒ Object
Constructor Details
#initialize(app_name:, api_name:, request_instrument_name:, cache_instrument_name:, server_cache_instrument_name:, metric_class:) ⇒ Instrumentation
Returns a new instance of Instrumentation.
6 7 8 9 10 11 12 13 |
# File 'lib/restful_resource/instrumentation.rb', line 6 def initialize(app_name:, api_name:, request_instrument_name:, cache_instrument_name:, server_cache_instrument_name:, metric_class:) @app_name = app_name @api_name = api_name @request_instrument_name = request_instrument_name @cache_instrument_name = cache_instrument_name @server_cache_instrument_name = server_cache_instrument_name @metric_class = metric_class end |
Instance Attribute Details
#api_name ⇒ Object (readonly)
Returns the value of attribute api_name.
15 16 17 |
# File 'lib/restful_resource/instrumentation.rb', line 15 def api_name @api_name end |
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
15 16 17 |
# File 'lib/restful_resource/instrumentation.rb', line 15 def app_name @app_name end |
#cache_instrument_name ⇒ Object (readonly)
Returns the value of attribute cache_instrument_name.
15 16 17 |
# File 'lib/restful_resource/instrumentation.rb', line 15 def cache_instrument_name @cache_instrument_name end |
#metric_class ⇒ Object (readonly)
Returns the value of attribute metric_class.
15 16 17 |
# File 'lib/restful_resource/instrumentation.rb', line 15 def metric_class @metric_class end |
#request_instrument_name ⇒ Object (readonly)
Returns the value of attribute request_instrument_name.
15 16 17 |
# File 'lib/restful_resource/instrumentation.rb', line 15 def request_instrument_name @request_instrument_name end |
#server_cache_instrument_name ⇒ Object (readonly)
Returns the value of attribute server_cache_instrument_name.
15 16 17 |
# File 'lib/restful_resource/instrumentation.rb', line 15 def server_cache_instrument_name @server_cache_instrument_name end |
Instance Method Details
#base_request_path(event) ⇒ Object
Converts a path like “/api/v2/cap_derivatives/75423/with_colours” to “api_v2_cap_derivatives_with_colours”
114 115 116 |
# File 'lib/restful_resource/instrumentation.rb', line 114 def base_request_path(event) path_from_event(event).split('/').drop(1).select {|a| a.match(/\d+/).nil? }.join('_') if event end |
#cache_notifier_namespace(metric:, event: nil) ⇒ Object
109 110 111 |
# File 'lib/restful_resource/instrumentation.rb', line 109 def cache_notifier_namespace(metric:, event: nil) [app_name, api_name, base_request_path(event), metric].compact.join('.') end |
#path_from_event(event) ⇒ Object
118 119 120 |
# File 'lib/restful_resource/instrumentation.rb', line 118 def path_from_event(event) url_from_event(event)&.path.to_s end |
#status_from_event(event) ⇒ Object
126 127 128 |
# File 'lib/restful_resource/instrumentation.rb', line 126 def status_from_event(event) event.payload[:env]&.status || event.payload&.status end |
#subscribe_to_notifications ⇒ Object
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 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 98 99 100 |
# File 'lib/restful_resource/instrumentation.rb', line 18 def subscribe_to_notifications validate_metric_class! # Subscribes to events from Faraday ActiveSupport::Notifications.subscribe request_instrument_name do |*args| event = ActiveSupport::Notifications::Event.new(*args) status = status_from_event(event) # Outputs per API log lines like: # measure#quotes_site.research_site_api.time=215.161237 # count#quotes_site.research_site_api.status.200=1 # count#quotes_site.research_site_api.called=1 metric_class.measure cache_notifier_namespace(metric: 'time'), event.duration metric_class.count cache_notifier_namespace(metric: "status.#{status}"), 1 metric_class.count cache_notifier_namespace(metric: 'called'), 1 # Outputs per resource log lines like: # measure#quotes_site.research_site_api.api_v2_cap_derivatives.time=215.161237 # count#quotes_site.research_site_api.api_v2_cap_derivatives.status.200=1 # count#quotes_site.research_site_api.api_v2_cap_derivatives.called=1 metric_class.measure cache_notifier_namespace(metric: 'time', event: event), event.duration metric_class.count cache_notifier_namespace(metric: "status.#{status}", event: event), 1 metric_class.count cache_notifier_namespace(metric: 'called', event: event), 1 end # Subscribes to events from Faraday::HttpCache ActiveSupport::Notifications.subscribe cache_instrument_name do |*args| event = ActiveSupport::Notifications::Event.new(*args) cache_status = event.payload.fetch(:cache_status, nil) # Outputs log lines like: # count#quotes_site.research_site_api.cache_hit=1 # count#quotes_site.research_site_api.api_v2_cap_derivatives.cache_hit=1 case cache_status when :fresh, :valid metric_class.count cache_notifier_namespace(metric: 'cache_hit'), 1 metric_class.count cache_notifier_namespace(metric: 'cache_hit', event: event), 1 when :invalid, :miss metric_class.count cache_notifier_namespace(metric: 'cache_miss'), 1 metric_class.count cache_notifier_namespace(metric: 'cache_miss', event: event), 1 when :unacceptable metric_class.count cache_notifier_namespace(metric: 'cache_not_cacheable'), 1 metric_class.count cache_notifier_namespace(metric: 'cache_not_cacheable', event: event), 1 when :bypass metric_class.count cache_notifier_namespace(metric: 'cache_bypass'), 1 metric_class.count cache_notifier_namespace(metric: 'cache_bypass', event: event), 1 end end # Subscribes to events from Faraday::Cdn::Metrics ActiveSupport::Notifications.subscribe server_cache_instrument_name do |*args| event = ActiveSupport::Notifications::Event.new(*args) client_cache_status = event.payload.fetch(:client_cache_status, nil) server_cache_status = event.payload.fetch(:server_cache_status, nil) if client_cache_status.nil? || !client_cache_status.in?([:fresh, :valid]) # Outputs log lines like: # count#quotes_site.research_site_api.server_cache_hit=1 # count#quotes_site.research_site_api.api_v2_cap_derivatives.server_cache_hit=1 case server_cache_status when :fresh metric_class.count cache_notifier_namespace(metric: 'server_cache_hit'), 1 metric_class.count cache_notifier_namespace(metric: 'server_cache_hit', event: event), 1 when :valid metric_class.count cache_notifier_namespace(metric: 'server_cache_hit_while_revalidate'), 1 metric_class.count cache_notifier_namespace(metric: 'server_cache_hit_while_revalidate', event: event), 1 when :invalid, :miss metric_class.count cache_notifier_namespace(metric: 'server_cache_miss'), 1 metric_class.count cache_notifier_namespace(metric: 'server_cache_miss', event: event), 1 when :unacceptable metric_class.count cache_notifier_namespace(metric: 'server_cache_not_cacheable'), 1 metric_class.count cache_notifier_namespace(metric: 'server_cache_not_cacheable', event: event), 1 when :bypass metric_class.count cache_notifier_namespace(metric: 'server_cache_bypass'), 1 metric_class.count cache_notifier_namespace(metric: 'server_cache_bypass', event: event), 1 when :unknown metric_class.count cache_notifier_namespace(metric: 'server_cache_unknown'), 1 metric_class.count cache_notifier_namespace(metric: 'server_cache_unknown', event: event), 1 end end end end |
#url_from_event(event) ⇒ Object
122 123 124 |
# File 'lib/restful_resource/instrumentation.rb', line 122 def url_from_event(event) event.payload[:env]&.url || event.payload&.url end |
#validate_metric_class! ⇒ Object
102 103 104 105 106 107 |
# File 'lib/restful_resource/instrumentation.rb', line 102 def validate_metric_class! metric_methods = %i(count sample measure) if metric_methods.any? {|m| !metric_class.respond_to?(m) } raise ArgumentError.new "Metric class '#{metric_class}' does not respond to #{metric_methods.join ','}" end end |