Skip to content

Commit

Permalink
Add Observe example
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmorrison authored Jun 28, 2024
1 parent a543ec5 commit 5b48391
Showing 1 changed file with 91 additions and 1 deletion.
92 changes: 91 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,91 @@

Rails Semantic Logger replaces the Rails default logger with [Semantic Logger](https://logger.rocketjob.io/)

When any large Rails application is deployed to production one of the first steps is to move to centralized logging, so that logs can be viewed and searched from a central location.

Centralized logging quickly falls apart when trying to consume the current human readable log files:
- Log entries often span multiple lines, resulting in unrelated log lines in the centralized logging system. For example, stack traces.
- Complex Regular Expressions are needed to parse the text lines and make them machine readable. For example to build queries, or alerts that are looking for specific elements in the message.
- Writing searches, alerts, or dashboards based on text logs is incredibly brittle, since a small change to the text logged can often break the parsing of those logs.
- Every log entry often has a completely different format, making it difficult to make consistent searches against the data.

For these and many other reasons switching to structured logging, or logs in JSON format, in testing and production makes centralized logging incredibly powerful.

For example, adding these lines to `config/application.rb` and removing any other log overrides from other environments, will switch automatically to structured logging when running inside Kubernetes:
~~~ruby
# Setup structured logging
config.semantic_logger.application = "my_application"
config.semantic_logger.environment = ENV["STACK_NAME"] || Rails.env

# Switch to JSON Logging output to stdout when running on Kubernetes
if ENV["LOG_TO_CONSOLE"] || ENV["KUBERNETES_SERVICE_HOST"]
config.rails_semantic_logger.add_file_appender = false
config.semantic_logger.add_appender(io: $stdout, level: ENV["LOG_LEVEL"] || config.log_level, formatter: :json)
end
~~~

Then configure the centralized logging system to tell it that the data is in JSON format, so that it will parse it for you into a hierarchy.

For example, the following will instruct [Observe](https://www.observeinc.com/) to parse the JSON data and create machine readable data from it:
~~~ruby
interface "log", "log":log

make_col event:parse_json(log)

make_col
time:parse_isotime(event.timestamp),
duration:duration_ms(event.duration_ms),
level:string(event.level),
name:string(event.name),
message:string(event.message),
metric:string(event.metric),
named_tags:event.named_tags,
payload:event.payload,
tags:array(event.tags),
host:string(event.host),
application:string(event.application),
environment:string(event.environment),
level_index:int64(event.level_index),
pid:int64(event.pid),
thread:string(event.thread),
file:string(event.file),
line:int64(event.line),
backtrace:array(event.backtrace),
metric_amount:int64(event.metric_amount),
dimensions:event.dimensions,
exception:event.exception
~~~

Now queries can be built to drill down into each of these fields, including `payload` which is a nested object.

For example to find all failed Sidekiq job calls where the causing exception class name is `NoMethodError`:
~~~ruby
filter environment = "uat2"
filter level = "error"
filter metric ~ /Sidekiq/
filter (string(exception.cause.name) = "NoMethodError")
~~~

Or, to create a dashboard showing the duration of all successful Sidekiq jobs:
~~~ruby
filter environment = "production"
filter level = "info"
filter metric ~ /Sidekiq.*/
timechart duration:avg(duration), group_by(name)
~~~

* http://github.com/reidmorrison/rails_semantic_logger

## Documentation

For complete documentation see: https://logger.rocketjob.io/rails

## Upgrading to Semantic Logger v4.15 - Sidekiq Support

Rails Semantic Logger introduces direct support for Sidekiq v4, v5, v6, and v7.
Please remove any previous custom patches or configurations to make Sidekiq work with Semantic Logger.
To see the complete list of patches being made, and to contribute your own changes, see: [Sidekiq Patches](https://github.com/reidmorrison/rails_semantic_logger/blob/master/lib/rails_semantic_logger/extensions/sidekiq/sidekiq.rb)

## Upgrading to Semantic Logger v4.4

With some forking frameworks it is necessary to call `reopen` after the fork. With v4.4 the
Expand All @@ -19,7 +98,18 @@ I.e. Please remove the following line if being called anywhere:
SemanticLogger::Processor.instance.instance_variable_set(:@queue, Queue.new)
~~~

## Supports
## New Versions of Rails, etc.

The primary purpose of the Rails Semantic Logger gem is to patch other gems, primarily Rails, to make them support structured logging though Semantic Logger.

When new versions of Rails and other gems are published they often make changes to the internals, so the existing patches stop working.

Rails Semantic Logger survives only when someone in the community upgrades to a newer Rails or other supported libraries, runs into problems,
and then contributes the fix back to the community by means of a pull request.

Additionally, when new popular gems come out, we rely only the community to supply the necessary patches in Rails Semantic Logger to make those gems support structured logging.

## Supported Platforms

For the complete list of supported Ruby and Rails versions, see the [Testing file](https://github.com/reidmorrison/rails_semantic_logger/blob/master/.github/workflows/ci.yml).

Expand Down

0 comments on commit 5b48391

Please sign in to comment.