rack-logstasher 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c284226cd552c5f8ad2a0fde3bbc33ff6798a58806b401b915b4530d50ae5067
4
- data.tar.gz: 5c80aa54ba69f36bb904b6c71cd9166a2d27025b0887042fa2c2b1ab6ee0d732
3
+ metadata.gz: 539692170e2083634c7acbd4e5230264dd8d3292ca1bc6f1da857ce076b57b6a
4
+ data.tar.gz: 5ee4f57bceaf8c9376f7fb9e14a4ee9a10c46b39cb6b9781cf7f619bd0579d50
5
5
  SHA512:
6
- metadata.gz: a2150a7ccd858ae23a984c746bfb552a887295cd01d36e3ff3ac7f5c0171294f3b8f107ebe4183fa08901765278270a48c5d7795b2e5cee540fa6faa92e977b3
7
- data.tar.gz: 17877af0bb475ec61202a79f8191463f6611c51ec8fc2aa5b3b9833a1a5c05525b7c419511b59bc373c272138ac4b54f8c9e6e19c4292145385f90e014ae0043
6
+ metadata.gz: 1cdb8df3283ca4bb814e6d834f236690ff7d75df81b66ec62fbd266843e70b7bf99cb4309dbc10c200d1cb20c2520d91811c3015c2179c7b4ff220504dc212ce
7
+ data.tar.gz: 07acce7172451096035b875431dfd095a5e6df8d7a6dc25ad19f96904bd3e774f3a9cdab07f3d0f86f1a48f408a24388ba0c0638b42960e61db5df5a09f66d8d
@@ -0,0 +1,10 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ - package-ecosystem: "github-actions"
8
+ directory: "/"
9
+ schedule:
10
+ interval: daily
@@ -0,0 +1,35 @@
1
+ on: [push, pull_request]
2
+
3
+ jobs:
4
+ test_matrix:
5
+ strategy:
6
+ fail-fast: false
7
+ matrix:
8
+ # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
9
+ ruby: [2.7, '3.0', 3.1, 3.2]
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v3
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby }}
16
+ bundler-cache: true
17
+ - run: bundle exec rake
18
+
19
+ # Branch protection rules cannot directly depend on status checks from matrix jobs.
20
+ # So instead we define `test` as a dummy job which only runs after the preceding `test_matrix` checks have passed.
21
+ # Solution inspired by: https://github.community/t/status-check-for-a-matrix-jobs/127354/3
22
+ test:
23
+ needs: test_matrix
24
+ runs-on: ubuntu-latest
25
+ steps:
26
+ - run: echo "All matrix tests have passed 🚀"
27
+
28
+ publish:
29
+ needs: test
30
+ if: ${{ github.ref == 'refs/heads/main' }}
31
+ permissions:
32
+ contents: write
33
+ uses: alphagov/govuk-infrastructure/.github/workflows/publish-rubygem.yml@main
34
+ secrets:
35
+ GEM_HOST_API_KEY: ${{ secrets.ALPHAGOV_RUBYGEMS_API_KEY }}
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
1
  --color
2
- --format progress
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ inherit_gem:
2
+ rubocop-govuk:
3
+ - config/default.yml
4
+ - config/rake.yml
5
+ - config/rspec.yml
6
+
7
+ inherit_mode:
8
+ merge:
9
+ - Exclude
10
+
11
+ # **************************************************************
12
+ # TRY NOT TO ADD OVERRIDES IN THIS FILE
13
+ #
14
+ # This repo is configured to follow the RuboCop GOV.UK styleguide.
15
+ # Any rules you override here will cause this repo to diverge from
16
+ # the way we write code in all other GOV.UK repos.
17
+ #
18
+ # See https://github.com/alphagov/rubocop-govuk/blob/main/CONTRIBUTING.md
19
+ # **************************************************************
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.1
1
+ 2.7.6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 2.0.0
2
+
3
+ - Add support for Rack 3
4
+ - **BREAKING** Only allows lower case header keys due to [underlying Rack spec change at 3.0.0beta1](https://github.com/rack/rack/blob/main/CHANGELOG.md#300beta1---2022-08-08).
5
+
6
+ # 1.1.0
7
+
8
+ - Require Ruby 2.7 and greater
9
+
1
10
  # 1.0.2
2
11
 
3
12
  - Fix logging error where Rack uses the system monotonic clock.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in rack-logstasher.gemspec
4
4
  gemspec
@@ -1,6 +1,6 @@
1
- Copyright (c) 2013 Alex Tomlins
1
+ The MIT License (MIT)
2
2
 
3
- MIT License
3
+ Copyright (C) 2013 Crown Copyright (Government Digital Service)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Rack::Logstasher
2
2
 
3
- Rack middleware to log requests in logstash json event format
3
+ Rack middleware to log requests in logstash json event format.
4
+ It is designed to give Rails-like logging to Sinatra apps.
5
+ The gem is currently used by [search-api](https://github.com/alphagov/search-api) and [contentful-listener-api](https://github.com/alphagov/contentful-listener-api).
4
6
 
5
7
  ## Installation
6
8
 
@@ -29,3 +31,7 @@ Add this to the middleware stack. e.g.
29
31
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
32
  4. Push to the branch (`git push origin my-new-feature`)
31
33
  5. Create new Pull Request
34
+
35
+ ## Licence
36
+
37
+ [MIT License](LICENCE)
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
-
3
2
  require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
4
+
4
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
5
7
 
6
- task :default => :spec
8
+ task default: %i[rubocop spec]
@@ -1,5 +1,5 @@
1
- require 'rack/common_logger'
2
- require 'logstash/event'
1
+ require "rack/common_logger"
2
+ require "logstash/event"
3
3
 
4
4
  module Rack
5
5
  module Logstasher
@@ -10,20 +10,20 @@ module Rack
10
10
  @extra_response_headers = opts[:extra_response_headers] || {}
11
11
  end
12
12
 
13
- private
13
+ private
14
14
 
15
15
  def log(env, status, response_headers, began_at)
16
16
  now = Utils.clock_time
17
17
 
18
18
  data = {
19
- :method => env["REQUEST_METHOD"],
20
- :path => env["PATH_INFO"],
21
- :query_string => env["QUERY_STRING"],
22
- :status => status.to_i,
23
- :duration => duration_in_ms(began_at, now).round(2),
24
- :remote_addr => env['REMOTE_ADDR'],
25
- :request => request_line(env),
26
- :length => extract_content_length(response_headers)
19
+ method: env["REQUEST_METHOD"],
20
+ path: env["PATH_INFO"],
21
+ query_string: env["QUERY_STRING"],
22
+ status: status.to_i,
23
+ duration: duration_in_ms(began_at, now).round(2),
24
+ remote_addr: env["REMOTE_ADDR"],
25
+ request: request_line(env),
26
+ length: extract_content_length(response_headers),
27
27
  }
28
28
 
29
29
  @extra_request_headers.each do |header, log_key|
@@ -39,8 +39,8 @@ module Rack
39
39
  end
40
40
  end
41
41
 
42
- event = LogStash::Event.new(data.merge('tags' => ['request']))
43
- msg = event.to_json + "\n"
42
+ event = LogStash::Event.new(data.merge("tags" => %w[request]))
43
+ msg = "#{event.to_json}\n"
44
44
  if @logger.respond_to?(:write)
45
45
  @logger.write(msg)
46
46
  else
@@ -53,12 +53,11 @@ module Rack
53
53
  end
54
54
 
55
55
  def request_line(env)
56
- line = "#{env["REQUEST_METHOD"]} #{env["SCRIPT_NAME"]}#{env['PATH_INFO']}"
57
- line << "?#{env["QUERY_STRING"]}" if env["QUERY_STRING"] and ! env["QUERY_STRING"].empty?
58
- line << " #{env["SERVER_PROTOCOL"]}"
56
+ line = "#{env['REQUEST_METHOD']} #{env['SCRIPT_NAME']}#{env['PATH_INFO']}"
57
+ line << "?#{env['QUERY_STRING']}" if env["QUERY_STRING"] && !env["QUERY_STRING"].empty?
58
+ line << " #{env['SERVER_PROTOCOL']}"
59
59
  line
60
60
  end
61
-
62
- end # Logger
61
+ end
63
62
  end
64
63
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module Logstasher
3
- VERSION = "1.0.2"
3
+ VERSION = "2.0.0".freeze
4
4
  end
5
5
  end
@@ -1,28 +1,29 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("lib", __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'rack/logstasher/version'
3
+ require "rack/logstasher/version"
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = "rack-logstasher"
8
7
  spec.version = Rack::Logstasher::VERSION
9
8
  spec.authors = ["GOV.UK Dev"]
10
9
  spec.email = ["govuk-dev@digital.cabinet-office.gov.uk"]
11
- spec.description = %q{Rack middleware to log requests in logstash json event format. Like the logstasher gem, but for rack apps.}
12
- spec.summary = %q{Rack middleware to log requests in logstash json event format}
10
+ spec.description = "Rack middleware to log requests in logstash json event format. Like the logstasher gem, but for rack apps."
11
+ spec.summary = "Rack middleware to log requests in logstash json event format"
13
12
  spec.homepage = "https://github.com/alphagov/rack-logstasher"
14
13
  spec.license = "MIT"
15
14
 
16
- spec.files = `git ls-files`.split($/)
15
+ spec.required_ruby_version = ">= 2.7"
16
+
17
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
+ spec.require_paths = %w[lib]
20
21
 
21
- spec.add_dependency "rack", "~> 2.0"
22
- spec.add_dependency "logstash-event"
22
+ spec.add_dependency "logstash-event", "~> 1.2"
23
+ spec.add_dependency "rack", ">= 2", "< 4"
23
24
 
24
- spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rack-test", "~> 2"
25
26
  spec.add_development_dependency "rake"
26
- spec.add_development_dependency "rack-test"
27
- spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rspec", "~> 3"
28
+ spec.add_development_dependency "rubocop-govuk", "4.9.0"
28
29
  end
@@ -0,0 +1,141 @@
1
+ require "spec_helper"
2
+ require "rack/test"
3
+ require "logger"
4
+
5
+ require "rack/logstasher"
6
+
7
+ describe Rack::Logstasher::Logger do
8
+ include Rack::Test::Methods
9
+
10
+ describe "parameters" do
11
+ def app
12
+ Rack::Logstasher::Logger.new(
13
+ proc { |_env| [200, {}, ["Inner app response"]] },
14
+ Logger.new(tmp_logfile_path),
15
+ )
16
+ end
17
+
18
+ it "adds request details to the logfile" do
19
+ get "/foo?bar=baz"
20
+
21
+ log_details = JSON.parse(last_log_line)
22
+
23
+ expect(log_details["method"]).to eq("GET")
24
+ expect(log_details["path"]).to eq("/foo")
25
+ expect(log_details["query_string"]).to eq("bar=baz")
26
+ expect(log_details["request"]).to eq("GET /foo?bar=baz HTTP/1.0")
27
+ end
28
+
29
+ it "adds a tag of 'request'" do
30
+ get "/foo?bar=baz"
31
+
32
+ log_details = JSON.parse(last_log_line)
33
+ expect(log_details["tags"]).to eq(%w[request])
34
+ end
35
+ end
36
+
37
+ describe "duration" do
38
+ def app
39
+ Rack::Logstasher::Logger.new(
40
+ proc do |_env|
41
+ sleep 0.1
42
+ [200, {}, ["Inner app response"]]
43
+ end,
44
+ Logger.new(tmp_logfile_path),
45
+ )
46
+ end
47
+
48
+ it "logs request duration" do
49
+ get "/foo"
50
+
51
+ log_details = JSON.parse(last_log_line)
52
+ expect(log_details["duration"]).to be_within(5).of(100)
53
+ end
54
+ end
55
+
56
+ describe "HTTP headers" do
57
+ context "when there are extra request headers" do
58
+ let(:extra_request_headers) { {} }
59
+
60
+ def app
61
+ Rack::Logstasher::Logger.new(
62
+ proc { |_env| [200, {}, ["Inner app response"]] },
63
+ Logger.new(tmp_logfile_path),
64
+ extra_request_headers: extra_request_headers,
65
+ )
66
+ end
67
+
68
+ it "adds specified extra request headers to the log under the given key" do
69
+ extra_request_headers["foo"] = "header_foo"
70
+ get "/something", {}, { "HTTP_FOO" => "bar" }
71
+
72
+ log_details = JSON.parse(last_log_line)
73
+
74
+ expect(log_details["header_foo"]).to eq("bar")
75
+ end
76
+
77
+ it "does not add the key if the header is missing" do
78
+ extra_request_headers["foo"] = "header_foo"
79
+ get "/something"
80
+
81
+ log_details = JSON.parse(last_log_line)
82
+
83
+ expect(log_details).not_to have_key("header_foo")
84
+ end
85
+
86
+ it "handles dashes in header name" do
87
+ extra_request_headers["Varnish-Id"] = "varnish_id"
88
+ get "/something", {}, { "HTTP_VARNISH_ID" => "1234" }
89
+
90
+ log_details = JSON.parse(last_log_line)
91
+
92
+ expect(log_details["varnish_id"]).to eq("1234")
93
+ end
94
+ end
95
+
96
+ context "when there are extra response headers" do
97
+ let(:extra_response_headers) { {} }
98
+ let(:inner_response_headers) { {} }
99
+
100
+ def app
101
+ Rack::Logstasher::Logger.new(
102
+ proc do |_env|
103
+ headers = inner_response_headers
104
+ [200, headers, ["Inner app response"]]
105
+ end,
106
+ Logger.new(tmp_logfile_path),
107
+ extra_response_headers: extra_response_headers,
108
+ )
109
+ end
110
+
111
+ it "adds specified extra response headers to the log under the given key" do
112
+ extra_response_headers["foo"] = "header_foo"
113
+ inner_response_headers["foo"] = "bar"
114
+ get "/something"
115
+
116
+ log_details = JSON.parse(last_log_line)
117
+
118
+ expect(log_details["header_foo"]).to eq("bar")
119
+ end
120
+
121
+ it "does not add the key if the header is missing" do
122
+ extra_response_headers["foo"] = "header_foo"
123
+ get "/something"
124
+
125
+ log_details = JSON.parse(last_log_line)
126
+
127
+ expect(log_details).not_to have_key("header_foo")
128
+ end
129
+
130
+ it "handles dashes in header name" do
131
+ extra_response_headers["X-Cache"] = "cache_status"
132
+ inner_response_headers["X-Cache"] = "MISS"
133
+ get "/something"
134
+
135
+ log_details = JSON.parse(last_log_line)
136
+
137
+ expect(log_details["cache_status"]).to eq("MISS")
138
+ end
139
+ end
140
+ end
141
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,10 +3,9 @@
3
3
  # Require this file using `require "spec_helper"` to ensure that it is only
4
4
  # loaded once.
5
5
 
6
-
7
6
  # Requires supporting ruby files with custom matchers and macros, etc,
8
7
  # in spec/support/ and its subdirectories.
9
- Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
8
+ Dir[File.expand_path("support/**/*.rb", __dir__)].sort.each { |f| require f }
10
9
 
11
10
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
11
  RSpec.configure do |config|
@@ -17,5 +16,5 @@ RSpec.configure do |config|
17
16
  # order dependency and want to debug it, you can fix the order by providing
18
17
  # the seed, which is printed after each run.
19
18
  # --seed 1234
20
- config.order = 'random'
19
+ config.order = "random"
21
20
  end
@@ -1,7 +1,7 @@
1
- require 'tempfile'
1
+ require "tempfile"
2
2
 
3
3
  module LogfileHelper
4
- TMP_LOGFILE = Tempfile.new('rack-logstasher-tmp-log')
4
+ TMP_LOGFILE = Tempfile.new("rack-logstasher-tmp-log")
5
5
 
6
6
  def reset_tmp_log
7
7
  TMP_LOGFILE.rewind
@@ -18,6 +18,6 @@ module LogfileHelper
18
18
  end
19
19
 
20
20
  RSpec.configuration.include(LogfileHelper)
21
- RSpec.configuration.before :each do
21
+ RSpec.configuration.before do
22
22
  reset_tmp_log
23
23
  end
metadata CHANGED
@@ -1,57 +1,63 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-logstasher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-15 00:00:00.000000000 Z
11
+ date: 2023-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rack
14
+ name: logstash-event
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '1.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '1.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: logstash-event
28
+ name: rack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '2'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '4'
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
41
  - - ">="
39
42
  - !ruby/object:Gem::Version
40
- version: '0'
43
+ version: '2'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '4'
41
47
  - !ruby/object:Gem::Dependency
42
- name: bundler
48
+ name: rack-test
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
45
- - - ">="
51
+ - - "~>"
46
52
  - !ruby/object:Gem::Version
47
- version: '0'
53
+ version: '2'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
- - - ">="
58
+ - - "~>"
53
59
  - !ruby/object:Gem::Version
54
- version: '0'
60
+ version: '2'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: rake
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -67,33 +73,33 @@ dependencies:
67
73
  - !ruby/object:Gem::Version
68
74
  version: '0'
69
75
  - !ruby/object:Gem::Dependency
70
- name: rack-test
76
+ name: rspec
71
77
  requirement: !ruby/object:Gem::Requirement
72
78
  requirements:
73
- - - ">="
79
+ - - "~>"
74
80
  - !ruby/object:Gem::Version
75
- version: '0'
81
+ version: '3'
76
82
  type: :development
77
83
  prerelease: false
78
84
  version_requirements: !ruby/object:Gem::Requirement
79
85
  requirements:
80
- - - ">="
86
+ - - "~>"
81
87
  - !ruby/object:Gem::Version
82
- version: '0'
88
+ version: '3'
83
89
  - !ruby/object:Gem::Dependency
84
- name: rspec
90
+ name: rubocop-govuk
85
91
  requirement: !ruby/object:Gem::Requirement
86
92
  requirements:
87
- - - ">="
93
+ - - '='
88
94
  - !ruby/object:Gem::Version
89
- version: '0'
95
+ version: 4.9.0
90
96
  type: :development
91
97
  prerelease: false
92
98
  version_requirements: !ruby/object:Gem::Requirement
93
99
  requirements:
94
- - - ">="
100
+ - - '='
95
101
  - !ruby/object:Gem::Version
96
- version: '0'
102
+ version: 4.9.0
97
103
  description: Rack middleware to log requests in logstash json event format. Like
98
104
  the logstasher gem, but for rack apps.
99
105
  email:
@@ -102,20 +108,22 @@ executables: []
102
108
  extensions: []
103
109
  extra_rdoc_files: []
104
110
  files:
111
+ - ".github/dependabot.yml"
112
+ - ".github/workflows/ci.yml"
105
113
  - ".gitignore"
106
114
  - ".rspec"
115
+ - ".rubocop.yml"
107
116
  - ".ruby-version"
108
117
  - CHANGELOG.md
109
118
  - Gemfile
110
- - Jenkinsfile
111
- - LICENSE.txt
119
+ - LICENCE
112
120
  - README.md
113
121
  - Rakefile
114
122
  - lib/rack/logstasher.rb
115
123
  - lib/rack/logstasher/logger.rb
116
124
  - lib/rack/logstasher/version.rb
117
125
  - rack-logstasher.gemspec
118
- - spec/logger_spec.rb
126
+ - spec/rack/logstasher/logger_spec.rb
119
127
  - spec/spec_helper.rb
120
128
  - spec/support/logfile_helper.rb
121
129
  homepage: https://github.com/alphagov/rack-logstasher
@@ -130,18 +138,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
138
  requirements:
131
139
  - - ">="
132
140
  - !ruby/object:Gem::Version
133
- version: '0'
141
+ version: '2.7'
134
142
  required_rubygems_version: !ruby/object:Gem::Requirement
135
143
  requirements:
136
144
  - - ">="
137
145
  - !ruby/object:Gem::Version
138
146
  version: '0'
139
147
  requirements: []
140
- rubygems_version: 3.0.3
148
+ rubygems_version: 3.4.4
141
149
  signing_key:
142
150
  specification_version: 4
143
151
  summary: Rack middleware to log requests in logstash json event format
144
152
  test_files:
145
- - spec/logger_spec.rb
153
+ - spec/rack/logstasher/logger_spec.rb
146
154
  - spec/spec_helper.rb
147
155
  - spec/support/logfile_helper.rb
data/Jenkinsfile DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env groovy
2
-
3
- library("govuk")
4
-
5
- node {
6
- govuk.buildProject()
7
- }
data/spec/logger_spec.rb DELETED
@@ -1,136 +0,0 @@
1
- require 'spec_helper'
2
- require 'rack/test'
3
- require 'logger'
4
-
5
- require 'rack/logstasher'
6
-
7
- describe "Logger" do
8
- include Rack::Test::Methods
9
-
10
- describe "adding request details to the logfile" do
11
- def app
12
- Rack::Logstasher::Logger.new(
13
- proc {|env|
14
- sleep @sleep_time if @sleep_time
15
- [200, {}, ["Inner app response"]]
16
- },
17
- Logger.new(tmp_logfile_path)
18
- )
19
- end
20
-
21
- it "should add request details to the logfile" do
22
- get "/foo?bar=baz"
23
-
24
- log_details = JSON.parse(last_log_line)
25
-
26
- expect(log_details['method']).to eq('GET')
27
- expect(log_details['path']).to eq('/foo')
28
- expect(log_details['query_string']).to eq('bar=baz')
29
- expect(log_details['request']).to eq('GET /foo?bar=baz ') # env['SERVER_PROTOCOL'] is not set under rack-test
30
- end
31
-
32
- it "should add request duration" do
33
- @sleep_time = 0.1
34
- get "/foo"
35
-
36
- log_details = JSON.parse(last_log_line)
37
-
38
- expect(log_details['duration']).to be_within(5).of(100)
39
- end
40
-
41
- it "should add a tag of 'request'" do
42
- get "/foo?bar=baz"
43
-
44
- log_details = JSON.parse(last_log_line)
45
- expect(log_details['tags']).to eq(['request'])
46
- end
47
-
48
- describe "adding extra headers to the log" do
49
- before :each do
50
- @extra_request_headers = {}
51
- @extra_response_headers = {}
52
- end
53
- def app
54
- Rack::Logstasher::Logger.new(
55
- proc {|env|
56
- headers = @inner_response_headers || {}
57
- [200, headers, ["Inner app response"]]
58
- },
59
- Logger.new(tmp_logfile_path),
60
- :extra_request_headers => @extra_request_headers,
61
- :extra_response_headers => @extra_response_headers
62
- )
63
- end
64
-
65
- context "extra request headers" do
66
- it "should add specified extra request headers to the log under the given key" do
67
- @extra_request_headers["foo"] = "header_foo"
68
- get "/something", {}, {"HTTP_FOO" => "bar"}
69
-
70
- log_details = JSON.parse(last_log_line)
71
-
72
- expect(log_details['header_foo']).to eq('bar')
73
- end
74
-
75
- it "should not add the key if the header is missing" do
76
- @extra_request_headers["foo"] = "header_foo"
77
- get "/something"
78
-
79
- log_details = JSON.parse(last_log_line)
80
-
81
- expect(log_details).not_to have_key('header_foo')
82
- end
83
-
84
- it "should handle dashes in header name" do
85
- @extra_request_headers["Varnish-Id"] = "varnish_id"
86
- get "/something", {}, {"HTTP_VARNISH_ID" => "1234"}
87
-
88
- log_details = JSON.parse(last_log_line)
89
-
90
- expect(log_details['varnish_id']).to eq('1234')
91
- end
92
- end
93
-
94
- context "extra response headers" do
95
- it "should add specified extra response headers to the log under the given key" do
96
- @extra_response_headers["foo"] = "header_foo"
97
- @inner_response_headers = {"Foo" => "bar"}
98
- get "/something"
99
-
100
- log_details = JSON.parse(last_log_line)
101
-
102
- expect(log_details['header_foo']).to eq('bar')
103
- end
104
-
105
- it "should not add the key if the header is missing" do
106
- @extra_response_headers["foo"] = "header_foo"
107
- get "/something"
108
-
109
- log_details = JSON.parse(last_log_line)
110
-
111
- expect(log_details).not_to have_key('header_foo')
112
- end
113
-
114
- it "should handle dashes in header name" do
115
- @extra_response_headers["X-Cache"] = "cache_status"
116
- @inner_response_headers = {"X-Cache" => "MISS"}
117
- get "/something"
118
-
119
- log_details = JSON.parse(last_log_line)
120
-
121
- expect(log_details['cache_status']).to eq('MISS')
122
- end
123
-
124
- it "should match header in a case-insensitive fashion" do
125
- @extra_response_headers["X-CacHe"] = "cache_status"
126
- @inner_response_headers = {"x-cAche" => "MISS"}
127
- get "/something"
128
-
129
- log_details = JSON.parse(last_log_line)
130
-
131
- expect(log_details['cache_status']).to eq('MISS')
132
- end
133
- end
134
- end
135
- end
136
- end