perfetto 0.1.6 → 0.1.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: 336d2d67b2921461a66687b0a06fca3ef0e925cbdc83e028bdbf2863ef3012d4
4
- data.tar.gz: 3dc3d8b63d9029dcc10d5250f156663d5248da727652aa0b3cce42a55ac856af
3
+ metadata.gz: fd838b9d196922765c51a65336d1304c6d9ac9aca456fce60d42b8c72b8b62d4
4
+ data.tar.gz: 9c1972ee1b588bd0dacaaca9cd81c39d90fd892ff0c57b0286157cf8944c6ae8
5
5
  SHA512:
6
- metadata.gz: c2771fce6b5aa3b804f6c79d7e64fde0af8238ba25d6325707634d5612a05254df7c6dbf0b34d78d8c2f9c9f502f331e0dd954805167993378f28540cc474c57
7
- data.tar.gz: 2a006c86b49bad07a0a0d7117c65a5813dd9a957cb24992aee9b419efe0ac3644a92f6d63338a994b97e9ef800b25dc2bc941d0434a42c20443eb04c5b7aad5d
6
+ metadata.gz: 92e2da1eceb1263f9d9590fed6130623deee27ab0caa9e4faaf701e3ad340456a6c3730abba3b84da93df94f732a9136ddc8fa9eb7558344701fc132a7f6b69e
7
+ data.tar.gz: f2f6ba2c22ca06fdaea0e09c6ad5e555d7b580a6630a77ff0132c0de1ee188fd33c6ab76b35fc5819a8ea1f5aed5863323207b232c0dfabbb633b6b524136247
data/README.md CHANGED
@@ -4,7 +4,9 @@
4
4
 
5
5
  https://ui.perfetto.dev/
6
6
 
7
- # Usage
7
+ ## Usage
8
+
9
+ ### Basic
8
10
 
9
11
  ```ruby
10
12
  require "perfetto"
@@ -83,3 +85,25 @@ Perfetto.stop_tracing "example.pftrace"
83
85
  ```
84
86
 
85
87
  ![example](./example/example.png)
88
+
89
+ ### Rack Middleware
90
+
91
+ ```ruby
92
+ # frozen_string_literal: true
93
+
94
+ require "sinatra/base"
95
+ require "perfetto"
96
+
97
+ class Server < Sinatra::Base
98
+ use Perfetto::Middleware, env_proc: ->(env) { env.to_json }
99
+
100
+ get "/" do
101
+ "Hello World"
102
+ end
103
+ end
104
+
105
+ Perfetto.setup enable_tracing: true
106
+ Perfetto.start_tracing
107
+ Server.run!
108
+ Perfetto.stop_tracing "middleware.pftrace"
109
+ ```
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # rubocop:disable Metrics/AbcSize
4
+ # rubocop:disable Metrics/MethodLength
5
+
3
6
  module Perfetto
4
7
  # To intercept method calls in other classes
5
8
  module Interceptor
@@ -39,7 +42,6 @@ module Perfetto
39
42
  def perfetto_trace_instance_method(method_name)
40
43
  return if method_name.to_s.start_with? "_pfi_"
41
44
 
42
- # warn "Perfetto[InstanceMethod]: #{name}##{method_name}"
43
45
  perfetto_traced_instance_methods << method_name
44
46
 
45
47
  original_method = instance_method(method_name)
@@ -56,17 +58,16 @@ module Perfetto
56
58
  end
57
59
 
58
60
  def perfetto_trace_class_method(method_name)
59
- return if method_name.to_s == "singleton_method_added" || method_name.to_s.start_with?("_pfc_")
61
+ return if method_name.to_s.start_with? "_pfc_"
60
62
 
61
- # warn "Perfetto[ClassMethod]: #{name}.#{method_name}"
62
63
  perfetto_traced_class_methods << method_name
63
64
 
64
65
  original_method = method(method_name)
65
66
  singleton_class.send(:alias_method, "_pfc_#{method_name}", method_name)
66
67
 
67
68
  define_singleton_method(method_name) do |*args, **kwargs, &block|
68
- category = self.name
69
- task_name = "#{self.name}.#{method_name}"
69
+ category = name
70
+ task_name = "#{name}.#{method_name}"
70
71
  Perfetto.trace_event_begin category, task_name
71
72
  original_method.call(*args, **kwargs, &block)
72
73
  ensure
@@ -106,3 +107,5 @@ module Perfetto
106
107
  end
107
108
  end
108
109
  end
110
+ # rubocop:enable Metrics/AbcSize
111
+ # rubocop:enable Metrics/MethodLength
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Perfetto
4
+ # Rack middleware
5
+ class Middleware
6
+ def initialize(app, options = {})
7
+ @app = app
8
+ @options = options
9
+ @env_proc = options[:env_proc]
10
+ end
11
+
12
+ # rubocop:disable Metrics/MethodLength
13
+ def perfetto_traced_call(env)
14
+ category = "RackMiddleware"
15
+ method = env["REQUEST_METHOD"] || "UNKNOWN"
16
+ path = env["PATH_INFO"] || "UNKNOWN PATH"
17
+ task_name = "#{method} #{path}"
18
+ env_str = @env_proc&.call(env) || { env: "unknown" }.to_json
19
+
20
+ Perfetto.trace_event_begin_with_debug_info category, task_name, "env", env_str
21
+ begin
22
+ @app.call(env)
23
+ ensure
24
+ Perfetto.trace_event_end category
25
+ end
26
+ end
27
+ # rubocop:enable Metrics/MethodLength
28
+
29
+ def call(env)
30
+ @app.call(env) unless Perfetto::Configure.enable_tracing
31
+
32
+ perfetto_traced_call(env)
33
+ end
34
+ end
35
+ end
@@ -13,7 +13,7 @@ module Perfetto
13
13
  start_tracing_native Configure.buffer_size_kb
14
14
  end
15
15
 
16
- def self.stop_tracing(trace_file_name = "#{Time.now.strftime("%Y%m%d%H%M%S")}.pftrace")
16
+ def self.stop_tracing(trace_file_name = "#{Time.now.strftime("%Y%m%d-%H-%M-%S")}.pftrace")
17
17
  stop_tracing_native trace_file_name
18
18
  end
19
19
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Perfetto
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.8"
5
5
  end
data/lib/perfetto.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
4
+
3
5
  require_relative "perfetto/version"
4
6
 
5
7
  require_relative "perfetto/core_ext/configurable"
6
8
  require_relative "perfetto/configure"
7
9
 
10
+ # Rack middleware
11
+ require_relative "perfetto/middleware"
12
+
8
13
  # To minimize the overhead of tracing at runtime
9
14
  # we determine whether to enable instrumentations
10
15
  # at the first call to 'setup' method instead of
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perfetto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kowalski Dark
@@ -35,6 +35,7 @@ files:
35
35
  - lib/perfetto/configure.rb
36
36
  - lib/perfetto/core_ext/configurable.rb
37
37
  - lib/perfetto/interceptor.rb
38
+ - lib/perfetto/middleware.rb
38
39
  - lib/perfetto/perfetto.rb
39
40
  - lib/perfetto/version.rb
40
41
  - sig/perfetto.rbs