https://github.com/nowlinuxing/kushojin
Send and record changes of ActiveRecord models via Fluentd
https://github.com/nowlinuxing/kushojin
activerecord fluentd ruby ruby-on-rails
Last synced: about 1 month ago
JSON representation
Send and record changes of ActiveRecord models via Fluentd
- Host: GitHub
- URL: https://github.com/nowlinuxing/kushojin
- Owner: nowlinuxing
- License: mit
- Created: 2017-07-11T18:54:15.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-08-23T07:50:01.000Z (almost 4 years ago)
- Last Synced: 2025-02-25T05:05:54.785Z (over 1 year ago)
- Topics: activerecord, fluentd, ruby, ruby-on-rails
- Language: Ruby
- Homepage:
- Size: 76.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Kushojin
[](https://badge.fury.io/rb/kushojin)
[](https://github.com/nowlinuxing/kushojin/actions)
[](https://codeclimate.com/github/nowlinuxing/kushojin/maintainability)
[](https://codeclimate.com/github/nowlinuxing/kushojin/test_coverage)
Kushojin gathers changes to the attributes of the ActiveRecord model and sends them externally via Fluentd.
This is useful for logging, tracking and real-time aggregation of accesses involving database updates.
Since Fluentd is used for external transmission, Kushojin can respond flexibly to various requests.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'kushojin'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install kushojin
## Usage
```ruby
Kushojin::Config.logger = Fluent::Logger::FluentLogger.new(nil, host: "localhost", port: 24224)
class User < ApplicationRecord
record_changes
end
class UsersController < ApplicationController
send_changes
def create
User.create(params[:user])
end
end
```
$ curl -X POST -d "user[name]=bill&user[age]=20" http://localhost:3000/users
# output: users.create {"event":"create","request_id":"4afd0731-dd25-4668-b769-2017dbdd3642","table_name":"users","id":1,"changes":{"name":[null,"bill"],"age":[null,20]}}
Changes is recorded when the model is created, updated and destroyed.
The `:only` option can be used same as filters of controller.
```ruby
class User < ApplicationRecord
record_changes only: [:create, :destroy]
end
```
$ curl -X POST -d "user[name]=bill&user[age]=20" http://localhost:3000/users
# output: users.create {"event":"create","request_id":"4afd0731-dd25-4668-b769-2017dbdd3642","table_name":"users","id":1,"changes":{"name":[null,"bill"],"age":[null,20]}}
$ curl -X PATCH -d "user[age]=21" http://localhost:3000/users/1
# no output
### Customize sending fields of changes
Kushojin sends model changes with some information:
- tag: Controller name and action name concatenated with a period.
- event: The event which model is changed on.
- table_name: Table name of the model.
- primary key: Primary key name and value.
- changes: Model changes without its primary key, `created_at`, and `updated_at`. It contains pairs of attribute name and before/after values.
- request_id: Request ID.
It is able to pass a customized sender to add additional information.
```ruby
class CustomSender < Kushojin::Sender::EachSender
private
# Add "user_id"
def serialize(change, controller)
super.merge!("user_id" => controller.current_user.id)
end
end
class MessagesController < ApplicationController
send_changes Kushojin::ControllerMethods::SendChangeCallback.new(sender: CustomSender.new)
def current_user
return_user_record_with_any_authentication_logic
end
def create
Message.create(params[:message])
end
end
```
### Customize callbacks of model
You can pass in a class or an instance to change behaviors of the callbacks.
```ruby
class CustomCallbacks
# Must be able to respond to after_create, after_update, and after_destroy.
def after_create(record); end
def after_update(record); end
def after_destroy(record); end
end
class User < ApplicationRecord
record_changes CustomCallbacks.new
end
```
### Override
You can override options of Recording changes in subclass.
```ruby
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
record_changes
end
class User < ApplicationRecord
record_changes only: [:create, :destroy]
end
```
## 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](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/nowlinuxing/kushojin.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).