|
1 | | -# This file showcases usage of logtail on Ruby projects |
| 1 | +# This file showcases thread safety testing of logtail on Ruby projects |
2 | 2 | # For more information visit https://github.com/logtail/logtail-ruby |
3 | 3 |
|
4 | 4 | # SETUP |
|
11 | 11 | puts "Program needs source token and ingesting host to run. Run the program as followed\nbundle exec ruby main.rb <source_token> <ingesting_host>" |
12 | 12 | exit |
13 | 13 | end |
14 | | -# Create logger |
15 | | -http_device = Logtail::LogDevices::HTTP.new(ARGV[0], ingesting_host: ARGV[1]) |
16 | | -logger = Logtail::Logger.new(http_device) |
17 | 14 |
|
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 |
20 | 20 |
|
21 | | -# LOGGING |
| 21 | +# Thread safety test |
| 22 | +puts "Starting thread safety test with #{thread_count} threads..." |
| 23 | +puts "Initial thread count: #{Thread.list.size}" |
22 | 24 |
|
23 | | -# Send debug logs messages using the debug() method |
24 | | -logger.debug("Better Stack is ready!") |
| 25 | +threads = [] |
25 | 26 |
|
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 |
41 | 53 |
|
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 |
44 | 61 |
|
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 |
47 | 65 |
|
| 66 | +puts "\nThread safety test completed!" |
| 67 | +puts "Final thread count: #{Thread.list.size}" |
48 | 68 | puts "All done! You can check your logs now." |
0 commit comments