From f12c439983fe2c4d4cc8ea266f38b4bedfeafc78 Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Thu, 17 Aug 2023 14:10:45 +0200 Subject: [PATCH] Add ability to filter logs send to Better Stack (#23) --- .github/workflows/main.yml | 1 - example-project/Gemfile | 2 +- example-project/Gemfile.lock | 4 ++-- example-project/main.rb | 6 ++++++ lib/logtail/config.rb | 22 ++++++++++++++++++++++ lib/logtail/log_devices/http.rb | 2 ++ lib/logtail/version.rb | 2 +- logtail.gemspec | 2 +- 8 files changed, 35 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fd4178c..b8d03d3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/example-project/Gemfile b/example-project/Gemfile index a31326a..7ea987c 100644 --- a/example-project/Gemfile +++ b/example-project/Gemfile @@ -1,2 +1,2 @@ source 'https://rubygems.org' -gem "logtail" +gem "logtail", "~> 0.1.12" diff --git a/example-project/Gemfile.lock b/example-project/Gemfile.lock index 66ce8bd..09679ba 100644 --- a/example-project/Gemfile.lock +++ b/example-project/Gemfile.lock @@ -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 diff --git a/example-project/main.rb b/example-project/main.rb index 6af69ec..b062ddd 100644 --- a/example-project/main.rb +++ b/example-project/main.rb @@ -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 @@ -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!") diff --git a/lib/logtail/config.rb b/lib/logtail/config.rb index 662162b..6ea6729 100755 --- a/lib/logtail/config.rb +++ b/lib/logtail/config.rb @@ -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 diff --git a/lib/logtail/log_devices/http.rb b/lib/logtail/log_devices/http.rb index 863102e..c3243d4 100755 --- a/lib/logtail/log_devices/http.rb +++ b/lib/logtail/log_devices/http.rb @@ -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. diff --git a/lib/logtail/version.rb b/lib/logtail/version.rb index b320866..4197c1c 100755 --- a/lib/logtail/version.rb +++ b/lib/logtail/version.rb @@ -1,3 +1,3 @@ module Logtail - VERSION = "0.1.11" + VERSION = "0.1.12" end diff --git a/logtail.gemspec b/logtail.gemspec index a921e4c..baa25c2 100644 --- a/logtail.gemspec +++ b/logtail.gemspec @@ -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")