Skip to content

ez-engines/ez-tracer

Repository files navigation

Ez::Tracer

Rubocop & RSpec

It is often hard to give an answer to the question: "What is fu#1ing going on in my app?". Why data is so strange? What changed the state, when and why?

It's because you don't trace workflows. EzTracer comes here to rescue you from blind and silent operations.

How to use

class MyOperation
  # No magic include or inheritance, just initialize private memoized method like tracer

  def call
    # Log simple status message
    tracer.log('Preparing some data...')
    # ruby code that prepeared some data
    # Log message and measure how much time it took since the last message
    tracer.log('Data prepared', spent: true)
    # ruby code changed this data
    # Again log simple status message
    tracer.log('Executing data change')
    # Aggregate changed state
    changed = ['a', 'b', 'c']
    # Log aggregated state and measure how much it took to perform
    tracer.log('Data has been changed', collection: changed, spent: true)
    # Log complete of the workflow
    tracer.done!
  end

  private

  def tracer
    @tracer ||= Ez::Tracer::Agent.new(
      # Tracer need any global unique ID to identify reference later on
      id: 'any-given-id',

      # Define how an agent should store logged trace
      storage: Ez::Tracer::Storage::FileSystem.new("logs/my_operation/#{'any-given-id'}-#{Time.now.utc.to_i}"),

      # Agent by default will buffer all logs and save them on `done!`
      # But you can change this behavior to save every line after log has been called
      buffer: false,

      # On every `log` execution you can trigger proc with current tracer object and message
      stream: proc do |tracer, message|
        NotificationsChannel.broadcast_to("tracer-#{'any-given-id'}", message: message)
      end
  end
end

operation = MyOperation.new
operation.call
operation.tracer.logs # =>
# 2020-08-05 12:57:13 UTC => Preparing some data...
# 2020-08-05 12:57:18 UTC => Data prepared. Spent 5 seconds
# 2020-08-05 12:57:18 UTC => Executing data change
# 2020-08-05 12:57:30 UTC => Data has been changed: a, b, c. Spent 12 seconds
# 2020-08-05 12:57:30 UTC => Done.
operation.tracer.save # => Persist traced logs within given storage backend

Use cases

  • Massive data transactions
  • External API calls, request-response details
  • CSV imports
  • Long running background jobs ... any workflow execution where you need to track and log changes

Storage backend

Default STDOUT

Ez::Tracer::Storage::Stdout

Local filesystem

Ez::Tracer::Storage::FileSystem.new('file_path')

AWS S3

aws_credentials = {
  bucket:     ENV['AWS_S3_BUCKET'],
  key_id:     ENV['AWS_ACCESS_KEY_ID'],
  access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  region:     ENV['AWS_S3_REGION'],
  endpoint:   ENV['AWS_S3_ENDPOINT']
}

Ez::Tracer::Storage::AwsS3.new(aws_credentials, 'prefix/file_name')

Installation

Add this line to your application's Gemfile:

gem 'ez-tracer'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install ez-tracer

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ez-tracer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Ez::Tracer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Releases

No releases published

Packages

No packages published