govuk_app_config 8.1.1 → 9.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df59068ad0c6277dddb68a7c934e9557fc3e59db4a72022503c29942ca18fe1a
4
- data.tar.gz: 691a810bf1793a069121d37a343c0fb955b7034bdf55166377ebe5454b60af6e
3
+ metadata.gz: 734094235f224b9ff9f6997173bc0d43462c0b8c3ab315743cb57d480280a99f
4
+ data.tar.gz: 44b9a4f66e2af4aeb5f34c6734af0d579e1c31ffa758562ff212358c18025157
5
5
  SHA512:
6
- metadata.gz: a0dba812e73f859cb79e379f80f05322836c402043849070539d75fbdc489e8e83270ae26adbe816b58eae7a2789ac83cf35581ef94b586bba53e6feb86a6f51
7
- data.tar.gz: 9e4840809cdb83747be969f86c78de547d79c635e9e4eb83635e627f607b509b52689a2db66faf85ab11af99327877f9a45597465c8a50d629c2440b8fcc394d
6
+ metadata.gz: a283d0c433d0c52d14def673aec0bfcf6676f79ff69f685bf027b800cbc8be01839550bbbb3c89c3dd4298fe457650cdaf157bb81f6ac0931654f3978e151366
7
+ data.tar.gz: d179c6ab495c9812c47a43ec35329648fbb93ebb29fcf976fb4bc2f68be3c6f129808aad893f7033fe0fa84678720f32a3016c9241a18b34d2130a57528c5071
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 9.0.0
2
+
3
+ * BREAKING: JSON logs are no longer configured automatically for production Rails apps and are turned on with the GOVUK_RAILS_JSON_LOGGING environment variable ([#302](https://github.com/alphagov/govuk_app_config/pull/302))
4
+ * Add govuk_request_id to JSON logging for apps with gds-api-adapters ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
5
+ * BREAKING: Remove $stdout, $stderr and $real_stdout redirections ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
6
+ * BREAKING: Change error log behaviour from logging JSON to full string ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
7
+ * Remove monkeypatch for errors ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
8
+
1
9
  # 8.1.1
2
10
 
3
11
  * Fix prometheus_exporter to method patching compatible with open telemetry.
data/README.md CHANGED
@@ -142,8 +142,12 @@ check docs](docs/healthchecks.md) for more information on how to use it.
142
142
 
143
143
  ## Rails logging
144
144
 
145
- In Rails applications, the application will be configured to send JSON-formatted
146
- logs to `STDOUT` and unstructured logs to `STDERR`.
145
+ To enable production-like logging, an env variable `GOVUK_RAILS_JSON_LOGGING`
146
+ is set in the `govuk-helm-charts` and then checked in `railtie.rb`. This will
147
+ allow JSON format logs and `Govuk-Request-Id` to be visible.
148
+
149
+ For development logs, in order to see the production style logs, developers should
150
+ set `GOVUK_RAILS_JSON_LOGGING`in `govuk-docker` -> `docker-compose` files.
147
151
 
148
152
 
149
153
  ## Content Security Policy generation
@@ -1,32 +1,31 @@
1
+ require "json"
1
2
  require "logstasher"
2
3
  require "action_controller"
3
- require_relative "rails_ext/action_dispatch/debug_exceptions"
4
4
 
5
- module GovukLogging
5
+ module GovukJsonLogging
6
6
  def self.configure
7
- # GOV.UK Rails applications are expected to output JSON to stdout which is
8
- # then indexed in a Kibana instance. These log outputs are created by the
9
- # logstasher gem.
10
- #
11
- # Rails applications will typically write other things to stdout such as
12
- # `Rails.logger` calls or 'puts' statements. However these are not in a
13
- # JSON format which causes problems for the log file parsers.
14
- #
15
- # To resolve this we redirect stdout to stderr, to cover any Rails
16
- # writing. This frees up the normal stdout for the logstasher logs.
17
- #
18
- # We also disable buffering, so that logs aren't lost on crash or delayed
7
+ # We disable buffering, so that logs aren't lost on crash or delayed
19
8
  # indefinitely while troubleshooting.
20
-
21
- # rubocop:disable Style/GlobalVars
22
- $real_stdout = $stdout.clone
23
- $real_stdout.sync = true
24
- $stdout.reopen($stderr)
25
9
  $stdout.sync = true
26
- # rubocop:enable Style/GlobalVars
27
10
 
28
- # Send Rails' logs to STDERR because they're not JSON formatted.
29
- Rails.logger = ActiveSupport::TaggedLogging.new(Logger.new($stderr, level: Rails.logger.level))
11
+ Rails.logger = Logger.new(
12
+ $stdout,
13
+ level: Rails.logger.level,
14
+ formatter: proc { |severity, datetime, _progname, msg|
15
+ hash = {
16
+ "@timestamp": datetime.utc.iso8601(3),
17
+ message: msg,
18
+ level: severity,
19
+ tags: %w[rails],
20
+ }
21
+
22
+ if defined?(GdsApi::GovukHeaders) && !GdsApi::GovukHeaders.headers[:govuk_request_id].nil?
23
+ hash[:govuk_request_id] = GdsApi::GovukHeaders.headers[:govuk_request_id]
24
+ end
25
+
26
+ "#{hash.to_json}\n"
27
+ },
28
+ )
30
29
 
31
30
  LogStasher.add_custom_fields do |fields|
32
31
  # Mirrors Nginx request logging, e.g. GET /path/here HTTP/1.1
@@ -53,7 +52,7 @@ module GovukLogging
53
52
  Rails.application.config.logstasher.source = {}
54
53
 
55
54
  Rails.application.config.logstasher.logger = Logger.new(
56
- $real_stdout, # rubocop:disable Style/GlobalVars
55
+ $stdout,
57
56
  level: Rails.logger.level,
58
57
  formatter: proc { |_severity, _datetime, _progname, msg|
59
58
  "#{msg.is_a?(String) ? msg : msg.inspect}\n"
@@ -68,7 +67,5 @@ module GovukLogging
68
67
  # the responses it gets, so direct this to the logstasher logger.
69
68
  GdsApi::Base.default_options[:logger] = Rails.application.config.logstasher.logger
70
69
  end
71
-
72
- RailsExt::ActionDispatch.monkey_patch_log_error if RailsExt::ActionDispatch.should_monkey_patch_log_error?
73
70
  end
74
71
  end
@@ -13,7 +13,7 @@ module GovukAppConfig
13
13
  end
14
14
 
15
15
  config.before_initialize do
16
- GovukLogging.configure if Rails.env.production?
16
+ GovukJsonLogging.configure if ENV["GOVUK_RAILS_JSON_LOGGING"]
17
17
  end
18
18
 
19
19
  config.after_initialize do
@@ -1,3 +1,3 @@
1
1
  module GovukAppConfig
2
- VERSION = "8.1.1".freeze
2
+ VERSION = "9.0.0".freeze
3
3
  end
@@ -7,7 +7,7 @@ require "govuk_app_config/govuk_open_telemetry"
7
7
  require "govuk_app_config/govuk_prometheus_exporter"
8
8
 
9
9
  if defined?(Rails)
10
- require "govuk_app_config/govuk_logging"
10
+ require "govuk_app_config/govuk_json_logging"
11
11
  require "govuk_app_config/govuk_content_security_policy"
12
12
  require "govuk_app_config/railtie"
13
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_app_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.1.1
4
+ version: 9.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-04 00:00:00.000000000 Z
11
+ date: 2023-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstasher
@@ -336,13 +336,12 @@ files:
336
336
  - lib/govuk_app_config/govuk_healthcheck/rails_cache.rb
337
337
  - lib/govuk_app_config/govuk_healthcheck/redis.rb
338
338
  - lib/govuk_app_config/govuk_healthcheck/sidekiq_redis.rb
339
- - lib/govuk_app_config/govuk_logging.rb
339
+ - lib/govuk_app_config/govuk_json_logging.rb
340
340
  - lib/govuk_app_config/govuk_open_telemetry.rb
341
341
  - lib/govuk_app_config/govuk_prometheus_exporter.rb
342
342
  - lib/govuk_app_config/govuk_proxy/static_proxy.rb
343
343
  - lib/govuk_app_config/govuk_puma.rb
344
344
  - lib/govuk_app_config/govuk_statsd.rb
345
- - lib/govuk_app_config/rails_ext/action_dispatch/debug_exceptions.rb
346
345
  - lib/govuk_app_config/railtie.rb
347
346
  - lib/govuk_app_config/version.rb
348
347
  homepage: https://github.com/alphagov/govuk_app_config
@@ -1,52 +0,0 @@
1
- require "action_dispatch/middleware/debug_exceptions"
2
-
3
- module GovukLogging
4
- module RailsExt
5
- module ActionDispatch
6
- def self.should_monkey_patch_log_error?(clazz = ::ActionDispatch::DebugExceptions)
7
- empty_instance = clazz.new nil
8
- target_method = empty_instance.method :log_error
9
-
10
- expected_parameters = [%i[req request], %i[req wrapper]]
11
- actual_parameters = target_method.parameters
12
-
13
- should_monkey_patch = actual_parameters == expected_parameters
14
-
15
- unless should_monkey_patch
16
- Rails.logger.warn "Refused to monkey patch ::ActionDispatch::DebugExceptions#log_error - " \
17
- "signatures do not match. " \
18
- "Expected #{expected_parameters}, but got #{actual_parameters}"
19
- end
20
-
21
- should_monkey_patch
22
- rescue StandardError => e
23
- Rails.logger.warn "Failed to detect whether to monkey patch " \
24
- "::ActionDispatch::DebugExceptions#log_error - #{e.inspect}"
25
- false
26
- end
27
-
28
- def self.monkey_patch_log_error(clazz = ::ActionDispatch::DebugExceptions)
29
- clazz.class_eval do
30
- private
31
-
32
- def log_error(request, wrapper)
33
- logger = logger(request)
34
-
35
- return unless logger
36
-
37
- exception = wrapper.exception
38
-
39
- trace = wrapper.application_trace
40
- trace = wrapper.framework_trace if trace.empty?
41
-
42
- logger.fatal({
43
- exception_class: exception.class.to_s,
44
- exception_message: exception.message,
45
- stacktrace: trace,
46
- }.to_json)
47
- end
48
- end
49
- end
50
- end
51
- end
52
- end