Skip to content

Commit adb8d5a

Browse files
committed
Thread safety test
1 parent b5f0773 commit adb8d5a

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

example-project/main.rb

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This file showcases usage of logtail on Ruby projects
1+
# This file showcases thread safety testing of logtail on Ruby projects
22
# For more information visit https://github.com/logtail/logtail-ruby
33

44
# SETUP
@@ -11,38 +11,58 @@
1111
puts "Program needs source token and ingesting host to run. Run the program as followed\nbundle exec ruby main.rb <source_token> <ingesting_host>"
1212
exit
1313
end
14-
# Create logger
15-
http_device = Logtail::LogDevices::HTTP.new(ARGV[0], ingesting_host: ARGV[1])
16-
logger = Logtail::Logger.new(http_device)
1714

18-
# Filter logs that shouldn't be sent to Better Stack, see {Logtail::LogEntry} for available attributes
19-
Logtail.config.filter_sent_to_better_stack { |log_entry| log_entry.message.include?("DO_NOT_SEND") }
15+
# Configuration
16+
source_token = ARGV[0]
17+
ingesting_host = ARGV[1]
18+
thread_count = 100
19+
iterations_per_thread = 100
2020

21-
# LOGGING
21+
# Thread safety test
22+
puts "Starting thread safety test with #{thread_count} threads..."
23+
puts "Initial thread count: #{Thread.list.size}"
2224

23-
# Send debug logs messages using the debug() method
24-
logger.debug("Better Stack is ready!")
25+
threads = []
2526

26-
# Send informative messages about interesting events using the info() method
27-
logger.info("I am using Better Stack!")
28-
29-
# Send messages about worrying events using the warn() method
30-
# You can also log additional structured data
31-
logger.warn(
32-
"log structured data",
33-
item: {
34-
url: "https://fictional-store.com/item-123",
35-
price: 100.00
36-
}
37-
)
38-
39-
# Some messages can be filtered, see {Logtail::Config#filter_sent_to_better_stack} call above
40-
logger.info("This message will not be sent to Better Stack because it contains 'DO_NOT_SEND'")
27+
thread_count.times do |thread_index|
28+
threads << Thread.new(thread_index) do |index|
29+
iterations_per_thread.times do |iteration|
30+
begin
31+
# Create a new logger instance for each iteration
32+
http_device = Logtail::LogDevices::HTTP.new(source_token, ingesting_host: ingesting_host)
33+
logger = Logtail::Logger.new(http_device)
34+
35+
# Log messages
36+
logger.info("Log message with structured logging.", {
37+
thread_count:,
38+
thread_index:,
39+
iterations_per_thread:,
40+
iteration:,
41+
})
42+
43+
# Close logger to ensure that all logs are sent to Better Stack
44+
logger.close
45+
46+
puts "Thread #{index}: Completed iteration #{iteration + 1}"
47+
rescue => e
48+
puts "Thread #{index}: Error in iteration #{iteration + 1}: #{e.message}"
49+
end
50+
end
51+
end
52+
end
4153

42-
# Send error messages using the error() method
43-
logger.error("Oops! An error occurred!")
54+
# Monitor thread count while threads are running
55+
monitoring_thread = Thread.new do
56+
while threads.any?(&:alive?)
57+
puts "Current thread count: #{Thread.list.size}"
58+
sleep 0.5
59+
end
60+
end
4461

45-
# Send messages about fatal events that caused the app to crash using the fatal() method
46-
logger.fatal("Application crash! Needs to be fixed ASAP!")
62+
# Wait for all threads to complete
63+
threads.each(&:join)
64+
monitoring_thread.join
4765

66+
puts "\nThread safety test completed!"
67+
puts "Final thread count: #{Thread.list.size}"
4868
puts "All done! You can check your logs now."

0 commit comments

Comments
 (0)