rack-logstasher 1.0.2 → 1.1.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 +4 -4
- data/.github/dependabot.yml +10 -0
- data/.github/workflows/ci.yml +35 -0
- data/.rspec +1 -1
- data/.rubocop.yml +19 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -1
- data/{LICENSE.txt → LICENCE} +2 -2
- data/README.md +4 -0
- data/Rakefile +4 -2
- data/lib/rack/logstasher/logger.rb +17 -18
- data/lib/rack/logstasher/version.rb +1 -1
- data/rack-logstasher.gemspec +12 -11
- data/spec/rack/logstasher/logger_spec.rb +151 -0
- data/spec/spec_helper.rb +2 -3
- data/spec/support/logfile_helper.rb +3 -3
- metadata +33 -31
- data/Jenkinsfile +0 -7
- data/spec/logger_spec.rb +0 -136
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d877ce60ae84d0a7532df7b8e89c8947fc7fcb4d6bd3733894c5761bc877a7c
|
4
|
+
data.tar.gz: 19b5da86c58508f8031737a65f4244ae7f1d91c4451a0587ae7559d0fa43b3d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4607fc05644c2a62c82f326aa933f832f11f788a1ba462f5cd4aa48beb721544347d9f9ea3d2f59969769a1c2403da6fdbdfaa35da0238e9c24f6fcb951c0b00
|
7
|
+
data.tar.gz: d47fe6f832f458d24e4ad6cf91d3235a93ccd5e3c311c6d16d8af3e13a64bf6abe08ee359e5c90a57d1419965e8517fd26b57c0fd1c5dab4906a99f0a66bb6fa
|
@@ -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 ]
|
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.yaml@main
|
34
|
+
secrets:
|
35
|
+
GEM_HOST_API_KEY: ${{ secrets.ALPHAGOV_RUBYGEMS_API_KEY }}
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--
|
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
|
+
2.7.6
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/{LICENSE.txt → LICENCE}
RENAMED
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
The MIT License (MIT)
|
2
2
|
|
3
|
-
|
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
data/Rakefile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
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
|
-
|
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
|
-
:
|
20
|
-
:
|
21
|
-
:
|
22
|
-
:
|
23
|
-
:
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
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(
|
43
|
-
msg = event.to_json
|
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[
|
57
|
-
line << "?#{env[
|
58
|
-
line << " #{env[
|
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
|
data/rack-logstasher.gemspec
CHANGED
@@ -1,28 +1,29 @@
|
|
1
|
-
|
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
|
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 =
|
12
|
-
spec.summary =
|
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.
|
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 = [
|
20
|
+
spec.require_paths = %w[lib]
|
20
21
|
|
22
|
+
spec.add_dependency "logstash-event", "~> 1.2"
|
21
23
|
spec.add_dependency "rack", "~> 2.0"
|
22
|
-
spec.add_dependency "logstash-event"
|
23
24
|
|
24
|
-
spec.add_development_dependency "
|
25
|
+
spec.add_development_dependency "rack-test", "~> 2"
|
25
26
|
spec.add_development_dependency "rake"
|
26
|
-
spec.add_development_dependency "
|
27
|
-
spec.add_development_dependency "
|
27
|
+
spec.add_development_dependency "rspec", "~> 3"
|
28
|
+
spec.add_development_dependency "rubocop-govuk", "4.8.0"
|
28
29
|
end
|
@@ -0,0 +1,151 @@
|
|
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
|
+
|
140
|
+
it "matches header in a case-insensitive fashion" do
|
141
|
+
extra_response_headers["X-CacHe"] = "cache_status"
|
142
|
+
inner_response_headers["x-cAche"] = "MISS"
|
143
|
+
get "/something"
|
144
|
+
|
145
|
+
log_details = JSON.parse(last_log_line)
|
146
|
+
|
147
|
+
expect(log_details["cache_status"]).to eq("MISS")
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
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("
|
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 =
|
19
|
+
config.order = "random"
|
21
20
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require "tempfile"
|
2
2
|
|
3
3
|
module LogfileHelper
|
4
|
-
TMP_LOGFILE = Tempfile.new(
|
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
|
21
|
+
RSpec.configuration.before do
|
22
22
|
reset_tmp_log
|
23
23
|
end
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-logstasher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.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:
|
11
|
+
date: 2022-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
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
|
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
|
26
|
+
version: '1.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
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.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
40
|
+
version: '2.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rack-test
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '2'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '2'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,33 +67,33 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '3'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '3'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: rubocop-govuk
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '='
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: 4.8.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '='
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: 4.8.0
|
97
97
|
description: Rack middleware to log requests in logstash json event format. Like
|
98
98
|
the logstasher gem, but for rack apps.
|
99
99
|
email:
|
@@ -102,20 +102,22 @@ executables: []
|
|
102
102
|
extensions: []
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
|
+
- ".github/dependabot.yml"
|
106
|
+
- ".github/workflows/ci.yml"
|
105
107
|
- ".gitignore"
|
106
108
|
- ".rspec"
|
109
|
+
- ".rubocop.yml"
|
107
110
|
- ".ruby-version"
|
108
111
|
- CHANGELOG.md
|
109
112
|
- Gemfile
|
110
|
-
-
|
111
|
-
- LICENSE.txt
|
113
|
+
- LICENCE
|
112
114
|
- README.md
|
113
115
|
- Rakefile
|
114
116
|
- lib/rack/logstasher.rb
|
115
117
|
- lib/rack/logstasher/logger.rb
|
116
118
|
- lib/rack/logstasher/version.rb
|
117
119
|
- rack-logstasher.gemspec
|
118
|
-
- spec/logger_spec.rb
|
120
|
+
- spec/rack/logstasher/logger_spec.rb
|
119
121
|
- spec/spec_helper.rb
|
120
122
|
- spec/support/logfile_helper.rb
|
121
123
|
homepage: https://github.com/alphagov/rack-logstasher
|
@@ -130,18 +132,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
132
|
requirements:
|
131
133
|
- - ">="
|
132
134
|
- !ruby/object:Gem::Version
|
133
|
-
version: '
|
135
|
+
version: '2.7'
|
134
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
137
|
requirements:
|
136
138
|
- - ">="
|
137
139
|
- !ruby/object:Gem::Version
|
138
140
|
version: '0'
|
139
141
|
requirements: []
|
140
|
-
rubygems_version: 3.
|
142
|
+
rubygems_version: 3.3.24
|
141
143
|
signing_key:
|
142
144
|
specification_version: 4
|
143
145
|
summary: Rack middleware to log requests in logstash json event format
|
144
146
|
test_files:
|
145
|
-
- spec/logger_spec.rb
|
147
|
+
- spec/rack/logstasher/logger_spec.rb
|
146
148
|
- spec/spec_helper.rb
|
147
149
|
- spec/support/logfile_helper.rb
|
data/Jenkinsfile
DELETED
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
|