govuk_app_config 8.1.1 → 9.0.1

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: fc7d94f65aa2c81b7df62708d405601aeef8ca8dfe8455d59da46843c75eaf1e
4
+ data.tar.gz: 183aecdff3c9a1f8fd0f0ee3ad20bdece7a195d8005592d02b3eac1f4ae8770d
5
5
  SHA512:
6
- metadata.gz: a0dba812e73f859cb79e379f80f05322836c402043849070539d75fbdc489e8e83270ae26adbe816b58eae7a2789ac83cf35581ef94b586bba53e6feb86a6f51
7
- data.tar.gz: 9e4840809cdb83747be969f86c78de547d79c635e9e4eb83635e627f607b509b52689a2db66faf85ab11af99327877f9a45597465c8a50d629c2440b8fcc394d
6
+ metadata.gz: 82c3121d70042f7333b8c81acae37a2466bb2e04a6142a75eeaecdc2ccc345dd35886350da7b6657e0bc9550eeaf0d09aa43326a3327c4ffdcac5d6536784c28
7
+ data.tar.gz: 4dd45d0cebff36feda7e21a5032baa716d7147cb03b7388674e9bab8421a5f04af1089bb472f82b7267a60dc014a8085ea14bb3c7ef415ada8f674a8bbe1b3fb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 9.0.1
2
+
3
+ * Rename the "error" field in Rails logs from logstasher to "message" as error is supposed to be an object.
4
+
5
+ # 9.0.0
6
+
7
+ * 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))
8
+ * Add govuk_request_id to JSON logging for apps with gds-api-adapters ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
9
+ * BREAKING: Remove $stdout, $stderr and $real_stdout redirections ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
10
+ * BREAKING: Change error log behaviour from logging JSON to full string ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
11
+ * Remove monkeypatch for errors ([#300](https://github.com/alphagov/govuk_app_config/pull/300))
12
+
1
13
  # 8.1.1
2
14
 
3
15
  * 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
@@ -51,9 +50,14 @@ module GovukLogging
51
50
  # Elasticsearch index expect source to be an object and logstash defaults
52
51
  # source to be the host IP address causing logs to be dropped.
53
52
  Rails.application.config.logstasher.source = {}
53
+ # Elasticsearch index expect error to be an object and logstash defaults
54
+ # error to be a string causing logs to be dropped.
55
+ Rails.application.config.logstasher.field_renaming = {
56
+ error: :message,
57
+ }
54
58
 
55
59
  Rails.application.config.logstasher.logger = Logger.new(
56
- $real_stdout, # rubocop:disable Style/GlobalVars
60
+ $stdout,
57
61
  level: Rails.logger.level,
58
62
  formatter: proc { |_severity, _datetime, _progname, msg|
59
63
  "#{msg.is_a?(String) ? msg : msg.inspect}\n"
@@ -68,7 +72,5 @@ module GovukLogging
68
72
  # the responses it gets, so direct this to the logstasher logger.
69
73
  GdsApi::Base.default_options[:logger] = Rails.application.config.logstasher.logger
70
74
  end
71
-
72
- RailsExt::ActionDispatch.monkey_patch_log_error if RailsExt::ActionDispatch.should_monkey_patch_log_error?
73
75
  end
74
76
  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.1".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.1
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