datadog-json_logger 0.1.3 → 0.1.5
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/datadog-json_logger.gemspec +7 -0
- data/lib/datadog/loggers/version.rb +1 -1
- data/lib/datadog/sinatra_middleware.rb +19 -4
- data/sig/datadog/sinatra_middleware.rbs +4 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25aa55fc996314835493e0d134ffdf5c44080a36a27a3b82d21977d19623c565
|
4
|
+
data.tar.gz: 49c7e190cafd5dd5291afdb8f9baf0d0b655179e45763920fe188590f9692693
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c8c19712e5653add057734e39f22c6266fd60649868cb297ac5e79aa179c695645c12111b0a1d072af24707907472720d7618872a18278926c18ab5f5bd31ef
|
7
|
+
data.tar.gz: 8975469ccdefd6b762ef0605f8c1baa0923d3a6a5d8e15f24eaa1b8bd27fd2be3b93d453fa9c23e1340c44d399a06e2dceacfd9725f26b4cea216163facba413
|
data/datadog-json_logger.gemspec
CHANGED
@@ -16,6 +16,13 @@ Gem::Specification.new do |spec|
|
|
16
16
|
|
17
17
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
18
|
|
19
|
+
if ENV.fetch('GEM_PUSHER', 'default') == 'github'
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.pkg.github.com/buyco'
|
21
|
+
else
|
22
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
23
|
+
end
|
24
|
+
|
25
|
+
|
19
26
|
spec.metadata["homepage_uri"] = spec.homepage
|
20
27
|
spec.metadata["source_code_uri"] = "https://github.com/eth3rnit3/datadog-json_logger"
|
21
28
|
spec.metadata["changelog_uri"] = "https://github.com/eth3rnit3/datadog-json_logger/blob/main/CHANGELOG.md"
|
@@ -16,16 +16,21 @@ module Datadog
|
|
16
16
|
class SinatraMiddleware
|
17
17
|
attr_reader :app, :logger
|
18
18
|
|
19
|
-
def initialize(app, logger)
|
19
|
+
def initialize(app, logger, opt = {})
|
20
20
|
@app = app
|
21
21
|
@logger = logger
|
22
|
+
@raise_exceptions = opt.fetch(:raise_exceptions, false)
|
22
23
|
end
|
23
24
|
|
24
25
|
def call(env)
|
25
26
|
request = Rack::Request.new(env)
|
26
27
|
start_time = Time.now
|
27
28
|
|
28
|
-
status, headers, body =
|
29
|
+
status, headers, body = if @raise_exceptions
|
30
|
+
app.call(env)
|
31
|
+
else
|
32
|
+
safely_process_request(env)
|
33
|
+
end
|
29
34
|
end_time = Time.now
|
30
35
|
|
31
36
|
log_request(request, env, status, headers, start_time, end_time)
|
@@ -39,8 +44,8 @@ module Datadog
|
|
39
44
|
|
40
45
|
def safely_process_request(env)
|
41
46
|
app.call(env)
|
42
|
-
rescue StandardError
|
43
|
-
[500, { "Content-Type": "
|
47
|
+
rescue StandardError => e
|
48
|
+
[500, { "Content-Type": "application/json" }, [e.class.name, e.message].join(": ")]
|
44
49
|
end
|
45
50
|
|
46
51
|
def log_request(request, env, status, headers, start_time, end_time)
|
@@ -49,6 +54,7 @@ module Datadog
|
|
49
54
|
request_ip: request.ip,
|
50
55
|
method: request.request_method,
|
51
56
|
controller: env["sinatra.controller_name"],
|
57
|
+
resource: env["sinatra.resource_name"] || env["sinatra.controller_name"],
|
52
58
|
action: env["sinatra.action_name"],
|
53
59
|
path: request.path,
|
54
60
|
params: parse_query(request.query_string),
|
@@ -57,9 +63,18 @@ module Datadog
|
|
57
63
|
duration: calculate_duration(start_time, end_time)
|
58
64
|
}
|
59
65
|
|
66
|
+
log_data[:message] = message(log_data)
|
67
|
+
|
60
68
|
logger.info(log_data)
|
61
69
|
end
|
62
70
|
|
71
|
+
def message(log_data)
|
72
|
+
"Received #{log_data[:method]} request from #{log_data[:request_ip]} " \
|
73
|
+
"to #{log_data[:controller]}##{log_data[:action]} at #{log_data[:path]} " \
|
74
|
+
"with params #{log_data[:params].to_json}. Responded with status #{log_data[:status]} " \
|
75
|
+
"in #{log_data[:duration]}ms. Content-Type: #{log_data[:format]}."
|
76
|
+
end
|
77
|
+
|
63
78
|
def calculate_duration(start_time, end_time)
|
64
79
|
((end_time - start_time) * 1000).round # Duration in milliseconds
|
65
80
|
end
|
@@ -7,14 +7,13 @@ end
|
|
7
7
|
module Datadog
|
8
8
|
class SinatraMiddleware
|
9
9
|
@app: untyped
|
10
|
-
|
11
10
|
@logger: Logger
|
11
|
+
@raise_exceptions: bool
|
12
12
|
|
13
13
|
attr_reader app: untyped
|
14
|
-
|
15
14
|
attr_reader logger: Logger
|
16
15
|
|
17
|
-
def initialize: (untyped app, Logger logger) -> void
|
16
|
+
def initialize: (untyped app, Logger logger, ?Hash[Symbol, untyped] opt) -> void
|
18
17
|
|
19
18
|
def call: (untyped env) -> untyped
|
20
19
|
|
@@ -23,6 +22,8 @@ module Datadog
|
|
23
22
|
def safely_process_request: (untyped env) -> untyped
|
24
23
|
|
25
24
|
def log_request: (Rack::Request request, untyped env, String status, Hash[String, String] headers, Time start_time, Time end_time) -> untyped
|
25
|
+
|
26
|
+
def message: (Hash[Symbol, untyped] log_data) -> String
|
26
27
|
|
27
28
|
def calculate_duration: (Time start_time, Time end_time) -> Integer
|
28
29
|
|