Skip to content

Commit

Permalink
wip - add Profiler::Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Aug 23, 2024
1 parent ebc43f8 commit 871e90a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 75 deletions.
40 changes: 3 additions & 37 deletions sentry-ruby/lib/sentry/profiler.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

require "securerandom"
require_relative "profiler/helpers"

module Sentry
class Profiler
include Profiler::Helpers

VERSION = "1"
PLATFORM = "ruby"
# 101 Hz in microseconds
Expand Down Expand Up @@ -189,43 +192,6 @@ def log(message)
Sentry.logger.debug(LOGGER_PROGNAME) { "[Profiler] #{message}" }
end

def in_app?(abs_path)
abs_path.match?(@in_app_pattern)
end

# copied from stacktrace.rb since I don't want to touch existing code
# TODO-neel-profiler try to fetch this from stackprof once we patch
# the native extension
def compute_filename(abs_path, in_app)
return nil if abs_path.nil?

under_project_root = @project_root && abs_path.start_with?(@project_root)

prefix =
if under_project_root && in_app
@project_root
else
longest_load_path = $LOAD_PATH.select { |path| abs_path.start_with?(path.to_s) }.max_by(&:size)

if under_project_root
longest_load_path || @project_root
else
longest_load_path
end
end

prefix ? abs_path[prefix.to_s.chomp(File::SEPARATOR).length + 1..-1] : abs_path
end

def split_module(name)
# last module plus class/instance method
i = name.rindex("::")
function = i ? name[(i + 2)..-1] : name
mod = i ? name[0...i] : nil

[function, mod]
end

def record_lost_event(reason)
Sentry.get_current_client&.transport&.record_lost_event(reason, "profile")
end
Expand Down
46 changes: 46 additions & 0 deletions sentry-ruby/lib/sentry/profiler/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "securerandom"

module Sentry
class Profiler
module Helpers
def in_app?(abs_path)
abs_path.match?(@in_app_pattern)
end

# copied from stacktrace.rb since I don't want to touch existing code
# TODO-neel-profiler try to fetch this from stackprof once we patch
# the native extension
def compute_filename(abs_path, in_app)
return nil if abs_path.nil?

under_project_root = @project_root && abs_path.start_with?(@project_root)

prefix =
if under_project_root && in_app
@project_root
else
longest_load_path = $LOAD_PATH.select { |path| abs_path.start_with?(path.to_s) }.max_by(&:size)

if under_project_root
longest_load_path || @project_root

Check warning on line 27 in sentry-ruby/lib/sentry/profiler/helpers.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/profiler/helpers.rb#L27

Added line #L27 was not covered by tests
else
longest_load_path
end
end

prefix ? abs_path[prefix.to_s.chomp(File::SEPARATOR).length + 1..-1] : abs_path
end

def split_module(name)
# last module plus class/instance method
i = name.rindex("::")
function = i ? name[(i + 2)..-1] : name
mod = i ? name[0...i] : nil

[function, mod]
end
end
end
end
44 changes: 7 additions & 37 deletions sentry-ruby/lib/sentry/vernier/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
module Sentry
module Vernier
class Output
attr_reader :profile, :opts
include Profiler::Helpers

def initialize(profile, **opts)
attr_reader :profile

def initialize(profile, project_root:, in_app_pattern:, app_dirs_pattern:)
@profile = profile
@opts = opts
@project_root = project_root
@in_app_pattern = in_app_pattern
@app_dirs_pattern = app_dirs_pattern
end

def to_h
Expand Down Expand Up @@ -88,40 +92,6 @@ def stacks
def stack_table_hash
@stack_table_hash ||= profile._stack_table.to_h
end

def in_app?(abs_path)
abs_path.match?(opts[:in_app_pattern])
end

def compute_filename(abs_path, in_app)
return nil if abs_path.nil?

under_project_root = opts[:project_root] && abs_path.start_with?(opts[:project_root])

prefix =
if under_project_root && in_app
opts[:project_root]
else
longest_load_path = $LOAD_PATH.select { |path| abs_path.start_with?(path.to_s) }.max_by(&:size)

if under_project_root
longest_load_path || opts[:project_root]
else
longest_load_path
end
end

prefix ? abs_path[prefix.to_s.chomp(File::SEPARATOR).length + 1..-1] : abs_path
end

def split_module(name)
# last module plus class/instance method
i = name.rindex("::")
function = i ? name[(i + 2)..-1] : name
mod = i ? name[0...i] : nil

[function, mod]
end
end
end
end
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/vernier/profiler.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "securerandom"
require_relative "../profiler/helpers"
require_relative "output"

begin
Expand Down Expand Up @@ -110,7 +111,6 @@ def profile_meta
def output
@output ||= Output.new(
result,
event_id: @event_id,
project_root: @project_root,
app_dirs_pattern: @app_dirs_pattern,
in_app_pattern: @in_app_pattern
Expand Down

0 comments on commit 871e90a

Please sign in to comment.