twirp_rails 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +38 -5
- data/lib/twirp_rails/engine.rb +0 -4
- data/lib/twirp_rails/generators/twirp/twirp_generator.rb +2 -17
- data/lib/twirp_rails/log_subscriber.rb +27 -0
- data/lib/twirp_rails/version.rb +1 -1
- data/lib/twirp_rails.rb +8 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed5981c2bd96acaa3ea71757071e805a939edc81796009d70d0ce8308ad7ee29
|
4
|
+
data.tar.gz: 49e69faa936f5efb286b2e35de4b7558ac6811d4f487b06af2bfdb71c929749f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d3110ce249d65dcab59868b555b9bfa01eef0b430e8e5bd00ae8084dc92415868d14b7f717cde451a8e1c99796eda82f7f0baeffc9725d5d3ba26e2bd04ecb9
|
7
|
+
data.tar.gz: 83e8e6a07c327d539d9b6b8a97a6f68a2699171b666320ca679fb2cdf72d92cd56fcafc6f93d27f40b4258cfb22bf7ba040f80bee82a5c525750af7a8e2cb935
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## 0.3.0 - 2020-02-28
|
8
|
+
|
9
|
+
### Added
|
10
|
+
- Ability to detailed log twirp calls. Add `TwirpRails.log_twirp_calls!` to the initializer.
|
11
|
+
|
12
|
+
### Breaking changes
|
13
|
+
- Services not been instrumented via `ActiveSupport::Notifications` unless `TwirpRails.log_twirp_calls!` used.
|
14
|
+
|
7
15
|
## 0.2.0 - 2020-02-21
|
8
16
|
|
9
17
|
### Breaking changes
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
# TwirpRails
|
2
2
|
|
3
|
-
TwirpRails helps to use [twirp-ruby gem](https://github.com/twitchtv/twirp-ruby) with rails
|
4
|
-
|
3
|
+
TwirpRails helps to use [twirp-ruby gem](https://github.com/twitchtv/twirp-ruby) with rails.
|
4
|
+
|
5
|
+
* twirp code generation from ```.proto``` file
|
6
|
+
* handler, rspec and swagger code generation from ```.proto``` file
|
7
|
+
* `mount_twirp` route helper to mount handlers
|
8
|
+
* `rpc` helper to dry your handlers specs
|
9
|
+
* ability to log twirp calls by Rails logger
|
5
10
|
|
6
11
|
## Installation
|
7
12
|
|
@@ -71,8 +76,8 @@ rails s
|
|
71
76
|
|
72
77
|
And check it from rails console.
|
73
78
|
```ruby
|
74
|
-
PeopleClient.new('http://localhost:3000').get_name(
|
75
|
-
=> "Name of
|
79
|
+
PeopleClient.new('http://localhost:3000/twirp').get_name(uid: 'starship').data.name
|
80
|
+
=> "Name of starship"
|
76
81
|
```
|
77
82
|
|
78
83
|
### Test your service with rspec
|
@@ -81,8 +86,8 @@ If you use RSpec, twirp generator creates handler spec file with all service met
|
|
81
86
|
|
82
87
|
```ruby
|
83
88
|
describe TeamsHandler do
|
84
|
-
|
85
89
|
context '#get' do
|
90
|
+
let(:team) { create(:team) }
|
86
91
|
rpc { [:get, id: team.id] }
|
87
92
|
|
88
93
|
it { should match(team: team.to_twirp) }
|
@@ -101,6 +106,34 @@ end
|
|
101
106
|
|
102
107
|
or run ```rails g twirp:rspec``` to do it automatically.
|
103
108
|
|
109
|
+
## Log twirp calls
|
110
|
+
|
111
|
+
By default, Rails logs only start of POST request. To get a more detailed log of twirp calls, add this code to the initializer.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
# config/initializers/twirp_rails.rb
|
115
|
+
TwirpRails.log_twirp_calls!
|
116
|
+
```
|
117
|
+
|
118
|
+
You can customize log output by pass a block argument
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# config/initializers/twirp_rails.rb
|
122
|
+
TwirpRails.log_twirp_calls! do |event|
|
123
|
+
twirp_call_info = {
|
124
|
+
duration: event.duration,
|
125
|
+
method: event.payload[:env][:rpc_method],
|
126
|
+
params: event.payload[:env][:input].to_h
|
127
|
+
}
|
128
|
+
|
129
|
+
if (exception = event.payload[:env][:exception])
|
130
|
+
twirp_call_info[:exception] = exception
|
131
|
+
end
|
132
|
+
|
133
|
+
Rails.logger.info "method=%{method} duration=%{duration}" % twirp_call_info
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
104
137
|
## Development
|
105
138
|
|
106
139
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/twirp_rails/engine.rb
CHANGED
@@ -15,10 +15,6 @@ module TwirpRails
|
|
15
15
|
TwirpRails::RavenAdapter.install
|
16
16
|
end
|
17
17
|
|
18
|
-
initializer 'twirp_rails.logging' do
|
19
|
-
TwirpRails::LoggingAdapter.install
|
20
|
-
end
|
21
|
-
|
22
18
|
initializer 'twirp_rails.require_generated_files' do
|
23
19
|
TwirpRails::Twirp.auto_require_twirp_files
|
24
20
|
end
|
@@ -6,7 +6,8 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
6
6
|
class_option :skip_swagger, type: :boolean, default: false
|
7
7
|
class_option :swagger_out, type: :string, default: 'public/swagger'
|
8
8
|
|
9
|
-
|
9
|
+
GOPATH = ENV.fetch('GOPATH') { File.expand_path('~/go') }
|
10
|
+
GO_BIN_PATH = File.join(GOPATH, 'bin')
|
10
11
|
TWIRP_PLUGIN_PATH = ENV.fetch('TWIRP_PLUGIN_PATH') { File.join(GO_BIN_PATH, 'protoc-gen-twirp_ruby') }
|
11
12
|
SWAGGER_PLUGIN_PATH = ENV.fetch('SWAGGER_PLUGIN_PATH') { File.join(GO_BIN_PATH, 'protoc-gen-twirp_swagger') }
|
12
13
|
PROTOC_PATH = `which protoc`.chomp
|
@@ -105,22 +106,6 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
105
106
|
end
|
106
107
|
end
|
107
108
|
|
108
|
-
def inject_rspec_helper
|
109
|
-
in_root do
|
110
|
-
return unless File.exist?('spec/rails_helper.rb')
|
111
|
-
|
112
|
-
require_sentinel = %r{require 'rspec/rails'\s*\n}m
|
113
|
-
include_sentinel = /RSpec\.configure do |config|\s*\n/m
|
114
|
-
|
115
|
-
inject_into_file 'spec/rails_helper.rb',
|
116
|
-
"require 'twirp/rails/rspec/helper'",
|
117
|
-
after: require_sentinel, verbose: true, force: false
|
118
|
-
inject_into_file 'spec/rails_helper.rb',
|
119
|
-
' config.include TwirpRails::RSpec::Helper, type: :rpc, file_path: %r{spec/rpc}',
|
120
|
-
after: include_sentinel, verbose: true, force: false
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
109
|
private
|
125
110
|
|
126
111
|
def proto_type_to_ruby(result_type)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module TwirpRails
|
2
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
3
|
+
cattr_accessor :log_writer
|
4
|
+
|
5
|
+
def instrumenter(event)
|
6
|
+
if LogSubscriber.log_writer
|
7
|
+
LogSubscriber.log_writer.call(event)
|
8
|
+
else
|
9
|
+
default_log_writer(event)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def default_log_writer(event)
|
14
|
+
twirp_call_info = {
|
15
|
+
duration: event.duration,
|
16
|
+
method: event.payload[:env][:rpc_method],
|
17
|
+
params: event.payload[:env][:input].to_h
|
18
|
+
}
|
19
|
+
|
20
|
+
if (exception = event.payload[:env][:exception])
|
21
|
+
twirp_call_info[:exception] = exception
|
22
|
+
end
|
23
|
+
|
24
|
+
Rails.logger.info twirp_call_info
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/twirp_rails/version.rb
CHANGED
data/lib/twirp_rails.rb
CHANGED
@@ -5,7 +5,15 @@ require 'twirp_rails/engine'
|
|
5
5
|
require 'twirp_rails/generators/twirp/twirp_generator'
|
6
6
|
require 'twirp_rails/generators/twirp/twirp_rspec_generator'
|
7
7
|
require 'twirp_rails/active_record_extension'
|
8
|
+
require 'twirp_rails/log_subscriber'
|
8
9
|
|
9
10
|
module TwirpRails
|
10
11
|
class Error < StandardError; end
|
12
|
+
|
13
|
+
def self.log_twirp_calls!(&log_writer)
|
14
|
+
TwirpRails::LoggingAdapter.install
|
15
|
+
|
16
|
+
TwirpRails::LogSubscriber.log_writer = log_writer if block_given?
|
17
|
+
TwirpRails::LogSubscriber.attach_to(:twirp)
|
18
|
+
end
|
11
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twirp_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandr Zimin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: twirp
|
@@ -145,6 +145,7 @@ files:
|
|
145
145
|
- lib/twirp_rails/generators/twirp/USAGE
|
146
146
|
- lib/twirp_rails/generators/twirp/twirp_generator.rb
|
147
147
|
- lib/twirp_rails/generators/twirp/twirp_rspec_generator.rb
|
148
|
+
- lib/twirp_rails/log_subscriber.rb
|
148
149
|
- lib/twirp_rails/logging_adapter.rb
|
149
150
|
- lib/twirp_rails/raven_adapter.rb
|
150
151
|
- lib/twirp_rails/routes.rb
|