Skip to content

Commit

Permalink
DEV: Format files with syntax_tree gem (#208)
Browse files Browse the repository at this point in the history
Why this change?

The Discourse team has adopted the `syntax_tree` gem to format Ruby files.
  • Loading branch information
tgxworld authored Oct 2, 2023
1 parent 007583c commit 9f1e648
Show file tree
Hide file tree
Showing 42 changed files with 997 additions and 935 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ jobs:
working-directory: client-app
run: yarn install

- name: Syntax Tree
if: ${{ !cancelled() }}
run: |
bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') $(git ls-files '*.thor')
- name: JS linting
if: ${{ !cancelled() }}
working-directory: client-app
Expand Down
2 changes: 2 additions & 0 deletions .streerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--print-width=100
--plugins=plugin/trailing_comma
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in rack-log-viewer.gemspec
gemspec
27 changes: 10 additions & 17 deletions client-app/preload-json-manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,30 @@
# This script takes care of updating the content of the preloaded json in tests/index.html
# All you need to do is update the `tests_index_html` hash and run the script

require 'bundler/inline'
require 'json'
require 'cgi'
require "bundler/inline"
require "json"
require "cgi"

gemfile do
source 'https://rubygems.org'
gem 'nokogiri'
source "https://rubygems.org"
gem "nokogiri"
end

tests_index_html = {
env_expandable_keys: [],
gems_dir: "/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/",
backtrace_links_enabled: true,
gems_data: [
{
name: "activerecord",
url: "https://github.com/rails/rails/tree/v6.0.1/activerecord"
}
{ name: "activerecord", url: "https://github.com/rails/rails/tree/v6.0.1/activerecord" },
],
directories: [
{
path: "/var/www/discourse",
url: "https://github.com/discourse/discourse",
main_app: true
},
{ path: "/var/www/discourse", url: "https://github.com/discourse/discourse", main_app: true },
{
path: "/var/www/discourse/plugins/discourse-prometheus",
url: "https://github.com/discourse/discourse-prometheus"
}
url: "https://github.com/discourse/discourse-prometheus",
},
],
application_version: "ce512452b512b909c38e9c63f2a0e1f8c17a2399"
application_version: "ce512452b512b909c38e9c63f2a0e1f8c17a2399",
}

content = File.read("tests/index.html")
Expand Down
12 changes: 5 additions & 7 deletions lib/examples/sidekiq_logster_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ class SidekiqLogsterReporter
def call(ex, context = {})
# Pass context to Logster
fake_env = {}
context.each do |key, value|
Logster.add_to_env(fake_env, key, value)
end
context.each { |key, value| Logster.add_to_env(fake_env, key, value) }

text = "Job exception: #{ex}\n"
if ex.backtrace
Logster.add_to_env(fake_env, :backtrace, ex.backtrace)
end
Logster.add_to_env(fake_env, :backtrace, ex.backtrace) if ex.backtrace

Thread.current[Logster::Logger::LOGSTER_ENV] = fake_env
Logster.logger.error(text)
rescue => e
Logster.logger.fatal("Failed to log exception #{ex} #{hash}\nReason: #{e.class} #{e}\n#{e.backtrace.join("\n")}")
Logster.logger.fatal(
"Failed to log exception #{ex} #{hash}\nReason: #{e.class} #{e}\n#{e.backtrace.join("\n")}",
)
ensure
Thread.current[Logster::Logger::LOGSTER_ENV] = nil
end
Expand Down
32 changes: 15 additions & 17 deletions lib/logster.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# frozen_string_literal: true

require 'logster/version'
require 'logster/logger'
require 'logster/message'
require 'logster/configuration'
require 'logster/web'
require 'logster/ignore_pattern'
require 'logster/pattern'
require 'logster/suppression_pattern'
require 'logster/grouping_pattern'
require 'logster/group'
require 'logster/cache'

if defined? Redis
require 'logster/redis_store'
require "logster/version"
require "logster/logger"
require "logster/message"
require "logster/configuration"
require "logster/web"
require "logster/ignore_pattern"
require "logster/pattern"
require "logster/suppression_pattern"
require "logster/grouping_pattern"
require "logster/group"
require "logster/cache"

if defined?(Redis)
require "logster/redis_store"
else
STDERR.puts "ERROR: Redis is not loaded, ensure redis gem is required before logster"
exit
Expand Down Expand Up @@ -57,6 +57,4 @@ def self.set_environments(envs)
# check logster/configuration.rb for config options
# Logster.config.environments << :staging

if defined?(::Rails) && ::Rails::VERSION::MAJOR.to_i >= 3
require 'logster/rails/railtie'
end
require "logster/rails/railtie" if defined?(::Rails) && ::Rails::VERSION::MAJOR.to_i >= 3
48 changes: 26 additions & 22 deletions lib/logster/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Logster
class BaseStore

attr_accessor :level, :max_retention, :skip_empty, :ignore, :allow_custom_patterns

def initialize
Expand Down Expand Up @@ -166,31 +165,35 @@ def report(severity, progname, msg, opts = {})
message.populate_from_env(env)

if backtrace
if backtrace.respond_to? :join
backtrace = backtrace.join("\n")
end
backtrace = backtrace.join("\n") if backtrace.respond_to? :join
message.backtrace = backtrace
else
message.backtrace = caller.join("\n")
end

return if ignore && ignore.any? do |pattern|
if message =~ pattern
val = Regexp === pattern ? pattern.inspect : pattern.to_s
increment_ignore_count(val)
true
end
if ignore &&
ignore.any? { |pattern|
if message =~ pattern
val = Regexp === pattern ? pattern.inspect : pattern.to_s
increment_ignore_count(val)
true
end
}
return
end

if Logster.config.enable_custom_patterns_via_ui || allow_custom_patterns
custom_ignore = @patterns_cache.fetch(Logster::SuppressionPattern::CACHE_KEY) do
Logster::SuppressionPattern.find_all(store: self)
end
return if custom_ignore.any? do |pattern|
if message =~ pattern
increment_ignore_count(pattern.inspect)
true
custom_ignore =
@patterns_cache.fetch(Logster::SuppressionPattern::CACHE_KEY) do
Logster::SuppressionPattern.find_all(store: self)
end
if custom_ignore.any? { |pattern|
if message =~ pattern
increment_ignore_count(pattern.inspect)
true
end
}
return
end
end

Expand All @@ -199,7 +202,7 @@ def report(severity, progname, msg, opts = {})
if Logster.config.allow_grouping
message.apply_message_size_limit(
Logster.config.maximum_message_size_bytes,
gems_dir: Logster.config.gems_dir
gems_dir: Logster.config.gems_dir,
)
key = self.similar_key(message)
similar = get(key, load_env: false) if key
Expand All @@ -215,7 +218,7 @@ def report(severity, progname, msg, opts = {})
else
message.apply_message_size_limit(
Logster.config.maximum_message_size_bytes,
gems_dir: Logster.config.gems_dir
gems_dir: Logster.config.gems_dir,
)
saved = save(message)
message
Expand All @@ -224,9 +227,10 @@ def report(severity, progname, msg, opts = {})
message = similar || message

if (Logster.config.enable_custom_patterns_via_ui || allow_custom_patterns) && saved
grouping_patterns = @patterns_cache.fetch(Logster::GroupingPattern::CACHE_KEY) do
Logster::GroupingPattern.find_all(store: self)
end
grouping_patterns =
@patterns_cache.fetch(Logster::GroupingPattern::CACHE_KEY) do
Logster::GroupingPattern.find_all(store: self)
end

grouping_patterns.each do |pattern|
if message =~ pattern
Expand Down
3 changes: 2 additions & 1 deletion lib/logster/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def initialize(age = 2)
end

def fetch(key)
if !@hash.key?(key) || @hash[key][:created_at] + @age < Process.clock_gettime(Process::CLOCK_MONOTONIC)
if !@hash.key?(key) ||
@hash[key][:created_at] + @age < Process.clock_gettime(Process::CLOCK_MONOTONIC)
@hash[key] = { data: yield, created_at: Process.clock_gettime(Process::CLOCK_MONOTONIC) }
end
@hash[key][:data]
Expand Down
6 changes: 3 additions & 3 deletions lib/logster/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class Configuration
:max_env_bytes,
:max_env_count_per_message,
:maximum_message_length,
:use_full_hostname
:use_full_hostname,
)

attr_writer :subdirectory

def initialize
# lambda |env,block|
@current_context = lambda { |_, &block| block.call }
@environments = [:development, :production]
@environments = %i[development production]
@subdirectory = nil
@env_expandable_keys = []
@enable_custom_patterns_via_ui = false
Expand All @@ -50,7 +50,7 @@ def initialize
end

def subdirectory
@subdirectory || '/logs'
@subdirectory || "/logs"
end
end
end
6 changes: 2 additions & 4 deletions lib/logster/defer_logger.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# frozen_string_literal: true

require 'logster/scheduler'
require "logster/scheduler"

module Logster
class DeferLogger < ::Logster::Logger
private

def report_to_store(severity, progname, message, opts = {})
opts[:backtrace] ||= caller.join("\n")
Logster::Scheduler.schedule do
super(severity, progname, message, opts)
end
Logster::Scheduler.schedule { super(severity, progname, message, opts) }
end
end
end
37 changes: 17 additions & 20 deletions lib/logster/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ def initialize(key, messages_keys = [], timestamp: 0, count: 0)

def self.from_json(json)
hash = JSON.parse(json)
group = new(
hash["key"],
hash["messages_keys"],
timestamp: hash["timestamp"] || 0,
count: hash["count"] || 0
)
group =
new(
hash["key"],
hash["messages_keys"],
timestamp: hash["timestamp"] || 0,
count: hash["count"] || 0,
)
group.changed = false
group
end
Expand All @@ -32,12 +33,7 @@ def self.max_size
end

def to_h
{
key: @key,
messages_keys: @messages_keys,
timestamp: @timestamp,
count: @count
}
{ key: @key, messages_keys: @messages_keys, timestamp: @timestamp, count: @count }
end

def to_h_web
Expand All @@ -47,7 +43,7 @@ def to_h_web
timestamp: @timestamp,
messages: @messages,
severity: -1,
group: true
group: true,
}
end

Expand Down Expand Up @@ -111,14 +107,15 @@ def max_size
self.class.max_size
end

GroupWeb = Struct.new(*%i[regex count timestamp messages row_id]) do
def to_json(opts = nil)
JSON.fast_generate(self.to_h.merge(severity: -1, group: true), opts)
end
GroupWeb =
Struct.new(*%i[regex count timestamp messages row_id]) do
def to_json(opts = nil)
JSON.fast_generate(self.to_h.merge(severity: -1, group: true), opts)
end

def key
self.regex # alias for testing convenience
def key
self.regex # alias for testing convenience
end
end
end
end
end
3 changes: 1 addition & 2 deletions lib/logster/ignore_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Logster
class IgnorePattern

def initialize(message_pattern = nil, env_patterns = nil)
@msg_match = message_pattern
@env_match = env_patterns
Expand Down Expand Up @@ -44,7 +43,7 @@ def compare(message, pattern)
else
false
end
else
else
false
end
end
Expand Down
Loading

0 comments on commit 9f1e648

Please sign in to comment.