Skip to content

Commit

Permalink
optionally include IP info at start of search/find linked data requests
Browse files Browse the repository at this point in the history
Marks the start of each linked data search/find request.  Unless `Qa.config.suppress_ip_data_from_log`, this marker will include IP address, city, state, and country as reported by the `geocoder` gem.

NOTE: This also removes inclusion of the request_id directly in the logging messages.  To include request_id, it is recommended that you set up the logger to do this automatically in `config/environments/development.rb` (or relevant env file) using…

```
config.log_tags = [:request_id]
```
  • Loading branch information
elrayle committed Dec 13, 2019
1 parent 59d3d37 commit c89187e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
12 changes: 8 additions & 4 deletions app/services/qa/linked_data/request_header_service.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Service to construct a request header that includes optional attributes for search and fetch requests.
require 'geocoder'
module Qa
module LinkedData
class RequestHeaderService
Expand All @@ -16,7 +17,8 @@ class RequestHeaderService
def initialize(request:, params:)
@request = request
@params = params
@request_id = assign_request_id
@request_id = request.request_id
log_request
end

# Construct request parameters to pass to search_query (linked data module).
Expand Down Expand Up @@ -67,9 +69,11 @@ def content_type_for_format

private

# assign request id
def assign_request_id
SecureRandom.uuid
def log_request
gc = request.location
msg = "******** #{request.path_parameters[:action].upcase}"
msg += " from IP #{request.ip} in {city: #{gc.city}, state: #{gc.state}, country: #{gc.country}}" unless Qa.config.suppress_ip_data_from_log
Rails.logger.info(msg)
end

# filter literals in results to this language
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/qa/install/templates/config/initializers/qa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@
# When false, properties that do not override default optional behavior will be shown whether or not the property has a value in the graph.
# When true, properties that do not override default optional behavior will not be shown whn the property does not have a value in the graph.
# config.property_map_default_for_optional = false

# IP data including IP address, city, state, and country will be logged with each request.
# When false, IP data is logged
# When true, IP data will not be logged (default for backward compatibility)
# config.suppress_ip_data_from_log = true
end
9 changes: 4 additions & 5 deletions lib/qa/authorities/linked_data/find_term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def initialize(term_config)
def find(id, request_header: {}, language: nil, replacements: {}, subauth: nil, format: 'json', performance_data: false) # rubocop:disable Metrics/ParameterLists
request_header = build_request_header(language: language, replacements: replacements, subauth: subauth, format: format, performance_data: performance_data) if request_header.empty?
unpack_request_header(request_header)
raise Qa::InvalidLinkedDataAuthority, "#{request_id} - Unable to initialize linked data term sub-authority #{subauthority}" unless subauthority.nil? || term_subauthority?(subauthority)
raise Qa::InvalidLinkedDataAuthority, "Unable to initialize linked data term sub-authority #{subauthority}" unless subauthority.nil? || term_subauthority?(subauthority)
@id = id
url = authority_service.build_url(action_config: term_config, action: :term, action_request: normalize_id, request_header: request_header)
Rails.logger.info "#{request_id} - QA Linked Data term url: #{url}"
Rails.logger.info "QA Linked Data term url: #{url}"
load_graph(url: url)
normalize_results
end
Expand All @@ -63,7 +63,7 @@ def load_graph(url:)

access_end_dt = Time.now.utc
@access_time_s = access_end_dt - access_start_dt
Rails.logger.info("#{request_id} - Time to receive data from authority: #{access_time_s}s")
Rails.logger.info("Time to receive data from authority: #{access_time_s}s")
end

def normalize_results
Expand All @@ -73,7 +73,7 @@ def normalize_results

normalize_end_dt = Time.now.utc
@normalize_time_s = normalize_end_dt - normalize_start_dt
Rails.logger.info("#{request_id} - Time to normalize data: #{normalize_time_s}s")
Rails.logger.info("Time to normalize data: #{normalize_time_s}s")
results = append_data_outside_results(results)
results
end
Expand Down Expand Up @@ -316,7 +316,6 @@ def build_request_header(language:, replacements:, subauth:, format:, performanc
)
end
request_header = {}
request_header[:request_id] = SecureRandom.uuid
request_header[:replacements] = replacements || {}
request_header[:subauthority] = subauth || nil
request_header[:language] = language || nil
Expand Down
9 changes: 4 additions & 5 deletions lib/qa/authorities/linked_data/search_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def initialize(search_config)
def search(query, request_header: {}, language: nil, replacements: {}, subauth: nil, context: false, performance_data: false) # rubocop:disable Metrics/ParameterLists
request_header = build_request_header(language: language, replacements: replacements, subauth: subauth, context: context, performance_data: performance_data) if request_header.empty?
unpack_request_header(request_header)
raise Qa::InvalidLinkedDataAuthority, "#{request_id} - Unable to initialize linked data search sub-authority #{subauthority}" unless subauthority.nil? || subauthority?(subauthority)
raise Qa::InvalidLinkedDataAuthority, "Unable to initialize linked data search sub-authority #{subauthority}" unless subauthority.nil? || subauthority?(subauthority)
url = authority_service.build_url(action_config: search_config, action: :search, action_request: query, request_header: request_header)
Rails.logger.info "#{request_id} - QA Linked Data search url: #{url}"
Rails.logger.info "QA Linked Data search url: #{url}"
load_graph(url: url)
normalize_results
end
Expand All @@ -53,7 +53,7 @@ def load_graph(url:)

access_end_dt = Time.now.utc
@access_time_s = access_end_dt - access_start_dt
Rails.logger.info("#{request_id} - Time to receive data from authority: #{access_time_s}s")
Rails.logger.info("Time to receive data from authority: #{access_time_s}s")
end

def normalize_results
Expand All @@ -65,7 +65,7 @@ def normalize_results

normalize_end_dt = Time.now.utc
@normalize_time_s = normalize_end_dt - normalize_start_dt
Rails.logger.info("#{request_id} - Time to normalize data: #{normalize_time_s}s")
Rails.logger.info("Time to normalize data: #{normalize_time_s}s")
json = append_data_outside_results(json)
json
end
Expand Down Expand Up @@ -213,7 +213,6 @@ def build_request_header(language:, replacements:, subauth:, context:, performan
)
end
request_header = {}
request_header[:request_id] = SecureRandom.uuid
request_header[:replacements] = replacements || {}
request_header[:subauthority] = subauth || nil
request_header[:language] = language || nil
Expand Down
9 changes: 9 additions & 0 deletions lib/qa/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,14 @@ def property_map_default_for_optional
@property_map_default_for_optional = false if @property_map_default_for_optional.nil?
@property_map_default_for_optional
end

# IP data including IP address, city, state, and country will be logged with each request.
# When false, IP data is logged
# When true, IP data will not be logged (default for backward compatibility)
attr_writer :suppress_ip_data_from_log
def suppress_ip_data_from_log
@suppress_ip_data_from_log = true if @suppress_ip_data_from_log.nil?
@suppress_ip_data_from_log
end
end
end
1 change: 1 addition & 0 deletions qa.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency 'activerecord-import'
s.add_dependency 'deprecation'
s.add_dependency 'faraday'
s.add_dependency 'geocoder'
s.add_dependency 'ldpath'
s.add_dependency 'nokogiri', '~> 1.6'
s.add_dependency 'rails', '>= 5.0', "< 6.1"
Expand Down

0 comments on commit c89187e

Please sign in to comment.