Skip to content

Commit

Permalink
Add ability to filter logs send to Better Stack (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Aug 17, 2023
1 parent da2925f commit f12c439
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 6 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
- 2.5
- 2.4
- 2.3
- 2.2
- jruby-9.4.3.0
- jruby-9.2.14.0
- truffleruby-23.0.0
Expand Down
2 changes: 1 addition & 1 deletion example-project/Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
source 'https://rubygems.org'
gem "logtail"
gem "logtail", "~> 0.1.12"
4 changes: 2 additions & 2 deletions example-project/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
GEM
remote: https://rubygems.org/
specs:
logtail (0.1.9)
logtail (0.1.12)
msgpack (~> 1.0)
msgpack (1.7.1)
msgpack (1.7.2)

PLATFORMS
ruby
Expand Down
6 changes: 6 additions & 0 deletions example-project/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
http_device = Logtail::LogDevices::HTTP.new(ARGV[0])
logger = Logtail::Logger.new(http_device)

# Filter logs that shouldn't be sent to Better Stack, see {Logtail::LogEntry} for available attributes
Logtail.config.filter_sent_to_better_stack { |log_entry| log_entry.message.include?("DO_NOT_SEND") }

# LOGGING

# Send debug logs messages using the debug() method
Expand All @@ -33,6 +36,9 @@
}
)

# Some messages can be filtered, see {Logtail::Config#filter_sent_to_better_stack} call above
logger.info("This message will not be sent to Better Stack because it contains 'DO_NOT_SEND'")

# Send error messages using the error() method
logger.error("Oops! An error occurred!")

Expand Down
22 changes: 22 additions & 0 deletions lib/logtail/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ def call(severity, timestamp, progname, msg)

attr_writer :http_body_limit

# Whether a particular {Logtail::LogEntry} should be sent to Better Stack
def send_to_better_stack?(log_entry)
!@better_stack_filters&.any? { |blocker| blocker.call(log_entry) }
rescue => e
debug { "Could not determine whether to send LogEntry to Better Stack (assumed yes): #{e}" }
true
end

# This allows filtering logs that are sent to Better Stack. Can be called multiple times, all filters will
# be applied. If the passed block RETURNS TRUE for a particular LogEntry, it WILL NOT BE SENT to Better Stack.
#
# See {Logtail::LogEntry} for available attributes of the block parameter.
#
# @example Rails
# config.logtail.filter_sent_to_better_stack { |log_entry| log_entry.context_snapshot[:http][:path].start_with?('_') }
# @example Everything else
# Logtail.config.filter_sent_to_better_stack { |log_entry| log_entry.message.include?('IGNORE') }
def filter_sent_to_better_stack(&block)
@better_stack_filters ||= []
@better_stack_filters << -> (log_entry) { yield(log_entry) }
end

# Convenience method for logging debug statements to the debug logger
# set in this class.
# @private
Expand Down
2 changes: 2 additions & 0 deletions lib/logtail/log_devices/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def initialize(source_token, options = {})
# size is constricted by the Logtail API. The actual application limit is a multiple
# of this. Hence the `@request_queue`.
def write(msg)
return unless Logtail.config.send_to_better_stack?(msg)

@msg_queue.enq(msg)

# Lazily start flush threads to ensure threads are alive after forking processes.
Expand Down
2 changes: 1 addition & 1 deletion lib/logtail/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Logtail
VERSION = "0.1.11"
VERSION = "0.1.12"
end
2 changes: 1 addition & 1 deletion logtail.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage

spec.required_ruby_version = Gem::Requirement.new(">= 2.2.0")
spec.required_ruby_version = Gem::Requirement.new(">= 2.3")

spec.files = `git ls-files`.split("\n")
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down

0 comments on commit f12c439

Please sign in to comment.