logjam_agent 0.32.3 → 0.33.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: '001856dce13e72d273c32137e0cbe7d4c004aa22c788e511b9c409065394de77'
4
- data.tar.gz: d6b17a001250b7739ce3fe0e5096bbc57fea7f0632eca09859d28af53b94e368
3
+ metadata.gz: 29e2045e24e8a76d6cd8d13c4521562f237849880f39eec65c6be243b275cd06
4
+ data.tar.gz: 264a22f43eafaca127977d689ede51a657c4aa9403f5339528c56647f0a502c7
5
5
  SHA512:
6
- metadata.gz: 29bec994d20a63997d4b43757cc9a88703cd31208c8146bada5aada8d6bfb7de831bb262ad798441ff42423c59d4aa8acdaf8f730295d15fce98eb3da39e173f
7
- data.tar.gz: a27dfebcf4c233fe99b603179cf28a0edc39233b9baca0b3d9df5606e4f3629cd40f8bd832229378cf08c21c51b01963b31474299d6bc5af5289dbb96a11a9af
6
+ metadata.gz: 4fb90fd911820483409d59f8b0d8f272fe06b55460127899edbfc77da57cf064b32c633dee61bd1bf113acbf205487689bfdc43d623c6d8faaea4bbff9523bd5
7
+ data.tar.gz: 86470089e012dbf3462725708e65e0deb3a5d8d9cb1268452fb051948d80f55ce6c2361ffde3064aff41f8c9049a5d3452cfad4cbb8c7e87e455142289aed8f2
data/README.md CHANGED
@@ -10,7 +10,8 @@ Has experimental support for Sinatra.
10
10
  Currently only one mechanism is available for data transport:
11
11
  ZeroMQ. Support for AMQP has been dropped.
12
12
 
13
- [![Travis](https://travis-ci.org/skaes/logjam_agent.svg?branch=master)](https://travis-ci.org/github/skaes/logjam_agent)
13
+
14
+ ![Build](https://github.com/skaes/logjam_agent/actions/workflows/run-tests.yml/badge.svg)
14
15
 
15
16
 
16
17
  ## Usage
@@ -50,9 +50,11 @@ module LogjamAgent
50
50
  env_name = env["logjam_agent.environment_name"] || LogjamAgent.environment_name
51
51
  caller_id = env["HTTP_X_LOGJAM_CALLER_ID"] || ""
52
52
  caller_action = env["HTTP_X_LOGJAM_ACTION"] || ""
53
+ trace_id = env["HTTP_X_LOGJAM_TRACE_ID"] || ""
53
54
  extra_fields = {}
54
55
  extra_fields[:caller_id] = caller_id if caller_id.present?
55
56
  extra_fields[:caller_action] = caller_action if caller_action.present?
57
+ extra_fields[:trace_id] = trace_id if trace_id.present?
56
58
  LogjamAgent.start_request(app_name, env_name, extra_fields)
57
59
  end
58
60
 
@@ -32,23 +32,16 @@ module LogjamAgent
32
32
 
33
33
  def call_app(request, env)
34
34
  start_time = Time.now
35
- start_time_header = env['HTTP_X_STARTTIME']
36
- if start_time_header
37
- if start_time_header =~ /\At=(\d+)\z/
38
- # HTTP_X_STARTTIME is microseconds since the epoch (UTC)
39
- http_start_time = Time.at($1.to_f / 1_000_000.0)
40
- elsif start_time_header =~ /\Ats=(\d+)(?:\.(\d+))?\z/
41
- # HTTP_X_STARTTIME is seconds since the epoch (UTC) with a milliseconds resolution
42
- http_start_time = Time.at($1.to_f + $2.to_f / 1000)
43
- end
44
-
45
- if http_start_time && (wait_time_ms = (start_time - http_start_time) * 1000) > 0
35
+ wait_time_ms = 0.0
36
+ if http_start_time = extract_http_start_time(env)
37
+ wait_time_ms = (start_time - http_start_time) * 1000
38
+ if wait_time_ms > 0
46
39
  start_time = http_start_time
40
+ else
41
+ wait_time_ms = 0.0
47
42
  end
48
- else
49
- wait_time_ms = 0.0
50
43
  end
51
- before_dispatch(request, env, start_time)
44
+ before_dispatch(request, env, start_time, wait_time_ms)
52
45
  result = @app.call(env)
53
46
  rescue ActionDispatch::RemoteIp::IpSpoofAttackError
54
47
  result = [403, {}, ['Forbidden']]
@@ -57,6 +50,20 @@ module LogjamAgent
57
50
  after_dispatch(env, result, run_time_ms, wait_time_ms)
58
51
  end
59
52
 
53
+ def extract_http_start_time(env)
54
+ start_time_header = env['HTTP_X_STARTTIME']
55
+ if start_time_header
56
+ if start_time_header =~ /\At=(\d+)\z/
57
+ # HTTP_X_STARTTIME is microseconds since the epoch (UTC)
58
+ return Time.at($1.to_f / 1_000_000.0)
59
+ elsif start_time_header =~ /\Ats=(\d+)(?:\.(\d+))?\z/
60
+ # HTTP_X_STARTTIME is seconds since the epoch (UTC) with a milliseconds resolution
61
+ return Time.at($1.to_f + $2.to_f / 1000)
62
+ end
63
+ end
64
+ return nil
65
+ end
66
+
60
67
  def compute_tags(request)
61
68
  @taggers.collect do |tag|
62
69
  case tag
@@ -78,7 +85,7 @@ module LogjamAgent
78
85
  false
79
86
  end
80
87
 
81
- def before_dispatch(request, env, start_time)
88
+ def before_dispatch(request, env, start_time, wait_time_ms)
82
89
  logger.formatter.reset_attributes if logger.formatter.respond_to?(:reset_attributes)
83
90
  TimeBandits.reset
84
91
  Thread.current.thread_variable_set(:time_bandits_completed_info, nil)
@@ -90,6 +97,7 @@ module LogjamAgent
90
97
 
91
98
  logjam_request.start_time = start_time
92
99
  logjam_fields = logjam_request.fields
100
+ logjam_fields[:wait_time] = wait_time_ms if wait_time_ms > 0.0
93
101
  spoofed = nil
94
102
  ip = nil
95
103
  begin
@@ -125,7 +133,6 @@ module LogjamAgent
125
133
 
126
134
  if wait_time_ms < 0
127
135
  warn LogjamAgent::NegativeWaitTime.new("#{wait_time_ms} ms")
128
- wait_time_ms = 0.0
129
136
  end
130
137
 
131
138
  message = "Completed #{status} #{::Rack::Utils::HTTP_STATUS_CODES[status]} in %.1fms" % run_time_ms
@@ -135,7 +142,6 @@ module LogjamAgent
135
142
  ActiveSupport::LogSubscriber.flush_all!
136
143
  request_info = { :total_time => run_time_ms, :code => status }
137
144
  request_info[:view_time] = view_time if view_time
138
- request_info[:wait_time] = wait_time_ms if wait_time_ms > 0
139
145
  logjam_request.fields.merge!(request_info)
140
146
 
141
147
  env["time_bandits.metrics"] = TimeBandits.metrics
@@ -90,9 +90,9 @@ module LogjamAgent
90
90
  # Rails 5 fires on_load events multiple times, so we need to protect against endless recursion
91
91
  next if ActionController::TestCase::Behavior.instance_methods.include?(:process_without_logjam)
92
92
  module ActionController::TestCase::Behavior
93
- def process_with_logjam(*args)
93
+ def process_with_logjam(action, **opts)
94
94
  LogjamAgent.start_request
95
- process_without_logjam(*args)
95
+ process_without_logjam(action, **opts)
96
96
  ensure
97
97
  LogjamAgent.finish_request
98
98
  end
@@ -17,6 +17,7 @@ module LogjamAgent
17
17
  @uuid = LogjamAgent.generate_uuid
18
18
  @fields = initial_fields.merge(:request_id => @uuid, :host => LogjamAgent.hostname,
19
19
  :process_id => Process.pid, :lines => @lines)
20
+ @fields[:trace_id] ||= @uuid
20
21
  unless (revision = LogjamAgent.application_revision).blank?
21
22
  @fields[:revision] = revision
22
23
  end
@@ -70,6 +71,10 @@ module LogjamAgent
70
71
  @fields[:caller_action]
71
72
  end
72
73
 
74
+ def trace_id
75
+ @fields[:trace_id]
76
+ end
77
+
73
78
  def add_line(severity, timestamp, message)
74
79
  @mutex.synchronize do
75
80
  if @bytes_all_lines > @max_bytes_all_lines
@@ -1,3 +1,3 @@
1
1
  module LogjamAgent
2
- VERSION = "0.32.3"
2
+ VERSION = "0.33.1"
3
3
  end
data/test/request_test.rb CHANGED
@@ -44,6 +44,16 @@ module LogjamAgent
44
44
  assert_equal TRUNCATED_LINE, lines(@request)[1][2]
45
45
  end
46
46
 
47
+ def test_sets_trace_id_to_request_id_if_not_passed_in
48
+ assert_equal @request.uuid, @request.fields[:trace_id]
49
+ end
50
+
51
+ def test_sets_trace_id_to_passed_in_field_value
52
+ trace_id = "murks"
53
+ request = Request.new("app", "env", {trace_id: trace_id})
54
+ assert_equal trace_id, request.fields[:trace_id]
55
+ end
56
+
47
57
  private
48
58
 
49
59
  def lines(request)
data/test/sinatra_test.rb CHANGED
@@ -37,6 +37,7 @@ module LogjamAgent
37
37
  assert_kind_of String, payload["started_at"]
38
38
  assert_kind_of Integer, payload["started_ms"]
39
39
  assert_kind_of String, payload["ip"]
40
+ assert_nil payload["wait_time"]
40
41
  # assert_kind_of Float, payload["view_time"]
41
42
  lines = payload["lines"]
42
43
  assert_match(/Started GET.*password=\[FILTERED\]/, lines[0][2])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logjam_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.32.3
4
+ version: 0.33.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Kaes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-19 00:00:00.000000000 Z
11
+ date: 2022-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -275,16 +275,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
275
275
  - !ruby/object:Gem::Version
276
276
  version: '0'
277
277
  requirements: []
278
- rubygems_version: 3.2.7
278
+ rubygems_version: 3.3.4
279
279
  signing_key:
280
280
  specification_version: 4
281
281
  summary: Logjam client library to be used with logjam
282
282
  test_files:
283
+ - test/request_test.rb
283
284
  - test/sinatra_app.rb
284
- - test/sinatra_classic_test.rb
285
285
  - test/sinatra_classic_app.rb
286
- - test/request_test.rb
287
- - test/zmq_forwarder_test.rb
288
- - test/util_test.rb
289
- - test/test_helper.rb
286
+ - test/sinatra_classic_test.rb
290
287
  - test/sinatra_test.rb
288
+ - test/test_helper.rb
289
+ - test/util_test.rb
290
+ - test/zmq_forwarder_test.rb