semlogr-rack 0.1.0 → 0.1.1
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/CHANGELOG.md +5 -0
- data/lib/semlogr/rack/request_correlator.rb +33 -0
- data/lib/semlogr/rack/request_logger.rb +10 -2
- data/lib/semlogr/rack/version.rb +1 -1
- data/lib/semlogr/rack.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58aca5397f40069689c71225fb0b7902de8912b7
|
4
|
+
data.tar.gz: 77c856b53fd9943fea5557d307f15e3cca1fc79a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ceef19026b527ab771bd4ce8c394cbb6e8cc7b2e6f8d7496ad9f0cfcda0d69c8d9c0fb7932cb46e04cd264addd3a4091bef173c63694b96ea7ea798fb065ba0
|
7
|
+
data.tar.gz: 30a3e4529dc6e76ec8cba1e0580cea031dceb362b9386a2841c16f04809ef8009a1e6466e59c522cd45c0474783d62bea73ddaa6f9744e52216625c40a8f0cc7
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
### 0.1.1
|
4
|
+
|
5
|
+
- Add support for filtering requests by path for the request logger
|
6
|
+
- Add log correlator middleware which provides the ability to propogate a correlation id from a request header to the Semlogr ambient log context
|
7
|
+
|
3
8
|
### 0.1.0
|
4
9
|
|
5
10
|
- Initial release
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'semlogr'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module Semlogr
|
5
|
+
module Rack
|
6
|
+
class RequestCorrelator
|
7
|
+
def initialize(app, opts = {})
|
8
|
+
@app = app
|
9
|
+
@id_header = opts[:id_header] || 'X-Correlation-Id'
|
10
|
+
@id_generator = opts[:id_generator] || -> { SecureRandom.uuid }
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
id_header = env_header_name(@id_header)
|
15
|
+
correlation_id = env[id_header] || @id_generator.call
|
16
|
+
status, headers, body =
|
17
|
+
Semlogr::Context::LogContext.push_property(correlation_id: correlation_id) do
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
headers[@id_header] = correlation_id
|
22
|
+
|
23
|
+
[status, headers, body]
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def env_header_name(header)
|
29
|
+
'HTTP_' + header.tr('-', '_').upcase
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,12 +1,16 @@
|
|
1
1
|
module Semlogr
|
2
2
|
module Rack
|
3
3
|
class RequestLogger
|
4
|
-
def initialize(app, logger
|
4
|
+
def initialize(app, logger: nil, path_filters: [])
|
5
5
|
@app = app
|
6
6
|
@logger = logger
|
7
|
+
@path_filters = path_filters
|
7
8
|
end
|
8
9
|
|
9
10
|
def call(env)
|
11
|
+
path = env['REQUEST_URI']
|
12
|
+
return @app.call(env) if filtered?(path)
|
13
|
+
|
10
14
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
11
15
|
status, headers, body = @app.call(env)
|
12
16
|
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
@@ -14,7 +18,7 @@ module Semlogr
|
|
14
18
|
logger.info(
|
15
19
|
'HTTP {method} {path} - {status} ({duration}s)',
|
16
20
|
method: env['REQUEST_METHOD'],
|
17
|
-
path:
|
21
|
+
path: path,
|
18
22
|
status: status,
|
19
23
|
duration: (finish - start).round(4)
|
20
24
|
)
|
@@ -27,6 +31,10 @@ module Semlogr
|
|
27
31
|
def logger
|
28
32
|
@logger || Semlogr.logger
|
29
33
|
end
|
34
|
+
|
35
|
+
def filtered?(path)
|
36
|
+
@path_filters.any? { |filter| filter =~ path }
|
37
|
+
end
|
30
38
|
end
|
31
39
|
end
|
32
40
|
end
|
data/lib/semlogr/rack/version.rb
CHANGED
data/lib/semlogr/rack.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: semlogr-rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Sedich
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03
|
11
|
+
date: 2018-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: semlogr
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- bin/console
|
113
113
|
- bin/setup
|
114
114
|
- lib/semlogr/rack.rb
|
115
|
+
- lib/semlogr/rack/request_correlator.rb
|
115
116
|
- lib/semlogr/rack/request_logger.rb
|
116
117
|
- lib/semlogr/rack/version.rb
|
117
118
|
- semlogr-rack.gemspec
|