Skip to content

Commit

Permalink
Add support for Vernier profiler
Browse files Browse the repository at this point in the history
- Introduce `Sentry::Vernier::Profiler`
- Introduce `Sentry.config.profiler` that can be set to a class that
  should be used for profiling. By default it's set to `Sentry::Profiler`
  • Loading branch information
solnic committed Aug 26, 2024
1 parent 5b7394f commit 5220e97
Show file tree
Hide file tree
Showing 12 changed files with 699 additions and 97 deletions.
4 changes: 3 additions & 1 deletion sentry-ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ gem "sentry-ruby", path: "./"

rack_version = ENV["RACK_VERSION"]
rack_version = "3.0.0" if rack_version.nil?
gem "rack", "~> #{Gem::Version.new(rack_version)}" unless rack_version == "0"
# gem "rack", "~> #{Gem::Version.new(rack_version)}" unless rack_version == "0"
gem "rack", "~> 3.1"

redis_rb_version = ENV.fetch("REDIS_RB_VERSION", "5.0")
gem "redis", "~> #{redis_rb_version}"
Expand All @@ -14,6 +15,7 @@ gem "puma"

gem "timecop"
gem "stackprof" unless RUBY_PLATFORM == "java"
gem "vernier", platforms: :ruby if RUBY_VERSION >= "3.2.1"

gem "graphql", ">= 2.2.6" if RUBY_VERSION.to_f >= 2.7

Expand Down
6 changes: 6 additions & 0 deletions sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ def capture_exception_frame_locals=(value)
# @return [Symbol]
attr_reader :instrumenter

# The profiler class
# @return [Class]
attr_accessor :profiler

# Take a float between 0.0 and 1.0 as the sample rate for capturing profiles.
# Note that this rate is relative to traces_sample_rate / traces_sampler,
# i.e. the profile is sampled by this rate after the transaction is sampled.
Expand Down Expand Up @@ -393,6 +397,8 @@ def initialize
self.traces_sampler = nil
self.enable_tracing = nil

self.profiler = Sentry::Profiler

@transport = Transport::Configuration.new
@cron = Cron::Configuration.new
@metrics = Metrics::Configuration.new
Expand Down
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
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def initialize(
@effective_sample_rate = nil
@contexts = {}
@measurements = {}
@profiler = Profiler.new(@configuration)
@profiler = @configuration.profiler.new(@configuration)
init_span_recorder
end

Expand Down
3 changes: 1 addition & 2 deletions sentry-ruby/lib/sentry/transaction_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def populate_profile(transaction)
id: event_id,
name: transaction.name,
trace_id: transaction.trace_id,
# TODO-neel-profiler stubbed for now, see thread_id note in profiler.rb
active_thead_id: "0"
active_thread_id: Thread.current.object_id.to_s
}
)

Expand Down
97 changes: 97 additions & 0 deletions sentry-ruby/lib/sentry/vernier/output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

require "json"
require "rbconfig"

module Sentry
module Vernier
class Output
include Profiler::Helpers

attr_reader :profile

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

def to_h
@to_h ||= {
frames: frames,
stacks: stacks,
samples: samples,
thread_metadata: thread_metadata
}
end

private

def thread_metadata
profile.threads.map { |thread_id, thread_info|
[thread_id, { name: thread_info[:name] }]
}.to_h
end

def samples
profile.threads.flat_map { |thread_id, thread_info|
started_at = thread_info[:started_at]
samples, timestamps = thread_info.values_at(:samples, :timestamps)

samples.zip(timestamps).map { |stack_id, timestamp|
elapsed_since_start_ns = timestamp - started_at

next if elapsed_since_start_ns < 0

{
thread_id: thread_id.to_s,
stack_id: stack_id,
elapsed_since_start_ns: elapsed_since_start_ns.to_s
}
}.compact
}
end

def frames
funcs = stack_table_hash[:frame_table].fetch(:func)
lines = stack_table_hash[:func_table].fetch(:first_line)

funcs.map { |idx|
function, mod = split_module(stack_table_hash[:func_table][:name][idx])

abs_path = stack_table_hash[:func_table][:filename][idx]
in_app = in_app?(abs_path)
filename = compute_filename(abs_path, in_app)

{
function: function,
module: mod,
filename: filename,
abs_path: abs_path,
lineno: (lineno = lines[idx]) > 0 ? lineno : nil,
in_app: in_app
}.compact
}
end

def stacks
sample_stacks = profile.threads.flat_map { |_, thread_info|
samples = thread_info[:samples].map { |stack_id| profile.stack(stack_id) }

samples.map { |stack| [stack.idx, stack.frames.map(&:idx)] }
}.to_h

stacks = profile._stack_table.stack_count.times.map do |stack_id|
(sample_stacks[stack_id] || profile.stack(stack_id).frames.map(&:idx))
end

stacks
end

def stack_table_hash
@stack_table_hash ||= profile._stack_table.to_h
end
end
end
end
126 changes: 126 additions & 0 deletions sentry-ruby/lib/sentry/vernier/profiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

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

begin
require "vernier"
rescue LoadError
end

module Sentry
module Vernier
class Profiler
EMPTY_RESULT = {}.freeze

attr_reader :started, :event_id, :result

def initialize(configuration)
@event_id = SecureRandom.uuid.delete("-")

@started = false
@sampled = nil

@profiling_enabled = defined?(Vernier) && configuration.profiling_enabled?
@profiles_sample_rate = configuration.profiles_sample_rate
@project_root = configuration.project_root
@app_dirs_pattern = configuration.app_dirs_pattern || Backtrace::APP_DIRS_PATTERN
@in_app_pattern = Regexp.new("^(#{@project_root}/)?#{@app_dirs_pattern}")
end

def set_initial_sample_decision(transaction_sampled)
unless @profiling_enabled
@sampled = false
return
end

unless transaction_sampled
@sampled = false
log("Discarding profile because transaction not sampled")
return
end

case @profiles_sample_rate
when 0.0
@sampled = false
log("Discarding profile because sample_rate is 0")
return
when 1.0
@sampled = true
return
else
@sampled = Random.rand < @profiles_sample_rate
end

log("Discarding profile due to sampling decision") unless @sampled
end

def start
return unless @sampled && !@started

::Vernier.start_profile
@started = true

log("Started")

@started
rescue RuntimeError => e
# TODO: once Vernier raises something more dedicated, we should catch that instead
if e.message.include?("Profile already started")
log("Not started since running elsewhere")
else
log("Failed to start: #{e.message}")
raise e
end
end

def stop
return unless @sampled
return unless @started

@result = ::Vernier.stop_profile

log("Stopped")
end

def to_hash
return EMPTY_RESULT unless @started

unless @sampled
record_lost_event(:sample_rate)
return EMPTY_RESULT
end

{ **profile_meta, profile: output.to_h }
end

private

def log(message)
Sentry.logger.debug(LOGGER_PROGNAME) { "[Profiler::Vernier] #{message}" }
end

def record_lost_event(reason)
Sentry.get_current_client&.transport&.record_lost_event(reason, "profile")
end

def profile_meta
{
event_id: @event_id,
version: "1",
platform: "ruby"
}
end

def output
@output ||= Output.new(
result,
project_root: @project_root,
app_dirs_pattern: @app_dirs_pattern,
in_app_pattern: @in_app_pattern
)
end
end
end
end
Loading

0 comments on commit 5220e97

Please sign in to comment.