Skip to content

Commit

Permalink
Conccurent tracing tracking enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
aantix committed May 15, 2023
1 parent b363d8a commit fe57cf7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
18 changes: 14 additions & 4 deletions lib/callstacking/rails/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ module Rails
class Trace
include Callstacking::Rails::Helpers::HeadsUpDisplayHelper

attr_accessor :spans, :client, :lock
attr_accessor :spans, :client, :lock, :traces
attr_reader :settings
cattr_accessor :current_trace_id
cattr_accessor :current_tuid
cattr_accessor :trace_log

ICON = '💥'
MAX_TRACE_ENTRIES = 3000

@@trace_log||={}

def initialize(spans)
@traces = []
@spans = spans
@settings = Callstacking::Rails::Settings.new

@traces = []
@spans = spans
@settings = Callstacking::Rails::Settings.new

@lock = Mutex.new
@client = Callstacking::Rails::Client::Trace.new(settings.url, settings.auth_token)
Expand All @@ -29,6 +33,8 @@ def initialize(spans)

def begin_trace(controller)
@trace_id, @tuid = init_uuids(controller.request&.request_id || SecureRandom.uuid, TimeBasedUUID.generate)
trace_log[@trace_id] = controller.request&.original_url

init_callbacks(@tuid)

start_request(@trace_id, @tuid,
Expand All @@ -50,6 +56,10 @@ def end_trace(controller)
inject_hud(@settings, controller.request, controller.response)
end

def self.trace_log_clear
trace_log.clear
end

private

def init_callbacks(tuid)
Expand Down
18 changes: 10 additions & 8 deletions test/integration/thread_safety_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
class ThreadSafetyTest < ActionDispatch::IntegrationTest
TEST_URL = "http://www.example.com"

# Test initiates multiple http requests and makes multiple method calls in parallel for each of the requests.
# The results are validated against the Call Stacking server, ensuring that none of
# the trace values are intermixed.
test "concurrent tracing" do
::Callstacking::Rails::Trace.trace_log_clear

urls = {'/hello?debug=1' => 'English',
'/bounjor?debug=1' => 'French',
'/hallo?debug=1' => 'German'}
Expand All @@ -21,16 +26,13 @@ class ThreadSafetyTest < ActionDispatch::IntegrationTest
end
threads.each(&:join)

urls.each do |url, klass|
response = client.show('xxxx', url: "#{TEST_URL}#{url}")
::Callstacking::Rails::Trace.trace_log.each do |trace_id, url|
params = url.gsub(TEST_URL, '')
response = client.show(trace_id)
json = response.body

sleep 20

::Callstacking::Rails::Logger.log("url: #{url} -- json: #{json.inspect}")


json['trace_entries'][1..10].each do |trace_entry|
assert_equal klass, trace_entry['klass']
assert_equal urls[params], trace_entry['klass']
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
ENV["RAILS_ENV"] = "test"

# https://github.com/Shopify/minitest-silence
# ENV["CI"] = "true"
#
ENV["CI"] = "true"

Callstacking::Rails::Settings.new.save('test@test.com',
'testing123',
Callstacking::Rails::Settings::PRODUCTION_URL)
Expand Down

0 comments on commit fe57cf7

Please sign in to comment.