plain_apm 0.2.6 → 0.2.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef383306da20d00a0bc5c8b2072b1dfbb3d49eb7c5d1f0710cee6d6749b726d2
4
- data.tar.gz: b5c174eb5869f5909695af612182f2e9e6fe960230ce4ecd2d94b4701c5856fc
3
+ metadata.gz: fae5b33c61e3205e05e0321b6e2189f458095998d466c5f8f3494902eb279f26
4
+ data.tar.gz: 0e46fdbd9c5a3e7c6a41c4e75767046a03811c7d6c0af8688f72e14b5dce1a4e
5
5
  SHA512:
6
- metadata.gz: 86dc64bd65c57fc77f4713f10bf08beeff9f32c7f9e80e3fc454b4462ff14a4cfc0613bd24879d2ec08e2692e9e35eb3ccad49b015cbfc3e331e9bcbdf4e45b9
7
- data.tar.gz: 6f79c406f73217c44c06a46359722fb1ecb8d8c72e1c6de8ed5846992cab12ba56dc4c36fac18919896ab1f002a98129301d28cefde8502ccd32d7f6319f82c6
6
+ metadata.gz: 10f006bfd8ef860ca46a4af5656f99665c0de125d41d611a07678364650e50de6ca0b45cb34f2962a6344f6425ec8cf47af93c512bad50c2f712eca0bd3a9f65
7
+ data.tar.gz: 8f6d46f2e471fe8f5a917ececcee37db2f0c6e056e62f0320eb17797424ec86a73a43b942b54fd2e619becfdd0725705f8af3d3a299e8a5c6aab4ea1499de3b7
@@ -18,11 +18,23 @@ module PlainApm
18
18
  def collect(event)
19
19
  return unless @config.enabled
20
20
 
21
- @events << event.merge(
21
+ ##
22
+ # Context contains the trace ID (which comes from either
23
+ # HTTP_X_REQUEST_ID header, the deserialized job,
24
+ # or is generated by the trace_id middleware).
25
+ # It can also carry user inserted app data.
26
+ if defined?(PlainApm::Extensions::Context)
27
+ event.merge!(PlainApm::Extensions::Context.current)
28
+ end
29
+
30
+ event.merge!(
31
+ "version" => PlainApm::VERSION,
22
32
  "collected_at" => Time.now.utc.to_f,
23
33
  "pid" => Process.pid,
24
- "thread_id" => Thread.current.object_id.to_s
34
+ "thread_id" => Thread.current.object_id.to_s,
25
35
  )
36
+
37
+ @events << event
26
38
  end
27
39
 
28
40
  def start
@@ -2,15 +2,17 @@
2
2
 
3
3
  module PlainApm
4
4
  module Extensions
5
- module TraceId
5
+ module Context
6
6
  module ActiveJob
7
+ # TODO: would it be useful to de-serialize the whole context (e.g. user
8
+ # IDs, subscriptions, etc?)
7
9
  def serialize
8
- trace_id = PlainApm::Extensions::TraceId.current || SecureRandom.uuid
10
+ trace_id = PlainApm::Extensions::Context.trace_id || SecureRandom.uuid
9
11
  super.update("trace_id" => trace_id)
10
12
  end
11
13
 
12
14
  def deserialize(job)
13
- PlainApm::Extensions::TraceId.current = job["trace_id"]
15
+ PlainApm::Extensions::Context.trace_id = job["trace_id"]
14
16
 
15
17
  super(job)
16
18
  end
@@ -0,0 +1,11 @@
1
+ module PlainApm
2
+ module Extensions
3
+ module Context
4
+ module Helpers
5
+ def plain_apm_context(context = {})
6
+ PlainApm::Extensions::Context.context.merge!(context)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -10,26 +10,26 @@ require "securerandom"
10
10
  # See LICENSE.txt in the current directory for the license.
11
11
  module PlainApm
12
12
  module Extensions
13
- module TraceId
13
+ module Context
14
14
  class Middleware
15
15
  def initialize(app)
16
16
  @app = app
17
17
  end
18
18
 
19
19
  def call(env)
20
- TraceId.current = trace_id(env)
20
+ Context.trace_id = trace_id(env)
21
21
 
22
22
  status, headers, body = @app.call(env)
23
23
 
24
24
  body = Rack::BodyProxy.new(body) do
25
- TraceId.current = nil
25
+ Context.clear!
26
26
  end
27
27
 
28
28
  processed = true
29
29
 
30
30
  [status, headers, body]
31
31
  ensure
32
- TraceId.current = nil if !processed
32
+ Context.clear! if !processed
33
33
  end
34
34
 
35
35
  private
@@ -8,13 +8,13 @@
8
8
  # See LICENSE.txt in the current directory for the license.
9
9
  module PlainApm
10
10
  module Extensions
11
- module TraceId
11
+ module Context
12
12
  class Railtie < Rails::Railtie
13
- initializer "plain_apm.insert_trace_id_middleware" do |app|
13
+ initializer "plain_apm.insert_context_middleware" do |app|
14
14
  if defined?(ActionDispatch::RequestId)
15
- app.config.middleware.insert_after ActionDispatch::RequestId, PlainApm::Extensions::TraceId::Middleware
15
+ app.config.middleware.insert_after ActionDispatch::RequestId, PlainApm::Extensions::Context::Middleware
16
16
  else
17
- app.config.middleware.insert_after Rack::MethodOverride, PlainApm::Extensions::TraceId::Middleware
17
+ app.config.middleware.insert_after Rack::MethodOverride, PlainApm::Extensions::Context::Middleware
18
18
  end
19
19
  end
20
20
  end
@@ -0,0 +1,41 @@
1
+ module PlainApm
2
+ module Extensions
3
+ module Context
4
+ STORE_KEY = :plain_apm_extensions_store
5
+ TRACE_ID_KEY = :trace_id
6
+ CONTEXT_KEY = :context
7
+
8
+ def self.current
9
+ Thread.current[STORE_KEY] ||= {}
10
+ end
11
+
12
+ def self.clear!
13
+ Thread.current[STORE_KEY] = {}
14
+ end
15
+
16
+ def self.[](key)
17
+ current[key]
18
+ end
19
+
20
+ def self.[]=(key, value)
21
+ current[key] = value
22
+ end
23
+
24
+ def self.context
25
+ current[CONTEXT_KEY] ||= {}
26
+ end
27
+
28
+ def self.context=(hash)
29
+ current[CONTEXT_KEY] = hash
30
+ end
31
+
32
+ def self.trace_id
33
+ current[TRACE_ID_KEY]
34
+ end
35
+
36
+ def self.trace_id=(id)
37
+ current[TRACE_ID_KEY] = id
38
+ end
39
+ end
40
+ end
41
+ end
@@ -29,8 +29,7 @@ module PlainApm
29
29
  "class" => e.class.name,
30
30
  "message" => e.message,
31
31
  "backtrace" => e.backtrace,
32
- "params" => env["action_dispatch.request.parameters"],
33
- "trace_id" => PlainApm::Extensions::TraceId.current
32
+ "params" => env["action_dispatch.request.parameters"]
34
33
  }
35
34
 
36
35
  PlainApm::Agent.collect(event)
@@ -21,8 +21,7 @@ module PlainApm
21
21
  "backtrace" => filtered_backtrace,
22
22
  "allocations" => event.allocations,
23
23
  "started_at" => event.time,
24
- "finished_at" => event.end,
25
- "trace_id" => trace_id
24
+ "finished_at" => event.end
26
25
  }
27
26
 
28
27
  case name
@@ -21,8 +21,7 @@ module PlainApm
21
21
  "backtrace" => filtered_backtrace,
22
22
  "allocations" => event.allocations,
23
23
  "started_at" => event.time,
24
- "finished_at" => event.end,
25
- "trace_id" => trace_id
24
+ "finished_at" => event.end
26
25
  }
27
26
 
28
27
  case name
@@ -21,8 +21,7 @@ module PlainApm
21
21
  "backtrace" => filtered_backtrace,
22
22
  "started_at" => event.time,
23
23
  "finished_at" => event.end,
24
- "allocations" => event.allocations,
25
- "trace_id" => trace_id
24
+ "allocations" => event.allocations
26
25
  }
27
26
 
28
27
  case name
@@ -22,7 +22,6 @@ module PlainApm
22
22
  "backtrace" => filtered_backtrace,
23
23
  "started_at" => event.time,
24
24
  "finished_at" => event.end,
25
- "trace_id" => trace_id,
26
25
  "allocations" => event.allocations,
27
26
  "queue_name" => job.queue_name,
28
27
  "job_id" => job.job_id,
@@ -23,8 +23,7 @@ module PlainApm
23
23
  "backtrace" => filtered_backtrace,
24
24
  "started_at" => event.time,
25
25
  "finished_at" => event.end,
26
- "allocations" => event.allocations,
27
- "trace_id" => trace_id
26
+ "allocations" => event.allocations
28
27
  }
29
28
 
30
29
  case name
@@ -45,13 +45,6 @@ module PlainApm
45
45
  @cleaner.clean(caller)
46
46
  end
47
47
  end
48
-
49
- ##
50
- # The trace ID (comes from either HTTP_X_REQUEST_ID header, or is
51
- # generated by the trace_id middleware)
52
- def trace_id
53
- PlainApm::Extensions::TraceId.current
54
- end
55
48
  end
56
49
  end
57
50
  end
@@ -32,8 +32,7 @@ module PlainApm
32
32
  "backtrace" => e.backtrace,
33
33
  "handled" => handled,
34
34
  "severity" => severity,
35
- "context" => context,
36
- "trace_id" => PlainApm::Extensions::TraceId.current
35
+ "context" => context
37
36
  }
38
37
 
39
38
  if e.cause
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlainApm
4
- VERSION = "0.2.6"
4
+ VERSION = "0.2.8"
5
5
  end
data/lib/plain_apm.rb CHANGED
@@ -11,10 +11,11 @@ require_relative "plain_apm/hooks/deploy"
11
11
  begin
12
12
  require "rack/body_proxy"
13
13
 
14
- require_relative "plain_apm/extensions/trace_id"
15
- require_relative "plain_apm/extensions/trace_id/middleware"
16
- require_relative "plain_apm/extensions/trace_id/active_job" if defined?(ActiveSupport)
17
- require_relative "plain_apm/extensions/trace_id/railtie" if defined?(Rails::Railtie)
14
+ require_relative "plain_apm/extensions/context"
15
+ require_relative "plain_apm/extensions/context/helpers"
16
+ require_relative "plain_apm/extensions/context/middleware"
17
+ require_relative "plain_apm/extensions/context/active_job" if defined?(ActiveSupport)
18
+ require_relative "plain_apm/extensions/context/railtie" if defined?(Rails::Railtie)
18
19
  rescue LoadError
19
20
  nil
20
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plain_apm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - PlainAPM Team
@@ -82,14 +82,15 @@ files:
82
82
  - lib/plain_apm/agent.rb
83
83
  - lib/plain_apm/backoff.rb
84
84
  - lib/plain_apm/config.rb
85
+ - lib/plain_apm/extensions/context.rb
86
+ - lib/plain_apm/extensions/context/LICENSE.txt
87
+ - lib/plain_apm/extensions/context/active_job.rb
88
+ - lib/plain_apm/extensions/context/helpers.rb
89
+ - lib/plain_apm/extensions/context/middleware.rb
90
+ - lib/plain_apm/extensions/context/railtie.rb
85
91
  - lib/plain_apm/extensions/exceptions/active_job.rb
86
92
  - lib/plain_apm/extensions/exceptions/rack.rb
87
93
  - lib/plain_apm/extensions/exceptions/railtie.rb
88
- - lib/plain_apm/extensions/trace_id.rb
89
- - lib/plain_apm/extensions/trace_id/LICENSE.txt
90
- - lib/plain_apm/extensions/trace_id/active_job.rb
91
- - lib/plain_apm/extensions/trace_id/middleware.rb
92
- - lib/plain_apm/extensions/trace_id/railtie.rb
93
94
  - lib/plain_apm/hooks/action_mailer.rb
94
95
  - lib/plain_apm/hooks/action_pack.rb
95
96
  - lib/plain_apm/hooks/action_view.rb
@@ -1,15 +0,0 @@
1
- module PlainApm
2
- module Extensions
3
- module TraceId
4
- TRACE_KEY = :plain_apm_extensions_trace_id
5
-
6
- def self.current
7
- Thread.current[TRACE_KEY]
8
- end
9
-
10
- def self.current=(id)
11
- Thread.current[TRACE_KEY] = id
12
- end
13
- end
14
- end
15
- end