plain_apm 0.9.0 → 0.9.2

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: 86351e7cfbfef5b9e4916152f8f7638f3d25ec63eea2228552a7ed18a30af210
4
- data.tar.gz: 3e2054248c4affa80a1a563fb340310fbeeac0d75caf9fc77dadcf1c14a9f975
3
+ metadata.gz: 22fd1fe0f960151ee14b8a6bfc21c960533baf3f6a6eb86666f0f10253213c74
4
+ data.tar.gz: 2ec36a0a3d381563f6b7d48dfb54ea01d42f1a06576436c7613665c934167221
5
5
  SHA512:
6
- metadata.gz: 594f73225b32f2b172251c848b176c1c8fc1370a45d0804bd3741be82eff94cb26fc5526d290127591c274ee886723782890bc1be095b00b158f85331c366f2c
7
- data.tar.gz: 6435cb48e14b99c36b1457e28eb343a65618cbda9a0a014e15031b3e08b17aa6da0e15dd3cffa5808df4b44d5db19b180c102103320c231b95d1da1a39bf14c2
6
+ metadata.gz: e75c9b55fa3a4b3bbde453fef90239f0ed2daea79917115fb41a470fd387bfd7ecf15d6cab45bdc8f1f4fa7c162ea9f5e65ba7455b4414073e5ab106f13d32a4
7
+ data.tar.gz: e86597538c045928dff96133dba5dfff9d8db15f0caa568d4a3c4db15a21874943e0e0fd567aa57e32735dde121e9314d0883e929a106bd29c7ade6ba7ec1144
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "singleton"
4
- require "json"
5
4
 
6
5
  module PlainApm
7
6
  class Agent
@@ -15,48 +14,67 @@ module PlainApm
15
14
  instance.start
16
15
  end
17
16
 
18
- def self.stop
19
- instance.stop
20
- end
21
-
22
17
  def collect(event)
23
18
  return unless @config.enabled
24
19
 
20
+ publisher_start if @pid != $$
21
+
25
22
  @events << event
26
23
  end
27
24
 
28
25
  def start
29
- # Already running
30
- return unless @publisher.nil?
26
+ if !defined?(@started)
27
+ @started = true
28
+ else
29
+ return
30
+ end
31
31
 
32
32
  configure
33
33
 
34
34
  return unless @config.enabled
35
35
 
36
+ warn("PlainAPM agent enabled.")
37
+
38
+ setup_at_exit_hooks
39
+ publisher_start
40
+ install_hooks
41
+ end
42
+
43
+ private
44
+
45
+ def stop
46
+ uninstall_hooks
47
+ publisher_shutdown
48
+ end
49
+
50
+ def publisher_start
51
+ # Store PID for fork detection
52
+ @pid = $$
53
+
54
+ # Already running
55
+ return if @publisher&.alive?
56
+
36
57
  # TODO: sized queue w/ a timeout.
37
58
  @events = Thread::Queue.new
38
59
 
39
60
  # TODO: Multiple threads
40
61
  @publisher = Thread.new { publisher_loop }
62
+ end
41
63
 
42
- install_hooks
43
-
44
- # TODO: add a cleaner shutdown.
64
+ def setup_at_exit_hooks
45
65
  at_exit { stop }
46
66
  end
47
67
 
48
- def stop
68
+ def publisher_shutdown
49
69
  return if @publisher.nil?
50
70
 
51
- uninstall_hooks
52
-
71
+ # FIXME: raise in / kill the threads after a pre-determined timeout not
72
+ # to block
53
73
  @events << nil
54
74
  @publisher.join
55
75
  @publisher = nil
56
76
  end
57
77
 
58
- private
59
-
60
78
  def configure
61
79
  @config = Config.new
62
80
  end
@@ -95,10 +113,12 @@ module PlainApm
95
113
  loop do
96
114
  event = @events.pop
97
115
 
98
- break if event.nil?
116
+ Thread.exit if event.nil?
117
+
118
+ meta = { queue: @events.size, pid: $$, thread: Thread.current.object_id }
99
119
 
100
- # TODO: event serialization
101
- _response, _error, _retriable = transport.deliver(JSON.generate(event))
120
+ # TODO: event serialization, batching
121
+ _response, _error, _retriable = transport.deliver(event, meta)
102
122
 
103
123
  # TODO: retries / drops
104
124
  end
@@ -10,14 +10,12 @@ module PlainApm
10
10
  @enabled = enabled? && key_present?
11
11
  @endpoint = ENV["PLAIN_APM_ENDPOINT"] || DEFAULT_EVENT_ENDPOINT
12
12
  @app_key = ENV["PLAIN_APM_APP_KEY"]
13
-
14
- warn("PlainAPM agent enabled.") if enabled
15
13
  end
16
14
 
17
15
  private
18
16
 
19
17
  def enabled?
20
- (production? || force_enabled?) && !force_disabled?
18
+ ((production? && !interactive?) || force_enabled?) && !force_disabled?
21
19
  end
22
20
 
23
21
  def key_present?
@@ -28,6 +26,10 @@ module PlainApm
28
26
  (ENV["RAILS_ENV"] || ENV["RACK_ENV"]) == "production"
29
27
  end
30
28
 
29
+ def interactive?
30
+ $stdout.tty?
31
+ end
32
+
31
33
  # Allow to start in e.g. development.
32
34
  def force_enabled?
33
35
  ENV["PLAIN_APM_ENABLED"] == "1"
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "net/http"
4
4
  require "json"
5
+ require "zlib"
5
6
 
6
7
  module PlainApm
7
8
  class Transport
@@ -47,10 +48,7 @@ module PlainApm
47
48
 
48
49
  http.open_timeout = HTTP_OPEN_TIMEOUT_SECONDS
49
50
  http.read_timeout = HTTP_READ_TIMEOUT_SECONDS
50
-
51
- if RUBY_VERSION >= "2.6.0"
52
- http.write_timeout = HTTP_WRITE_TIMEOUT_SECONDS
53
- end
51
+ http.write_timeout = HTTP_WRITE_TIMEOUT_SECONDS
54
52
 
55
53
  at_exit { shutdown }
56
54
  end
@@ -60,9 +58,9 @@ module PlainApm
60
58
  #
61
59
  # @param data [String] serialized payload to POST
62
60
  # @return [Array] [response, error, retriable]
63
- def deliver(data)
61
+ def deliver(data, meta = {})
64
62
  http_response do
65
- http_request(http, uri.path, data)
63
+ http_request(http, uri.path, data, meta)
66
64
  end
67
65
  end
68
66
 
@@ -82,16 +80,21 @@ module PlainApm
82
80
  http.finish if http.started?
83
81
  end
84
82
 
85
- def http_request(http, path, body)
86
- request = Net::HTTP::Post.new(path, http_headers)
87
- http.request(request, body)
83
+ def http_request(http, path, body, meta)
84
+ request = Net::HTTP::Post.new(path, http_headers(meta))
85
+ http.request(request, Zlib::Deflate.deflate(JSON.generate(body)))
88
86
  end
89
87
 
90
- def http_headers
88
+ def http_headers(meta)
89
+ meta_headers = meta.map do |k, v|
90
+ ["X-PlainApm-#{k.to_s.split("_").map(&:capitalize).join("-")}", v.to_s]
91
+ end.to_h
92
+
91
93
  {
92
94
  "Content-Type" => "application/json, charset=UTF-8",
95
+ "Content-Encoding" => "gzip",
93
96
  "X-PlainApm-Key" => app_key
94
- }
97
+ }.merge(meta_headers)
95
98
  end
96
99
 
97
100
  def http_response
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlainApm
4
- VERSION = "0.9.0"
4
+ VERSION = "0.9.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plain_apm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - PlainAPM Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-07 00:00:00.000000000 Z
11
+ date: 2024-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest