twirp_rails 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99287bad34a0f5b439f258dfbb0adb7bd62828f1c5f6a34078da76fde019379a
4
- data.tar.gz: f6966d4a6bf9c5dfc808382e0c2392f3a9fa559ba6a195a95d67872e223c35d8
3
+ metadata.gz: ed5981c2bd96acaa3ea71757071e805a939edc81796009d70d0ce8308ad7ee29
4
+ data.tar.gz: 49e69faa936f5efb286b2e35de4b7558ac6811d4f487b06af2bfdb71c929749f
5
5
  SHA512:
6
- metadata.gz: 032fd8c2fb3e9e9c127b35cfd62ac551ec93d30095c6881e4d76ac91ecd6ed26be5bd0d71a381a91d12183c62333303037b6e8539beca626c3e7a3ac6d7c727a
7
- data.tar.gz: d4d5d2a7e4bdf475b1a98587c3280a76108b89b0124a2cf75c72bc50f3c46f036dc0f8c8639f82fc4fb0857d5bc278ac9e41a672ee384f2b5609e3767adfefbd
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twirp_rails (0.2.1)
4
+ twirp_rails (0.3.0)
5
5
  railties (~> 6.0)
6
6
  twirp (~> 1)
7
7
 
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 and to
4
- automate code generation from ```.proto``` files.
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(GetNameRequest.new uid: '1').data.name
75
- => "Name of 1"
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.
@@ -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
- GO_BIN_PATH = ENV.fetch('GOPATH') { File.expand_path('~/go/bin') }
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
@@ -1,3 +1,3 @@
1
1
  module TwirpRails
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
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.2.1
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-26 00:00:00.000000000 Z
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